创建物体,加上Character Controller的组件
要实现人物移动需要先获取输入
float x, y;
x = Input.GetAxis("Horizontal");
y = Input.GetAxis("Vertical");
移动逻辑
Vector3 move;
move = transform.right * x + transform.forward * y;
playercontroller.Move(move * speed * Time.deltaTime);
完整代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playermove : MonoBehaviour
{
public float speed = 5.0f;
public CharacterController playercontroller;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
float x, y;
x = Input.GetAxis("Horizontal");
y = Input.GetAxis("Vertical");
Vector3 move;
move = transform.right * x + transform.forward * y;
playercontroller.Move(move * speed * Time.deltaTime);
}
}