- Home /
Relative Rotation
I am trying to create a tank enemy in my game, and I want it to have a linear turret rotation speed, so I´m trying to calculate the relative rotation between the tank and it´s turret so I can make it point to it´s target ( the player )
below is the code I have:
void Update () {
// this is just a sample code to change the tank´s rotation so I can test the code
float s = Time.deltaTime * 10f;
this.transform.position += this.transform.forward * s; // this would make it move forward
this.transform.forward = Quaternion.AngleAxis( 45*Time.deltaTime, Vector3.up) * this.transform.forward; // this makes it rotate a little
Camera cam = Camera.mainCamera;
Vector3 dif = cam.transform.position - this.transform.position; // this will save a vector pointing to player
dif.Normalize(); // this is so that my vector would have the some length as the forward vector
dif = dif - this.transform.forward; // this is what I thought would work
float ay = Mathf.Atan2( dif.x, dif.z ) * 180f / Mathf.PI; // this code works fine if the tank is stopped
_turret.localEulerAngles = new Vector3( -90, ay, 0 );
}
suggest unityGE$$anonymous$$S.com for good explanation of Quaternions
Answer by sparkzbarca · Nov 10, 2012 at 08:34 AM
the turret should be seperate the hull and just attached.
Rotate the turret towards the target by using
transform.RotateTowards(current, target);
the hull shouldnt factor into it.
Or are you trying to make the hull point towards the enemy in which case
take the transform of the hull and rotate towards the enemy
or your trying to rotate the hull the match the turrets vector which is again
rotatetowards(current,turretsVector)