- Home /
Rigidbody + Vector3
Is there a way to make a rigidbody go directly to a Position? For Example if my rigidbody is on 0, 0, 0 so it wents straight to lets say 10, 20, 30. I dont want to use Transform.translate, because when i use that the rigidbody wont collide with other rigidbodys.
Answer by DavidDebnar · Jul 08, 2013 at 07:40 PM
Use Rigidbody.MovePosition in FixedUpdate().
Edit:
Example 1:
In this example, the rigidbody goes straight to the Destination (10,2,10) with speed of 5 m/s (defined by 'Speed' var). The MinDist variable is there to tell it when to stop, to prevent weird behaviour.
var Destination : Vector3 = Vector3(10,2,10);
var Speed : float = 5;
var MinDist : float = 0.5;
function FixedUpdate () {
if((Destination - rigidbody.position).sqrMagnitude > MinDist*MinDist)
rigidbody.MovePosition(rigidbody.position + (Destination-rigidbody.position).normalized*Speed*Time.deltaTime); //Moves the rigidbody to Vector3(10,2,10)
}
Example2:
In this example, the rigidbody goes in the Direction (1,0.5,1) with speed of 5 m/s (again, defined by 'Speed').
var Direction : Vector3 = Vector3(1,0.5,1);
var Speed : float = 5;
function FixedUpdate () {
rigidbody.MovePosition(rigidbody.position + Direction.normalized*Speed*Time.deltaTime);
}
--David--
But how do i use this to let a rigidbody go directly to the Vector3?
Answer by kromenak · Jul 08, 2013 at 07:42 PM
You can try setting Rigidbody.position directly, which is similar to Transform.position, except the position is only applied after the physics steps. I'm not sure whether this means that it will be affected by collision volumes though (you might still get instances where you are stuck halfway in a collider). Still, might be worth a shot.
Another option when you want to move something directly to a position is to use Physics.CheckSphere or Physics.CheckCapsule (or SphereCast/Raycast if more appropriate). Before performing the move, you can check the desired move location (10, 20, 30) with a check/cast function to see whether there are any colliders at that spot. This is best if the Rigidbody you are trying to move is relatively simple (obviously if your Rigidbody is just a Sphere Collider, then the CheckSphere will be perfect), but it can work for more complicated Rigidbodies if you are careful and allow enough buffer.
As far as I know, Rigidbodies will only really perform expected collision detection if you perform continuous movement using either a force or maybe MovePosition function. When you set the position directly, you'll need to perform your own checks to ensure that the spot you are moving to is not occupied by some other collider.
Answer by zenforhire · Jul 08, 2013 at 08:20 PM
My situation was steering a ship to specific waypoint/position. The physics engine determines the incremental position of the rigidbody. I don't calculate the positions. I wrote a ManeuverComputer to control the rudder and engine to steer it to the position. When the ship's world vector was closing in on intersection with waypoint, I straighten out the controls.