• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by gravelpootis · Apr 04, 2020 at 08:55 PM · rotation axis

Tank turret rotation problem,Tank turret rotation axis problem

Hello, I was not sleeping because I was trying to figure out how to fix this. My problem is turret X axis rotation. (see gif)
I just want to rotate turret X axis with tank. I found issue, please check last two images -> https://imgur.com/a/6zlp4IR why is that ? :|

 public class Tank : MonoBehaviour
 {
     public GameObject cannon;   // not using yet
     public GameObject turret;   // tank turret
     private GameObject tank;    // tank
 
 
     // Debug
     public Text text1;
     public Text text2;
     public Text text3;
 
 
     public LayerMask layer;
     private Camera cam;
 
     public float    TurretRotationSpeed;
 
     private float   _defaultRotationX = -90; // This is default X axis rotation for turret, because if I set it to 0 then turret will be flipped 
 
     private Quaternion  _lookRotation;
     private Vector3     _direction;
 
     void Start()
     {
         cam = Camera.main;
         tank = gameObject;
     }
 
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.Space))
             ResetTurretRotation();
 
         TurretRotation();
     }
 
     private void TurretRotation()
     {
         text1.text = $"turret rotation: {turret.transform.rotation.x}, {turret.transform.rotation.y}, {turret.transform.rotation.z}";
         text2.text = $"tank rotation: {tank.transform.rotation.x}, {tank.transform.rotation.y}, {tank.transform.rotation.z}";
 
         /// Here I want to update turret rotation X axis based on tank X rotation to fix that issue but It is not working :(
         Vector3 newRotation             = new Vector3(tank.transform.eulerAngles.x - _defaultRotationX, tank.transform.eulerAngles.y, tank.transform.eulerAngles.z);
         turret.transform.eulerAngles    = newRotation;
 
         // Code below is used for moving (aiming) turret in Y axis (maybe Z axis too idk), so no up down just to sides
 
         Ray camRay = cam.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
 
         if (Physics.Raycast(camRay, out hit, float.MaxValue, layer)) // I use this because I want to have dynamic raycasting Reticle like WoT
         {
             //find the vector pointing from our position to the target
             _direction      = (hit.point - turret.transform.position).normalized;
 
             Vector3 fixedRot = new Vector3((turret.transform.rotation.x - tank.transform.rotation.x), 0, 0);
 
             _direction.y = 0;
             _direction.x = (_defaultRotationX) + fixedRot[0];
 
             text3.text = $"calc: {_direction.x}, {_direction.y}, {_direction.z}";
 
             //create the rotation we need to be in to look at the target
             _lookRotation = Quaternion.Euler(_direction);
 
             //rotate us over time according to speed until we are in the required rotation
             turret.transform.rotation = Quaternion.Slerp(turret.transform.rotation, _lookRotation, Time.deltaTime * TurretRotationSpeed);
         }
     }
 
     private void ResetTurretRotation()
     {
         // Here I want to reset turret rotation based on tank rotation, I tried some calculations using tank.transfrom.rotation but I cant figure out why it was not working, it was always reseting turret rotation to 0
         Vector3 resetRotation       = new Vector3(_defaultRotationX + tank.transform.rotation.x, tank.transform.rotation.y, tank.transform.rotation.z);
         turret.transform.rotation   = Quaternion.Euler(resetRotation);
 
         // Im not sure if im calculating AXISes correctly
     }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gravelpootis · Apr 04, 2020 at 03:41 PM 0
Share

Okay I fixed it finally after 13 hours... >:(

https://imgur.com/a/mxgJrdd

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by dougantor · Apr 05, 2020 at 10:40 AM

The reason those two rotations (i.e the one printed on screen and the one on the inspector) are different is because one is a Quaternion, and the other is a Euler angle.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

124 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Issues rotating around transform.up 2 Answers

Keep AI from flipping? 1 Answer

Rotation gameobject 0 Answers

How can I get Rigidbody.MoveRotation to rotate in world space on two axes? 1 Answer

Rotate GameObject With GUI Slider 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges