使用 GetAxisRaw() 和 transform.Translate() 移动
using GetAxisRaw() and transform.Translate() to move
首先...我的母语不是英语,很抱歉英语不好,我正在努力提高它们。
所以,我正在研究这个简单的角色移动系统,在测试时我意识到,如果按下 "W" 或 "S" 键,玩家将向前移动,如果我按下"A" 或 "D" 播放器会向后移动,而不是侧向移动,我一直在搜索和搜索,但找不到与我的问题类似的东西...有人可以帮助我吗?
void Update () {
// input
Vector2 input = new Vector2 (Input.GetAxisRaw ("Horizontal"), Input.GetAxisRaw ("Vertical"));
Vector2 inputDir = input.normalized;
bool running = Input.GetKey (KeyCode.LeftShift);
Move (input, running);
if (Input.GetKeyDown (KeyCode.Space)) {
Jump ();
}
}
void Move(Vector2 inputDir, bool running) {
float targetSpeed = ((running) ? runSpeed : walkSpeed);
float realSpeed = targetSpeed * Time.deltaTime;
Vector3 directionToMove = new Vector3 (inputDir.x, 0, inputDir.y) * realSpeed;
transform.Translate (directionToMove);
}
When i press "S" he doesn't move backwards, he move forward too,and
when i press "A" he move backwards, when i press "D" he also move
backwards
代码没有问题,应该可以正常工作。
您的输入设置可能已被您或外部插件修改。有时,这只是一个错误。只需重置它,"WASD" 键应该可以正常工作。
转到编辑 ---> 项目设置 ---> 输入 然后单击设置图标并重置它。
如果问题仍然存在,请确保您没有从另一个脚本中移动该对象。尝试禁用其他脚本以查看导致问题的脚本。
首先...我的母语不是英语,很抱歉英语不好,我正在努力提高它们。
所以,我正在研究这个简单的角色移动系统,在测试时我意识到,如果按下 "W" 或 "S" 键,玩家将向前移动,如果我按下"A" 或 "D" 播放器会向后移动,而不是侧向移动,我一直在搜索和搜索,但找不到与我的问题类似的东西...有人可以帮助我吗?
void Update () {
// input
Vector2 input = new Vector2 (Input.GetAxisRaw ("Horizontal"), Input.GetAxisRaw ("Vertical"));
Vector2 inputDir = input.normalized;
bool running = Input.GetKey (KeyCode.LeftShift);
Move (input, running);
if (Input.GetKeyDown (KeyCode.Space)) {
Jump ();
}
}
void Move(Vector2 inputDir, bool running) {
float targetSpeed = ((running) ? runSpeed : walkSpeed);
float realSpeed = targetSpeed * Time.deltaTime;
Vector3 directionToMove = new Vector3 (inputDir.x, 0, inputDir.y) * realSpeed;
transform.Translate (directionToMove);
}
When i press "S" he doesn't move backwards, he move forward too,and when i press "A" he move backwards, when i press "D" he also move backwards
代码没有问题,应该可以正常工作。
您的输入设置可能已被您或外部插件修改。有时,这只是一个错误。只需重置它,"WASD" 键应该可以正常工作。
转到编辑 ---> 项目设置 ---> 输入 然后单击设置图标并重置它。
如果问题仍然存在,请确保您没有从另一个脚本中移动该对象。尝试禁用其他脚本以查看导致问题的脚本。