Search Unity

the rotation of the gun in the tank does not work well

Discussion in 'Scripting' started by Trild123787898, Jan 15, 2020.

  1. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    I wrote a script so that my cannon rotates in x, but when I rotate the turret, the cannon rotates in the opposite direction why so?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class mehanic : MonoBehaviour
    7. {
    8.     public Transform detail1;
    9.    
    10.  
    11.     void Start()
    12.     {
    13.        
    14.  
    15.  
    16.     }
    17.     void FixedUpdate()
    18.     {
    19.  
    20.         if (Input.GetKey(KeyCode.E))
    21.         {
    22.            
    23.  
    24.             detail1.rotation = Quaternion.RotateTowards(detail1.rotation, Quaternion.Euler(new Vector3(30, 0, 0)), 30f * Time.deltaTime);
    25.  
    26.  
    27.         }
    28.         if (Input.GetKey(KeyCode.Q))
    29.         {
    30.  
    31.             detail1.rotation = Quaternion.RotateTowards(detail1.rotation, Quaternion.Euler(new Vector3(-30, 0, 0)), 30f * Time.deltaTime);
    32.  
    33.  
    34.         }
    35.        
    36.        
    37.     }
    38. }
    39.  
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Be more specific. When you say "rotate in x", what do you mean? Based on the keys being used, I think you mean to rotate left and right (like shaking your head no), but when you use the X rotation, you're rotating around the X axis. Rotating on the X axis actually rotates like you're nodding your head. If you want to rotate left and right, you need to use the Y axis.
     
  3. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    Sorry, but I have a turn on the x axis going up and down, closer to the point I still can not understand why it does this?
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    When you describe rotations with Euler angles, what you're actually describing with each number is the axis around which it's rotating. The X axis points to the right, Y axis points up, and Z axis points forward.

    So now, rotate around the axis that points to the right... and your object "nods" up and down. That's what you're doing now.

    What you want to do (I assume) is have it turn left and right, which means you want it to rotate around the axis that points upward. That's the Y axis. So, your Euler parameters should be (0,30,0) rather than (30,0,0).
     
    Joe-Censored likes this.
  5. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    I have a cannon and a turret, the cannon must go up and down, the turret to the right and left, and everything works well, only when I rotate the TOWER, the cannon no longer rotates along those axes along which I set
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Oh, ok. That changes things. This is why it's important to be clear and precise when describing problems.

    You need to be using localRotation for this, rather than rotation, in all 4 places where you use rotation in this code. "rotation" means the worldspace rotation, but you actually want the rotation relative to the tower - its local rotation.
     
  7. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    yea
     
  8. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    maybe it's stupid but I tried to do it and nothing changed
    Code (CSharp):
    1.  detail1.localRotation = Quaternion.RotateTowards(detail1.rotation, Quaternion.Euler(new Vector3(30, 0, 0)), 30f * Time.deltaTime);
     
  9. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    sorry corrected, missed such a trifle, thanks for the advice