我试图让我的角色改变它的旋转到相机的旋转,所以如果我的相机看着不同的方向,它应该使我的角色移动到那个方向,改变我的角色的方向,然后我的角色移动到那个方向,当我点击"W"。我该怎么修理它?(角色的旋转方向应该与摄像机相同,这样它就可以朝那个方向移动。)
My Character Movement Script:
public Rigidbody rb;
public float moveSpeed;
public Transform Cam;
public Transform target;
void Start()
{
}
void Update()
{
target.rotation = Cam.rotation;
if (Input.GetKey(KeyCode.W))
{
rb.velocity = Vector3.forward * moveSpeed;
}
if (Input.GetKey(KeyCode.S))
{
rb.velocity = Vector3.back * moveSpeed;
}
if (Input.GetKeyDown(KeyCode.Space))
{
rb.velocity = Vector3.up * moveSpeed;
}
if (Input.GetKey(KeyCode.A))
{
rb.velocity = Vector3.left * moveSpeed;
}
if (Input.GetKey(KeyCode.D))
{
rb.velocity = Vector3.right * moveSpeed;
}
if(Input.GetKey(KeyCode.LeftShift))
{
moveSpeed = 8f;
}
if(Input.GetKey(KeyCode.LeftControl))
{
moveSpeed = 3f;
}
}
}
My Camera Follow Script:
public float rotateSpeed;
private float moveX;
public Transform target;
public Vector3 offset;
private float moveY;
public Transform Cam;
void Start()
{
}
void Update()
{
transform.position = target.position + offset;
moveX = Input.GetAxis("Mouse X");
moveY = Input.GetAxis("Mouse Y");
transform.Rotate(0f, moveX * rotateSpeed * Time.deltaTime, 0f);
target.rotation = Cam.rotation;
}
}
目前你正在使用Vector3属性设置刚体速度,这将返回一个相对于世界坐标的矢量。您需要设置相对于字符旋转的速度,这可以通过更改
来实现。rb.velocity = Vector3.forward * moveSpeed;
移动到
rb.velocity = target.transform.forward * moveSpeed;
等。