我是 Unity 和 Stack Overflow 的新手,正在寻找一个可以使对象(如玩家)移动的脚本。我找到了一个有效的脚本,好吧,有点有效或去计划。当我测试脚本时,每当我向前按箭头键时,它不会继续前进,而是会开始跳跃。如果我按下向下箭头键,立方体(或玩家)将尝试将自己推到地下,然后永远掉下去,但左右箭头键工作得很好。请注意,此脚本目前仅用于移动播放器,仅用于移动播放器,以防万一您认为它应该用于其他用途或不同用途。这是代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour {
public float moveSpeed;
// Use this for initialization
void Start()
{
moveSpeed = 5f;
}
// Update is called once per frame
void Update()
{
transform.Translate(moveSpeed * Input.GetAxis("Horizontal") * Time.deltaTime, moveSpeed * Input.GetAxis("Vertical") * Time.deltaTime, 0f);
}
}
希望您能找到解决方案或能找到解释。感谢您的回复。 问候, 用户:9104031
最佳答案
Unity 使用 Y 向上 Z 向前的坐标系。
在您的代码中,您根据 Y 轴上的“垂直”轴输入移动播放器,这当然会将您的向上/向下箭头键映射到错误的方向。
你要做的就是改变
transform.Translate(
moveSpeed * Input.GetAxis("Horizontal") * Time.deltaTime
, moveSpeed * Input.GetAxis("Vertical") * Time.deltaTime
, 0f);
到
transform.Translate(
moveSpeed * Input.GetAxis("Horizontal") * Time.deltaTime
, 0f
, moveSpeed * Input.GetAxis("Vertical") * Time.deltaTime);
关于c# - 用于移动 3D 对象的 Unity 脚本并未完全按计划进行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48087882/