- Home /
apply a modified rotation based on another objects' 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.
Answer by MountDoomTeam · Oct 05, 2012 at 03:49 PM
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.
http://answers.unity3d.com/questions/324886/tree-branch-help-cumulation-of-quaternion-rotation.html
see, that's what does my head in =] I found that while searching, but I cannot get my head around multiplying 2 angles gives a result which is actually adding 2 angles. But I appreciate the response , and shall check with this now :
var turnAngle : Quaternion = thePlayer.rotation * Quaternion.Euler( 0.0, 180.0, 0.0 );
Well, it still does my head in, but it really is that simple, wow. When I changed the relevant parts to the below, it worked as expected. $$anonymous$$any thanks =]
var turnAngle : Quaternion = thePlayer.rotation * Quaternion.Euler( 0.0, 180.0, 0.0 );
transform.rotation = Quaternion.Lerp( transform.rotation, turnAngle, turnSpeed * Time.deltaTime );
i was on that one for a loooong scratchy head time .... so i didnt even stop to wonder if i knew how it works when i found the solution! it''s just some 3d trickery. $$anonymous$$any of the greatest $$anonymous$$ds of our century have struggled with the question of 3d rotations. it's something to do with matrixes of sin and cos theta. :)