Moving an object with vector3.MoveTowards

后端 未结 2 1670
挽巷
挽巷 2021-01-22 06:37

I am working on a game app with Unity. I have an issue when it comes to move a GameObject .

In my game, when the player swipes up with his device, the GameObject moves f

相关标签:
2条回答
  • 2021-01-22 07:06

    Here is how to use MoveTowards:

    void Update()
    {
        float step = speed * Time.deltaTime;
        transform.position = Vector3.MoveTowards(transform.position, PositionB, step);
    }
    

    LearnMore

    0 讨论(0)
  • 2021-01-22 07:08

    Vector3.MoveTowards takes the current position, the target position, and the step, but it seems like your first argument here is origin of the move, rather than current position. Normally you'd do this something like this, in your Update():

    transform.localPosition = Vector3.MoveTowards (transform.localPosition, PositionB, Time.deltaTime * speed);

    with the current position as the first argument.

    0 讨论(0)
提交回复
热议问题