Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Turret rotation

Discussion in 'Scripting' started by UnityLighting, Dec 27, 2021.

  1. UnityLighting

    UnityLighting

    Joined:
    Mar 31, 2015
    Posts:
    3,793
    Hi

    I want to rotate the turret continuously between certain angles... In other word I want to simulate seeking mode for turret

    Currently i Implemented using animator, I want to implement it using scripting

    I have a lot of experience in scripting but I don't know anything about Quaternion calculations:D
    Someone can share the script ?

    Animator video:
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
  3. UnityLighting

    UnityLighting

    Joined:
    Mar 31, 2015
    Posts:
    3,793
    This is the correct script:

    Code (CSharp):
    1.        
    2. // Turret's gun seeking animation system
    3.  
    4. public Transform gunHead;
    5.  
    6. // Seek animation
    7. public float seekSpeed = 50f;
    8. public float rotateAngle = 70f;
    9.  
    10. // Internal variables
    11. Vector3 originalRotation;
    12.  
    13. void Start()
    14. {
    15.         // Save the original rotation of the gun head
    16.         originalRotation = gunHead.localRotation.eulerAngles;
    17. }
    18. void Update()
    19. {
    20.          gunHead.localRotation = Quaternion.Euler(originalRotation.x, Mathf.PingPong(Time.time * seekSpeed, rotateAngle * 2) - rotateAngle, 1f);
    21. }
    22.  
    23.  
    24.  
    25.  
     
  4. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    mathf.clamp might be a decent choice as well if you want to make sure the rotation is completely restricted.
     
    UnityLighting likes this.
  5. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
    Ok so I would do this using the forward of the turret head. So what the forward looks at is where the turret looks. Where the prefab or object begins is his starting forward as you have done so. Take this forward value and store it. So you want to be rotating the turret on one part of its Euler as can be done in editor, maybe the y or the z. And you want to be halting your total angle and subtracting and adding it to your forward to get the bounds of your turrets rotational abilities. So unless it’s looking at an enemy, it’s forward can lerp back and forward between the bounds of its view angle which in desirable terms relates to 90*, -45 and + 45* from the objects starting forward.

    tell it so that transform.forward should = transform.forward + transform.right / x. and if you are adding eulers correctly you should be able to measure how many angles you added or subtracted along with this movement. And if this value should exceed 45 or -45* on a custom float Axis, then the turret shall not surpass the angle.
     
  6. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
    If the turret is a child of the blue "pillard" you can use its forward as reference...

    EDIT: back to my pc where i've an example. the angle is clamped between 25 on the left (the turret is on the right shoulder of the player. this prevents that he shoots his own chick...) and 90 degrees on the right. The thing is to know if the target (enemy) is on the left or the right. Hope it helps...

    Code (CSharp):
    1.     private void Update()
    2.     {
    3.         //Get the local direction from the parent to enemy (this works even if the parent moves)
    4.         Vector3 directionToTarget = enemy.position - transform.parent.position;
    5.         float dirAngle = Vector3.Angle(transform.parent.forward, directionToTarget);
    6.      
    7.         //Is Enemy on the left or on the right.
    8.         Vector3 enemyDirectionLocal = transform.parent.InverseTransformPoint(enemy.transform.position);
    9.         float dirX = enemyDirectionLocal.x;
    10.  
    11.         //Clamping the angle between 25 on the left and 90 on the right. Over 25 on the left the player shoot himself...
    12.         if (dirX < 0)
    13.         {
    14.             enemyDetected = Physics.CheckSphere(transform.parent.position, detectionRadius, whatIsEnemy) && Mathf.Abs(dirAngle) < 25.0f;
    15.         }
    16.         else
    17.         {
    18.             enemyDetected = Physics.CheckSphere(transform.parent.position, detectionRadius, whatIsEnemy) && Mathf.Abs(dirAngle) < 90;
    19.         }
    20.  
    21.         if (enemyDetected)
    22.         {
    23.             Shoot();
    24.  
    25.         }
    26.         else
    27.         {
    28.             StopShoot();
    29.         }
    30.     }
    31.  
    And to look at target, in shoot method, Quaternion.RotateTowards() as said above works pretty good.

    Code (CSharp):
    1.         //Setting  LookAt angle.
    2.         Quaternion angle = Quaternion.LookRotation(enemy.position - transform.position);
    3.         //look at target in a smooth transition.
    4.         Quaternion _LookAt = Quaternion.RotateTowards(transform.rotation, angle, 6.0f);
    5.         transform.rotation = _LookAt;
    6.  
     
    Last edited: Dec 30, 2021