Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8752143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:10:44+00:00 2026-06-13T13:10:44+00:00

at first, here the video of the problem: link to video , here the

  • 0

at first, here the video of the problem:
link to video, here the package
and here is a screenshot of the hierachey:
enter image description here
the GUN is the parent with the script(empty gameobject) Suspension gets dropped to the towerRotateObj and Gun gets dropped on the turretRotateObj. Suspension and Gun are also empty gameobjects. they are just some kind of group objects
and here the code:

public class WeaponMover : MonoBehaviour
{
    public Transform target;
    public GameObject turretRotateObj;
    public GameObject towerRotateObj;

    public float maxTowerRotationSpeed = 360.0f;
    public float maxTurretRotationSpeed = 360.0f;

    public float smoothFactorTower = 0.125f;
    public float smoothFactorTurret = 0.125f;

    public float maxTowerRotation = 130.0f;
    public float maxTurretRotation = 50.0f;

    private Vector3 m_newRotation;
    private Vector3 m_angles;
    private float m_minTowerAngle;
    private float m_maxTowerAngle;
    private float m_minTurretAngle;
    private float m_maxTurretAngle;
    private float m_velTower;
    private float m_velTurret;

    private bool m_isTransNecTower = false;
    private bool m_isTransNecTurret = false;

    // initialization
    void Start()
    {
       m_newRotation = Vector3.zero;
       m_angles = Vector3.zero;

       m_maxTowerAngle = towerRotateObj.transform.eulerAngles.y + maxTowerRotation/2;
       m_minTowerAngle = towerRotateObj.transform.eulerAngles.y - maxTowerRotation/2;

       m_maxTurretAngle = turretRotateObj.transform.eulerAngles.z + maxTurretRotation/2;
       m_minTurretAngle = turretRotateObj.transform.eulerAngles.z - maxTurretRotation/2;

       // check if rotation happens between 0/360
       // tower
       if(m_minTowerAngle <= 0.0f)
         m_minTowerAngle += 360.0f;

       if(m_maxTowerAngle >= 360.0f)
         m_maxTowerAngle -= 360.0f;

       if(m_minTowerAngle > m_maxTowerAngle)
         m_isTransNecTower = true;

       // turret
       if(m_minTurretAngle <= 0.0f)
         m_minTurretAngle += 360.0f;

       if(m_maxTurretAngle >= 360.0f)
         m_maxTurretAngle -= 360.0f;

       if(m_minTurretAngle > m_maxTurretAngle)
         m_isTransNecTurret = true;
    }

    void Update()
    {
       m_newRotation = Quaternion.LookRotation(target.position - towerRotateObj.transform.position).eulerAngles;
       m_angles = towerRotateObj.transform.rotation.eulerAngles;
       towerRotateObj.transform.rotation = Quaternion.Euler(m_angles.x,
         ClampAngle(Mathf.SmoothDampAngle(m_angles.y, 
          m_newRotation.y - 90.0f, 
          ref m_velTower, 
          smoothFactorTower, 
          maxTowerRotationSpeed), m_minTowerAngle, m_maxTowerAngle, m_isTransNecTower),
         m_angles.z);

       m_newRotation = Quaternion.LookRotation(target.position - turretRotateObj.transform.position).eulerAngles;
       m_angles = turretRotateObj.transform.rotation.eulerAngles;
       turretRotateObj.transform.rotation = Quaternion.Euler(m_angles.x,
         m_angles.y,
         ClampAngle(Mathf.SmoothDampAngle(m_angles.z, 
          -m_newRotation.x, 
          ref m_velTurret,
          smoothFactorTurret, 
          maxTurretRotationSpeed), m_minTurretAngle, maxTurretRotation, m_isTransNecTurret));
    }

    private float ClampAngle(float angle, float min, float max, bool isTranslationNecessary)
    {
       if(!isTranslationNecessary)
       {
         if(angle < min )
          return min;

         if(angle > max)
          return max;
       }
       else
       {
         if(angle > max && angle < min)
         {
          if(min - angle > angle - max)
              return max;
          else
              return min;
         }
       }

       return angle;
    }
}

so, it a similar setup, like the tower and gun of a tank….
i have posted that question already here, but it seems like few people seen the post… any advice is appreciated! thanks.

update:

    m_newRotation = Quaternion.LookRotation(m_target.transform.position - towerRotateObj.transform.position).eulerAngles;
    m_newRotation.y -= 90f;
    m_angles = towerRotateObj.transform.rotation.eulerAngles;
    towerRotateObj.transform.rotation = Quaternion.Euler(m_angles.x, m_newRotation.y, m_angles.z);

    m_newRotation = Quaternion.LookRotation(m_target.transform.position - turretRotateObj.transform.position).eulerAngles;
    m_angles = turretRotateObj.transform.rotation.eulerAngles;
    turretRotateObj.transform.rotation = Quaternion.Euler(m_angles.x, m_angles.y, -m_newRotation.x);

the problem stays the same 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:10:45+00:00Added an answer on June 13, 2026 at 1:10 pm

    So my guess, gimbal lock, like said in the other answer. However, I’m not sure I would try to fix THAT, considering you are passing quaternion to euler and vise versa. If I had to do it, I wouldn’t do it the way you did, since using quaternion for parenting between objects is equal to massive headache.

    First, while you are parenting objects, you are not using that feature! This is a very solid feature from Unity. Unity Parenting: http://docs.unity3d.com/Documentation/ScriptReference/Transform-parent.html

    Each of your turret objects only rotate in a single local axis. And Unity is built to handle that: http://docs.unity3d.com/Documentation/ScriptReference/Transform-localRotation.html

    When an object is parented to another, you can rotate it locally in the axis you want. Unity handle the overall matrix transformation on its own. When you are modifying the global rotation of the object, you are basically overriding the transformation coming from the parenting. At this point, it’s very easy for any hierarchy to gain error and imprecision over time.

    EDIT: With your package, I was able to write what I meant:

    public class WeaponMover : MonoBehaviour
    {
        public GameObject boat;
        public GameObject turretRotateObj;
        public GameObject towerRotateObj;
        public GameObject target;
    
        private Vector3 lastDirection;
    
        // initialization
        void Start()
        {
            lastDirection = boat.transform.forward;
        }
    
        void Update()
        {
            // Find direction toward our target. Use turret as origin.
            Vector3 wantedDirection = target.transform.position - turretRotateObj.transform.position;
            // Rotate our last direction toward that new best direction. Change floats to make it move faster or slower.
            lastDirection = Vector3.RotateTowards(lastDirection, wantedDirection, 0.01f, 0.01f);
    
            // Find the direction local to the tower as the boat can move around!
            Vector3 towerDirection = boat.transform.InverseTransformDirection(lastDirection);
            // Remove unwanted axis
            towerDirection = new Vector3(-towerDirection.z, 0, towerDirection.x);
            towerDirection.Normalize();
            // Set local rotation
            towerRotateObj.transform.localRotation = Quaternion.LookRotation(towerDirection);
    
            // Find the direction local to the gun, as the tower may have rotated!
            Vector3 turretDirection = towerRotateObj.transform.InverseTransformDirection(lastDirection);
            // Remove unwanted axis.
            turretDirection = new Vector3(turretDirection.x, turretDirection.y, 0);
            turretDirection.Normalize();
            // Set local rotation
            turretRotateObj.transform.localRotation = Quaternion.LookRotation(turretDirection);
        }
    }
    

    Note: I had to move the gun’s barrel to the Z axis instead of the X axis because I was lazy. So if you only copy paste this code, the barrel will be pointing toward the frame, but the gun will follow the target correctly.

    Since the rotation are now local, you can spin the ship for years, and the gun will never get any offset.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First here's what I'm using and trying to do: the minimal setup for this
This is my first here in Stackoverflow. So I just want to ask question
First timer here, and total noob when it comes to PHP and JavaScript. I
first post here as I'm stuck with my wonderful C++ function. The error I'm
First post here... I normally develop using PHP and Symfony with Propel and ActionScript
First off here is the code! <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Strict//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd>
First question here: it is a very short yet fundamental thing in Java that
first post here, and probably an easy one. I've got the code from Processing's
first post here, I come in peace :) I've searched but can't quite find
First question here so hello everyone. The requirement I'm working on is a small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.