Move a Rigidbody by a specific certain amount in a smooth motion?
So I'm basically trying to get my character to push a block (rigidbody), however when I set it to go at a velocity and have it stop after a certain amount of time (via the WaitForSeconds() function), it doesn't move the same amount each time. Using Vector3.Lerp with its position and smoothing it with Time.deltaTime also gives the same problem (not to mention, it has the block moving through other collisions when done this way).
Basically think of block-pushing in Zelda. How do I get it to move the same amount each time and move so smoothly too?
Answer by Jakeiiii · Sep 10, 2015 at 04:10 PM
Okay so I've managed to get a solution. Basically, I get the position that the box started in (origin) and then the point I want it to go to (origin+distance). I make the box move at a fixed velocity, with no drag/friction whatsoever. When the box moves, it isn't kinematic, and when it's stationary, it is kinematic. I then check the distance that it has traveled from the origin and the distance between the box and its destination. I make two boundaries out of the two distances and when it is a small distance from the origin or has gone too far past it, then it will stop moving and be positioned at the destination. I've also used Raycasts to check if the box won't be able to make the entire journey, so if not then it cannot be pushed/pulled. There are other complexities that I have omitted for the sake of only saying the important parts but that is the general idea.
Thanks to Peaj for your ideas. :) @Peaj
Answer by Peaj · Sep 09, 2015 at 12:57 PM
You could use Rigidbody.MovePosition together with Vector3.MoveTowards. MoveTowards does the same thing as lerp except it wont overshoot your target. Rigidbody.MovePosition will make sure that your block collides on its way.
I assume you mean to use RB.$$anonymous$$ovePosition(Vector3.$$anonymous$$oveTowards(xxx))? This still has the box going through things unfortunately, but at least it's moving at specifics amounts.
Yeah thats right. I guess you would have to use OnCollisionEnter to check wether a collision is happening. If it coullides you have to stop moving it. The collision detection should work just fine like that but if you go on moving the object by script it can't react in a proper physical way.
But then it won't react until the box has gone through something. It's ridiculous how this is something that is actually difficult to do; isn't there any kind of rigidbody movement function that goes between two Vector3s and has adjustable speed? :P
If you use OnCillisionEnter it will react exactly the moment it collides with the object. The same moment the physic engine would react to the object. It does not go all the way into the object.
Your answer
Follow this Question
Related Questions
Jump is higher then normal 0 Answers
Move NPC on triggerEnter 0 Answers
Player Rotation and movement 0 Answers
Stop moving gameObject and push it back 0 Answers
Change direction of rigidbody when force is no longer being applied 2 Answers