- Home /
The question is answered, right answer was accepted
Rotation Angle across 0, opposing angle?
Hi I ve got a tower that rotates towards a target. I am using a helper object that looks at the target and then i am lerping from the current rotation to the helper objects rotation. the problem that i have atm is when the target passes infront of my tower it rotates all the way around, so instead of going from +5 to -5 degrees (i want it to rotate 10 degrees) it rotates 350 degrees. what am i missing? here is a bit of my aiming code:
Aiming(){
rotateHelper.transform.LookAt(target.transform.position);
rotateHelper.transform.localEulerAngles.x=0;
rotateHelper.transform.localEulerAngles.z=0;
endBaseAngle = rotateHelper.transform.localEulerAngles.y;
baseAngleDifference = Mathf.Abs(endBaseAngle-startBaseAngle);
if(baseAngleDifference>180){ // this bit is trying to change that angle but fails
baseAngleDifference = Mathf.Abs(endBaseAngle-startBaseAngle);
print("new base angleDiff = "+baseAngleDifference);
}
changeBase=true;
}
// gets called in update if changeBase is true..
function ChangeBase(){ //small rotations should take less time. compare start and emd angle before we go here and somehow multiply rotate speed
soundPlayer.PlaySound(soundBaseRotate,0.0);
weaponBase.transform.localEulerAngles.y = Mathf.Lerp(startBaseAngle,endBaseAngle,rotateTimer*baseRotateSpeed);
weaponBase.transform.localEulerAngles.x=0;
weaponBase.transform.localEulerAngles.z=0;
rotateTimer+=Time.deltaTime/baseAngleDifference; //dividing by difference of old and new angle so a small angle is reached faster
if(rotateTimer*baseRotateSpeed>=1.0){
rotateTimer=0.0;
changeBase=false;
}
}
Also if you have any other improvements to my coding style it would be appreciated since i am still quite new to programming. I am sure there is an easier way to do this.
thanks in advance
Answer by aldonaletto · Jun 10, 2012 at 10:05 PM
localEulerAngles (as well as eulerAngles) return an angle usually between 0 and 360: when the angle falls below 0, it usually becomes 359, not -1 as you could expect.
To avoid this problem (among several others...), it's better to do it with quaternions:
var speed: float = 10; // speed in degrees per second
function Update(){ // NOTE: rotateHelper isn't necessary to this code rotateHelper.transform.LookAt(target.transform); // find the target direction relative to weaponBase: var dir = target.transform.position - weaponBase.transform.position; // kill any height difference to make the direction strictly horizontal: dir.y = 0; // calculate the desired quaternion: var rot = Quaternion.LookRotation(dir); // weaponBase rotates to target at constant speed: weaponBase.transform.rotation = Quaternion.RotateTowards(weaponBase.transform.rotation, rot, speed * Time.deltaTime); } This code will make weaponBase rotate horizontally at speed degrees per second in the target direction, independently of rotateHelper (remove it if it's unnecessary).
thanks for the quick reply Aldo. Rotate towards is what i forgot about, i think this will work perfectly. Also for ppl having a similar issue I ve read up on vectors here: http://unity3d.com/support/documentation/$$anonymous$$anual/DirectionDistanceFromOneObjectToAnother.html The last time i ve done maths was in school and tbh ive forgotten loads so the manual provides a great starting point to read up on things.
Follow this Question
Related Questions
Rotation circle to stop at a certain angle 1 Answer
Angle to Rotation 2 Answers
0-360 Y degree from Vector3.Angle 1 Answer