1.建立场景,编写控制主角移动代码(此代码放在主角身上)。摄像机调整好角度后直接放在主角中;关于主角仍需要添加刚体和碰撞器,并进行一等调整。(如下图所示)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControl : MonoBehaviour
{
//A、D控制主角左右移动 空格或W控制主角跳跃 主角一直跑
public GameObject player;
public float Movespeed = 5f;
public float Jumpspeed = 50f;
public static float RunSpeed;
private void Start()
{
RunSpeed = 10f;
}
void Update()
{
UserInterface();//调用函数,执行
AlwaysMove();
}
void UserInterface()//检测用户交互
{
float LeftrightValue = Input.GetAxis("Horizontal");//用户输入A D得到-1和+1
PlyHoriMove(LeftrightValue);//形参传递,调用函数
if(Input .GetKey(KeyCode.W))
{
Jump();
}
}
void PlyHoriMove(float HoriValue)//使用,接,名字可以和上边不一样;控制角色左右移动
{
player.transform.Translate(HoriValue * Movespeed * Time.deltaTime,0, 0);
}
void Jump()//控制角色跳跃
{
player.transform.Translate(new Vector3(0, Jumpspeed * Time.deltaTime, 0));
}
void AlwaysMove()//使角色一直向前跑
{
// Debug.Log("角色一直跑");
player.transform.Translate(new Vector3(0