0

我正在创建一个与常规略有不同的 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 和编程还很陌生,但是非常感谢您对这个问题的任何帮助。

我什至可以用任何帮助解决这个问题的人来命名我的第三个孩子。

4

2 回答 2

1

如果我理解这一点是正确的,而不是改变速度,因为让它精确移动 1 个单位几乎是不可能的。相反,为什么不将玩家精确地移动一个单位呢?使用协程,您可以使用 Vector3.Lerp 方法随着时间的推移从当前位置移动到所需位置。您想要的位置是 1 个单位,或者,您的每条车道与当前位置之间的距离。

void Update(){

    //For Moving Left
    if (Input.GetKeyDown(KeyCode.A))
            StartCoroutine(MoveToPosition(transform, new Vector3(transform.position.x - 1, transform.position.y, transform.position.z), 0.5f));
    
    //For Moving Right
    if (Input.GetKeyDown(KeyCode.D))
            StartCoroutine(MoveToPosition(transform, new Vector3(transform.position.x - 1, transform.position.y, transform.position.z), 0.5f));
}

public IEnumerator MoveToPosition(Transform transform, Vector3 position, float timeToMove)
    {
        var currentPos = transform.position;
        var t = 0f;
        while (t < 1)
        {
            t += Time.deltaTime / timeToMove;
            transform.position = Vector3.Lerp(currentPos, position, t);
            yield return null;
        }
    }

我假设您在 X 轴上向左/向右移动,如果不是,只需将 - 1 更改为您正在移动的轴。您也可以根据自己的喜好更改 TimeToMove 浮动。

希望这可以帮助。如果您有任何问题随时问!

于 2020-11-02T15:02:18.460 回答
0

在这里,您可以使用 Time.deltaTime 属性。速度的值应该是“你想在 1 秒内扫描多少个单位”乘以 2,因为你的协程将在 0.5 秒内停止。

或者,如果您的协程必须在 1.0 秒内停止。那么速度的值将是“你想在 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 * Time.deltaTime;
        StartCoroutine(stopLaneCH());
    }

    Debug.Log(GetComponent<Transform>().position);

    if (Input.GetKeyDown("d"))
    {
        rb.velocity = new Vector3(mright, 0, 0) * speed * Time.deltaTime;
        StartCoroutine(stopLaneCH());
    }
    

}

IEnumerator stopLaneCH ()
{
    yield return new WaitForSeconds(0.5f);
    GetComponent<Rigidbody>().velocity = new Vector3(0, 0, 0);
}
于 2020-11-02T17:26:43.117 回答