Turrets resetting to forward position - 3d Space

Hi all,

Been struggling with this one for a while and was hoping someone may have an answer. Using a Turret Rotation script that is working great for my 3d Space Project. I have several turrets on the player ship and when the player selects a target each turret turns towards the target, using a raycast to establish a line of sight. Here is the issue, when the line of sight is broken I would like the turret to return to its 0 position and 0 rotation so that its facing forward on the ship. With this being a 3d space game and the ship being all over the place this has turned out to be quite a challenge with my turret models ending up inside the ship or other rather awkward positions. :(. Ive tried using Quaternion.Idenity and other tranform.position, and transform.rotation ways but none seem to help.

Here is the turret rotation code I have;

void Update ()
{
    //If theres a target lets rotate the gun to the target
    if (target != null)
    {
        RaycastHit hit;          
        if (Physics.Raycast(this.transform.position, target.position - this.transform.position, out hit) && hit.transform.tag != "Player")
        {  
            float distanceToPlane = Vector3.Dot(trans.up, target.position - trans.position);
            Vector3 planePoint = target.position - trans.up * distanceToPlane;

            qTurret = Quaternion.LookRotation(planePoint - trans.position, transform.up);
            transform.rotation = Quaternion.RotateTowards(transform.rotation, qTurret, turretDegreesPerSecond * Time.deltaTime);

            Vector3 v3 = new Vector3(0.0f, distanceToPlane, (planePoint - transform.position).magnitude);
            qGun = Quaternion.LookRotation(v3);

            if (Quaternion.Angle(qGunStart, qGun) <= maxGunAngle)
            {
                gun.localRotation = Quaternion.RotateTowards(gun.localRotation, qGun, gunDegreesPerSecond * Time.deltaTime);
            }
            else
            {
                //If the gun goes beyond max angle
                gun.localRotation = Quaternion.RotateTowards(gun.localRotation, qGunStart, gunDegreesPerSecond * Time.deltaTime);
            }
            lineofsight = true;
        }
        else
        {
            //If raycast is broken
            lineofsight = false;
            gun.localRotation = Quaternion.RotateTowards(gun.localRotation, qGunStart, gunDegreesPerSecond * Time.deltaTime);
        }
    }
    else
    {
        gun.localRotation = Quaternion.RotateTowards(gun.localRotation, qGunStart, gunDegreesPerSecond * Time.deltaTime);
        lineofsight = false;
    }
}

It shouldn’t be so hard. If you assign a localRotation of Quaternion.Identity, this will point the gun back to its “default” orientation relative to its parent (which I assume is the ship).

If you want it to go back to that orientation gradually, you can use Quaternion.Lerp or Quaternion.RotateTowards.

If assigning Quaternion.Identity does not return the gun to its proper orientation relative to the ship, then I think you need to reexamine your object hierarchy.

whats qGunStart set to? Quaternion Identity? transform.rotation?

its likely that you want qGunStart set to transform.localRotation. and have it set in Awake()

edit: I just noticed that your code only rotates the gun when it loses track
the turret (which i assume is the transform) isn’t getting rotated as it prolly should like on line 14

also if the hardpoint the turret is attached to (a ship for example) can rotate, you most likely want to set localRotation instead

Yes I am thinking this shouldn’t be so difficult but its been giving me issues. I probably should have explained the Hierarchy as well in my original post because it is probably the issue, though I cant break this setup.

PlayerShip: has the actual position and rotation of the player as they are moving.

  • WeaponSlot1: empty game object that is pre-placed on the ship model
    – Turret Weapon: Has the base of the turret, holds the above script and rotates in a circle
    — Turret Barrel: Is the “gun” referred to in the above script…this moves up and down using the MaxGunAngle

The above are referenced as Parent on the top moving downwards as children of each in this order.
The qGunStart was my original attempt to get this working, right now its set on Start() as gun.transform.localRotation but it was what I also tried to use with Quaternion.Identity etc.

Though while I am typing this I am realizing I might of been only trying to set the gun barrel to Quaternion. Identity and not the base when I should probably do both…Ill have to dig in deeper tomorrow after work.

1 Like