- Home /
How can I rotate a tank's turret back to the starting turret position?
I have a tank in my game and I'm trying to figure out how to rotate the turret back to the position it started after there are no enemies in sight.
public Quaternion TurretRest;
public void Start()
{
TurretRest = this.gameObject.transform.FindChild ("Turret").transform.localRotation;
//Should be (0,0,0) when looking at the localrotation
}
Quaternion Rot3 = Quaternion.LookRotation(TurretRest);
//Cannot use a quaternion (TurretRest) after the look rotation so what would I put there instead?
Turret.transform.rotation = Quaternion.Slerp (Turret.transform.rotation, Rot3, Time.deltaTime);
Answer by Loui_Studios · Jan 27, 2017 at 04:38 PM
I think the problem is that TurretRest
is set to the turret's default local rotation, but then you're setting it's world rotation to it's local rotation.
Try replacing:
Turret.transform.rotation = Quaternion.Slerp (Turret.transform.rotation, Rot3, Time.deltaTime);
with:
Turret.transform.localRotation = Quaternion.Slerp (Turret.transform.localRotation, Rot3, Time.deltaTime);
and let me know if it works.
Thanks it works! Ill update the code above for others to use
public Quaternion TurretRest;
public void Start()
{
TurretRest = this.gameObject.transform.FindChild ("Turret").transform.localRotation;
}
public IEnumerator TurretReset()
{
Turret.transform.localRotation = Quaternion.Slerp (Turret.transform.localRotation, TurretRest, Time.deltaTime);
}
This is the basics of this code
Answer by IgorAherne · Jan 27, 2017 at 12:10 AM
Just declare Quaternion myInitialRotation;
In Start assign gameObject.transform.rotation to it.
In update, when no enemies are visible, rotate towards this "cached" value