我正在创建一个与常规略有不同的 3D 无尽跑步者。有 5 条 1 单位宽的车道,如果玩家向左或向右移动太远,他们可能会从两侧跌落。播放器不会向前移动、跳跃或滑动。它只有左或右运动。如果被迎面而来的障碍物击中,随着时间的推移速度会增加,玩家可能会被反弹回来。
关于玩家的左/右移动,我非常接近。但是我无法准确定位播放器。
有时,当玩家移动到一条车道上时,位置有时会偏离 0.1,并且可能会随着时间的推移开始累积。即从0 位置开始,向左移动,在0.9 或1.1 处停止。
这是到目前为止的代码 -
{
public float speed;
public float mleft = -0.7f;
public float mright = 0.7f;
private Rigidbody rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate()
{
if (Input.GetKeyDown("a"))
{
rb.velocity = new Vector3(mleft, 0, 0) * speed;
StartCoroutine(stopLaneCH());
}
Debug.Log(GetComponent<Transform>().position);
if (Input.GetKeyDown("d"))
{
rb.velocity = new Vector3(mright, 0, 0) * speed;
StartCoroutine(stopLaneCH());
}
}
IEnumerator stopLaneCH ()
{
yield return new WaitForSeconds(0.5f);
GetComponent<Rigidbody>().velocity = new Vector3(0, 0, 0);
}
}
我不得不尝试在速度和mleft/mright变量之间找到平衡,这会使玩家移动 1 个单位。
我对 Unity 和编程还很陌生,但是非常感谢您对这个问题的任何帮助。
我什至可以用任何帮助解决这个问题的人来命名我的第三个孩子。