- Home /
Return to original position using eulerAngles & FromToRotation
I have a model of a Mech, the problem is that the original models axis are incorrect so have to compensate when tracking the target with -
rotation *= Quaternion.FromToRotation(Vector3.up, Vector3.right);
So when I use this script to return the turret back to match the lower half of the Mech with the above line in, it never yields because it mess the eulerAngles I presume, without the line in it rotates and yields but with the Mechs head out of orientation, so how would I correct mechTurret.eulerAngles to take into account the Quaternion.FromToRotation.
If I use an empty game object to correct it works but I loose the animations on the turret, so thats not an option, any help would be great, thanks.
function rotateTurret(){
while((mechTurret.eulerAngles - transform.eulerAngles).sqrMagnitude > 0.01f){
// Rotate the Mech Turret to face the direction of the bottom half of the Mech
var lookPos = transform.forward;
var rotation = Quaternion.LookRotation(lookPos);
rotation *= Quaternion.FromToRotation(Vector3.up, Vector3.right);
mechTurret.rotation = Quaternion.Slerp(mechTurret.rotation, rotation, Time.deltaTime * 1);
yield;
}
}
just purely FYI. it would be really advisable to not use the variable name "rotation" as it is a property
could you make it something more descriptive like "desiredTarget" or the like?
I don't totally understand what you're doing, but could the problem relate to:
you are using global up to rotate it around, rather than it's local up?
Just also, would it be tremendously easier to just use "RotateAround" here?
Fattie, thanks for the input, what I have is a function for the Turret of a $$anonymous$$ech walker to track the player when he gets within a detection range -
function turretTrackPlayer(){
// Rotate turret to face the Player
var lookPos = player.position - mechTurret.position;
var rotation = Quaternion.LookRotation(lookPos);
// Next 2 line corrects the original axis being incorrect
rotation *= Quaternion.FromToRotation(Vector3.up, Vector3.right);
rotation *= Quaternion.Euler(90, 0, 0);
mechTurret.rotation = Quaternion.Slerp(mechTurret.rotation, rotation, Time.deltaTime * turnToLookSpeed);
}
Then when the player goes out of the detection range I wanted the turret to return back to match the rotation of the bottom half, the legs of the $$anonymous$$ech walker, this is where I'm stuck, because of the two correction lines of code it throws the eulerAngles off, this is the only way I thought I knew how to do it with some help from whydoidoit, this is the script to return the turret back into line with the bottom half, the legs -
function rotateTurret(){
while((mechTurret.eulerAngles - transform.eulerAngles).sqr$$anonymous$$agnitude > 0.01f){
// Rotate the $$anonymous$$ech Turret to face the direction of the bottom half of the $$anonymous$$ech
var lookPos = transform.forward;
var rotation = Quaternion.LookRotation(lookPos);
rotation *= Quaternion.FromToRotation(Vector3.up, Vector3.right);
rotation *= Quaternion.Euler(90, 0, 0);
mechTurret.rotation = Quaternion.Slerp(mechTurret.rotation, rotation, Time.deltaTime * 1);
yield;
}
}
I maybe using the wrong formula, to some this may be a stupid error and know of an easy solution but it's got me, my head hurts .. :)
Your answer
Follow this Question
Related Questions
Question regarding - Quaternion.FromToRotation, Walking around a cube 0 Answers
Head and body rotate threshold 1 Answer
How to get inspector rotation values ? 2 Answers
Rigidbody2D constraints checked for x and y but still moves when character jumps on it from distance 0 Answers
Player Rotation while walking on wall 0 Answers