1
\$\begingroup\$

How do I move and collide my object (position given by touch or click), by Using rigidBody2D.MovePosition i can only teleport my character, and by using velocity, my character keeps moving, I want him to stop at the position given

Ilustrations:

enter image description here

enter image description here

\$\endgroup\$
1
\$\begingroup\$

This is called arrival. It's a steering behavior. The basic concept is:

  1. Apply an acceleration towards the target position (alternatively, you can simply set the velocity if you're not using acceleration)
  2. When within a defined range of the target object, start applying acceleration away from the object, this acceleration is scaled based on the distance to the object and the current velocity. (If not using acceleration, skip to step 3)
  3. When within a "close enough" range, and below an acceptable velocity, stop all motion on the object. Optionally, you can also snap the position of the object to the target position.

You can find a good explanation of the implementation here.

\$\endgroup\$
0
\$\begingroup\$

You'll have to change the velocity after your object reaches the correct position. You could store the target position in your script, and you could check the distance to the target during Update. Once the object has arrived, set the velocity back to zero. Arrival should be determined by some non-zero minimum distance to the target, because the object is unlikely to land exactly on the target position. If you like, you could then move the object perfectly to the target position as one final step.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.