运动和旋转



所以我目前正在学习Unity 3D,但遇到了一个问题。所以Ive完成了这个代码,玩家用WASD键移动。这个动作很好。我试着进行一些轮换,这样球员就可以转身或换到不同的位置。但问题是,我让轮换球员转身,但当他转身时,他用W换S,所以基本上,如果我转身,当我按W向前时,他会向后,当我按下S而不是向后时,他就会向前。我不想在我的代码中做太多更改,因为我认为它非常";基本的";现在很容易理解。所以,如果有人能解释我做错了什么,我将不胜感激。

using System.Collections.Generic;
using UnityEngine;

//[RequireComponent(typeof(Rigidbody))]
public class PlayerMov : MonoBehaviour
{
//Player mov speed
float MovSpeed = 10f;
//Player jump, jumpforce, check if is on the ground and rb(rigidbody component)
public Vector3 jump;
public float jumpForce = 2.0f;
public bool isGrounded;
Rigidbody rb;
public float TurnSpeed = 100f;




void Start()
{

rb = GetComponent<Rigidbody>();
jump = new Vector3(0.0f, 2.0f, 0.0f);
}
void OnCollisionStay()
{
isGrounded = true;
}


void Update()
{
if (Input.GetKey(KeyCode.W))
{
transform.position -= Vector3.forward * MovSpeed * Time.deltaTime;
//transform.Translate(Vector3.forward * MovSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.A))
{
transform.position -= Vector3.left * MovSpeed * Time.deltaTime;

}
if (Input.GetKey(KeyCode.S))
{
transform.position -= Vector3.back * MovSpeed * Time.deltaTime;
//transform.Translate(-Vector3.forward * MovSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D))
{
transform.position -= Vector3.right * MovSpeed * Time.deltaTime;
transform.Rotate(Vector3.up * TurnSpeed * Time.deltaTime);
}
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
rb.AddForce(jump * jumpForce, ForceMode.Impulse);
isGrounded = false;
}
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.Rotate(Vector3.up * TurnSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.RightArrow))
{
transform.Rotate(Vector3.up * TurnSpeed * Time.deltaTime);
}

为什么不使用Translate方法?请参阅其文档。您可以定义您希望变换在中移动的空间(全局或本地(

https://docs.unity3d.com/ScriptReference/Transform.Translate.html

附带说明:为什么要检查每个单独的钥匙?使用CCD_ 1和CCD_。

最新更新