- Home /
moving an object from point to point using force
simply i want to make the same task but with a rigidbody:
transform.position = Vector3.MoveTowards (transform.position, currentTarget.position, speed * Time.deltaTime);
i want the object to stop as soon as it hits the target. also im using a collider in between the object and the starting point so i want the target to be able to collide with it, thats why i want to use add force. is there any other way to do it?
Answer by Hellium · Nov 28, 2017 at 10:53 PM
You don't need AddForce
, you can use MovePosition
: https://docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html
void FixedUpdate() {
Vector3 position = Vector3.MoveTowards (rigidbodyComponent.position, currentTarget.position, speed * Time.fixedDeltaTime);
rigidbodyComponent.MovePosition(position );
}
i tried it, it kind of worked but i feel like the object is teleporting to the next point not really moves to it. like when the frame rate dropes, it sometimes goes through the collider without colliding with it, especially when the speed is high
The doc says :
If Rigidbody interpolation is enabled on the Rigidbody, calling Rigidbody.$$anonymous$$ovePosition results in a smooth transition between the two positions in any intermediate frames rendered. This should be used if you want to continuously move a rigidbody in each FixedUpdate.
Your answer
Follow this Question
Related Questions
Knock down moving target ridigbody from collision. 1 Answer
How to limit only the Rigidbody.velocity from player input? 1 Answer
Problem with Rigidbody control script! (collision with perpendicular walls problem) 1 Answer
Added force to rigid body, but wont move same speed as horizontal speed... if rotating 1 Answer