【发布时间】:2020-11-03 01:26:43
【问题描述】:
我正在创建一个与常规略有不同的 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);
}
}
我不得不尝试在 speed 和 mleft/mright 变量之间找到平衡,以使玩家移动 1 个单位。
我对 Unity 和编程还很陌生,但非常感谢任何有关此问题的帮助。
我什至可以用任何帮助解决这个问题的人来命名我的第三个孩子。
【问题讨论】:
-
运动是否必须基于物理?
-
????由于我没有那么有经验,所以我不确定。但我猜是的。想想有人站在桥上躲避迎面而来的汽车。他们不前进,只有左右躲避汽车。但是如果他们被车撞了,他们就会被撞到后面。