# How do I get one object to point toward another using rigidbody physics?

I did some searching and only found answers that involved transform.rotate and such. I’m looking to do the same thing but while maintaining the physics system. I imagine this will involve using AddRelativeTorque.

The transformation takes place in three dimensions and needs to take place over time. I’ve tried a bunch of different angles on this one, any help is much appreciated.

Found this:

But it only works with 1 dimension. Need help implementing for 3 dimensions

@Seth’s answer uses Rigidbody.MoveRotation(). If MoveRotation() is good enough for what your are trying to achieve, then you can just do:

``````#pragma strict

var target : Transform;
var speed = 5.0;

function FixedUpdate() {
var qTo = Quaternion.LookRotation(target.position - transform.position);
qTo = Quaternion.Slerp(transform.rotation, qTo, speed * Time.deltaTime);
rigidbody.MoveRotation(qTo);
}
``````

Note it would be much more complicated to use “real” physics and adding torque to solve this problem. What needs to be done will depend on what you are trying to achieve here.

so are you doign this as 1 rotation or 3? (a realistic turret for example would use 3 seperate axes to move to it, a spaceship with RCS thrusters would find the axis of rotation and apply a single rotation along that axis)

Spaceship

``````StartVector = AIM_AT.transform.forward;

EndVector = (AIM_AT.transform.position - POINTING_OBJECT.transform.position).normalized;

AXIS_TO_ROTATE_ON = vector3.cross(EndVector,StartVector);

//angle is FYI you may not actually require this bit of information
ANGLE = Mathf.Atan2(Vector3.Magnitude(AXIS), Vector3.Dot(Startvector, EndVector));
``````

so now your just going to rotate along

I assume you know how to do the calcuations for 3 axis (just ignore an axis each time and find each 3)

If your issue is how do i start and stop the rotation.

well you simply apply torque along the axis which is what we computed above
so

so now we have it rotating properly on that axis

now comes another decision

do you want a constant turn rate with a sudden stop or a kind of curve where you approach it and gradually settle on your target.

if you want a sudden stop just do a

//bear in mind doing vector3 == vector3 isn’t actually dead accurate, answers taht are close but not exact work, FOR FLOATING POINT NUBMERS and vector3 is 3 floating point numbers floatinga == floatingb acutally means is floatinga APPROXIMATELY floatingb
this actually is ok though, you cant hit it aiming perfectly in a frame nessacarily but if your really close to aiming at it it’ll trigger the stop.

if(transform.forward == EndVector)
{
//apply a counter torque so get the current torque and apply that exact same amount of force on the -axis
}

if you want to gradually approach you can use a lerp/slerp

you can also use the angle caclatuion above or vector3.angle (might not work as well)
to basically take the angle and be like as the angle approaches 0 speed used in addrelativetorque appraoches 0.

anyways there are some ideas.