# apply a modified rotation based on another objects&#39; rotation

I’m completely lost on quaternion math and applying rotations so here is my question.

How can I get the rotation of object 1, calculate from that a target angle, and then apply that rotation to object 2 ?

here’s what I came up with so far :

``````public var thePlayer : Transform;
var speed : float = 8.0;
var turnSpeed : float = 0.5;
var startRotY : float = 25.0;
enum allStates { Idle, Spawning, FlyingForward, TurningToBehind, WaitingToRespawn }
var currentState : allStates;

function Spawning()
{
// position at relation to player + randomness
transform.position = thePlayer.position + ( thePlayer.forward * -10.0 ) + ( thePlayer.right * -20.0 );

// rotation at angle to player + randomness
var customRotation : Quaternion = Quaternion.Euler( thePlayer.rotation.x, RoundAngle( thePlayer.rotation.y + startRotY ), thePlayer.rotation.z );

//this.transform.rotation = thePlayer.rotation;
transform.rotation = customRotation;

// Switch state
currentState = allStates.FlyingForward;
}

function TurningToBehind()
{
// rotate to 180' to player
var turnAngle : Quaternion = Quaternion.Euler( thePlayer.rotation.x,
RoundAngle( thePlayer.rotation.y - 180.0 ),
thePlayer.rotation.z );

transform.rotation = Quaternion.Lerp( transform.rotation, turnAngle, turnSpeed * Time.deltaTime );

// keep turning and flying
transform.position += transform.forward * speed * Time.deltaTime;

//check if position is behind players local Z
// using Dot.Product here =]

// if behind player , change state
//currentState = allStates.WaitingToRespawn;
}

function RoundAngle( theAngle : float ) : float
{
if ( theAngle > 360 )
{
theAngle -= 360;
}
else if ( theAngle < -360 )
{
theAngle += 360;
}

return theAngle;
}
``````

When I rotate the Player, the Enemy does not notice this or use anything but Euler(0,0,0) while using thePlayer.rotation in any rotation calculation.

Obj1.transform.rotation = Obj2.transform.rotation * rot;
where rot is a vector3 declared as the eulerAngles of a Quaternion.

just get both objects’transform.rotation, and set one to be equals the other if you want to make them the same, else to add them, store the first one’s transform rotation as a variable, and set set the object to a new rotation by multiplying transform rotation of object 1 with that of object 2.