- Home /
Use force to move to position of object
Hello there.
What I'm looking to achieve, is to make a rigidbody follow another gameobjects position, and I want the rigidbody to travel faster if the distance between the rigidbody and the gameobject is less.
I tried using a spring joint, but I realized that I need it to work the opposite way as a spring joint, so instead it would move faster the closer to the connected body the rigidbody is.
I've experimented with configurable joint, but I can't seem to understand how to do what I want with that method.
I've been looking into adding constant force directly in the direction of the object:
Vector3 Direction = (GameobjectA.transform.position - gameObject.transform.position).normalized; rigidbody.constantForce.force = Direction;
I've been trying to modify the rigidbody.velocity directly, but I realized that I should not do that, as I need my physics objects to work correctly
So at the moment I'm wondering what method I should try and use. I'm not very experienced with maths and physics, and I've currently run out of options to try. I think adding force in the direction of the gameobject would be the way to go, but with what I have so far the rigidbody sort of bounces forth and back trying to reach the target position, and I'm not sure how to fix that without modifying the velocity directly or slow the rigidbody down when its closer, as I need the opposite to happen.
Note that my gameobject is empty, and my rigidbody is simply trying to reach its position with force.
Any help or ideas would be greatly appreciated, thanks in advance.
Answer by robertbu · Oct 06, 2013 at 08:34 PM
I'm not sure what you are going for here. You can modify the velocity based on distance. Here is a bit of code that, when the target is in range, chases the target. The closer it is, the faster it goes:
var target : Transform;
var maxDist = 5.0;
var maxVelocity = 8.0;
function FixedUpdate () {
var dist = Vector3.Distance(target.position, transform.position);
if (dist < maxDist) {
rigidbody.velocity = (target.transform.position - transform.position).normalized * (maxVelocity * (1.0 - dist / maxDist));
}
else {
rigidbody.velocity = Vector3.zero;
}
}
Hello thanks for your answer! The thing is that I need my rigidbody to work physically correct with the physics. That's why I'm trying to move my rigidbody with forces, and if I modify the rigidbody.velocity directly, wouldn't this mean that the rigidbody will no longer collide properly etc?
I'm wondering if ins$$anonymous$$d of setting the velocity to zero when it's closer, if I ins$$anonymous$$d should use additional forces to bring it back in position. When dealing with forces like in my example above, as the rigidbody chases the gameobject, the problem occours when the rigidbody reaches target position, it naturally passes through the gameobject and applies forces to the rigidbody to try and reach its position again. This causes a sort of bouncing effect. So I'm wondering if there is a way to stop the rigidbody without modifying the velocity directly, and ins$$anonymous$$d stop the gameobject with forces.
I just thought of a solution to try out. What if I check the direction of the rigidbody and the distance, and if it's moving away from the gameobject, then apply forces in the right direction based on the velocity of the rigidbody, and the distance from the gameobject. So basically I would try and hold the rigidbody in one spot by putting forces from all directions. Note that I'm also using gravity on my rigidbody.
I will try and implement this today and I'll see how it goes, but mostly I'm wondering if I'm even going in the right direction here, and if what I've questioned with modifying the velocity of the rigidbody directly is true
Thanks again, and sorry for the poor explanation
Hello thanks for your answer! The thing is that I need my rigidbody to work physically correct with the physics. That's why I'm trying to move my rigidbody with forces, and if I modify the rigidbody.velocity directly, wouldn't this mean that the rigidbody will no longer collide properly etc?
No. The physics will still work if you modify the velocity directly.
I'm wondering if ins$$anonymous$$d of setting the velocity to zero when it's closer, if I ins$$anonymous$$d should use additional forces to bring it back in position.
You could use additional force, but to make it work, you would need significant drag. AddForce() does not replace the current velocity, it just modifies it. Think space ship. If it is traveling in one direction and you apply a side force, the ship will not turn 90 degrees, but ins$$anonymous$$d angle off. This can make it difficult to aim your object unless you have enough drag to decay the existing velocity and enough new velocity to set the new direction.
it naturally passes through the gameobject and applies forces to the rigidbody to try and reach its position again. This causes a sort of bouncing effect. So I'm wondering if there is a way to stop the rigidbody without modifying the velocity directly, and ins$$anonymous$$d stop the gameobject with forces.
Again, modifying the velocity does not disable/hurt physics. You could create a counter force to stop the object, but that is equivalent to setting the velocity to zero.
Thank you so much for taking your time :)
I must say I'm a bit confused regarding the direct velocity changes. The docs says that in most cases you should not modify the velocity directly as it can result in unrealistic behaviour
http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-velocity.html
Does this mean that the docs are wrong, or should I use forces if I want 100% realistic behaviour? I've been experimenting and testing for a few hours now and I'm getting a lot closer to getting it to work with forces, although it would be a lot easier to just change the velocity directly if that works 100% realistically as well. Thanks again
The Doc is right to a point. But:
There are lots of games that have unrealistic physics that are played all the time.
It is possible to create unrealistic physics using AddForce(). For example, if you set the drag in the Rigidbody high, and apply force every frame, then the effect will be very similar to setting the velocity.
It is possible to make velocity more realistic through a bit of vector math.
What I'm saying is don't use AddForce()for this problem just because you think it is better by some mythical standard mentioned in the manual. Use it if it gives you something...feels better in terms of motion for example.
I don't know the nature of your game nor the game mechanic you are ai$$anonymous$$g at, so it is difficult to advise about AddForce(). I think with AddForce() you will struggle to have the following object hit the target. Upping the drag will help significantly.