- Home /
Use rigidbody2D.velocity instead of MoveTowards
Hello everybody!
I am trying to make an gameObject 1 to go from one point towards another. Together with this I want an another object 2 attached to the first to rotate following the direction of the velocity of 1. Right now to move the 1 I am using Vector2.MoveTowards, like this:
rb.position = Vector2.MoveTowards(transform.position, wayPoints[randomSpot].position, speed * Time.deltaTime);
However, by doing this I have seen that the velocity of the rigidbody is always (0, 0). Is there a way to use velocity to perform this movement? Any suggestion would be appreciated!
Answer by $$anonymous$$ · Jan 30, 2019 at 10:11 PM
Try this
var position = Vector2.MoveTowards(transform.position, wayPoints[randomSpot].position, speed * Time.deltaTime);
rb.MovePosition(position);
Sadly it has the same result, rb.velocity is still (0, 0). I was wondering if there is a way from $$anonymous$$ovePosition to derive the mean velocity for instance?
Answer by myzzie · Jan 31, 2019 at 01:48 PM
Sometimes you have to calculate velocity yourself.
var velocity = (currentPosition - lastPosition) / Time.deltaTime;
I wanted to try something similar, however I am not sure how to store and update the lastPosition for each frame :/
Your answer
Follow this Question
Related Questions
Player is not moving 0 Answers
2d kinematic projectile velocity problem 0 Answers
How to not get velocity by the other objects?,How to not get force by other gameobjects? 0 Answers
Shooting 2D - rigidbody2D.velocity not working 1 Answer
Maintaining the trajectory of a ball when manipulating its velocity in a Breakout/Arkanoid clone 1 Answer