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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Help aiming a tank barrel (locked y look rotation) to target, turret movement is fine though

Discussion in 'Scripting' started by shaunimsorry, Jun 3, 2019.

  1. shaunimsorry

    shaunimsorry

    Joined:
    Jan 19, 2019
    Posts:
    11
    Hey guys, so I'm working on an RTS style game. no problems getting the turret to turn when I click or when a target enters the radius (For the turret head rotation I am using a Quaternion.Look rotation which works like a charm. What i'm having a tough Time doing now is getting the barrel to track the target only on the vertical axis.

    So far what I've tried.
    Making the same function like I have for rotation (typed this by memory but you get the idea)

    Code (CSharp):
    1. var turrTgtQuatRot = new Quaternion (turrent.transform.position - target.transform.position, turrent.transform.up);
    2.     var turrFinQuatRot = (0,turrTgtQuatRot.y,0,turrTgtQuatRot.w);
    3.     turret.transform.rotation = Quaternion.LookRotation(turrFinQuatRot, turrTgtQuatRot, TurretTurnSpeed * Time.deltatime;
    4.  
    So when I tried to inverse this (As the turret only rotates around 'horizontal' but it ends up spazzing out and 'twisting' despite finishing the rotation (X axis read keeps changing ? infinitely) So ive found a work around which is using Euler angles and then locking the other two values but then the barrel stays in one position and no longer rotates with the head. Need some help understanding how I can lock a LookRotation only to the Y. I am thinking once I get back ill try a Slerp style solution. I also suspect the anchor point might be a problem as X makes it move up and down vs changing Y rotation.

    *For me Quaternion.LookAt() is fine but it goes RIGHT for the target (Works perfectly but too fast. Wonder if there is a way I can look at store that value and then interpolate to it from the current destination.*

    Any help would be appreciated as to the best way to do a rotation and then a secondary locked rotation.

    also making the use of this thread to ask a more technical question, id like the barrel to track (vertically) the target when it comes closer to the target vs just going for it right away. Im sure I can do this with a CoRoutine delay as I know the speed of the initial turret time to target lock but wanted to ask if there was a way I can say if you are X > minimum target activation range don't start vertically tracking. Just wanted to making the tracking look fancy

    Thanks guys!!!

    Quaternions are hard but im getting there X_X
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,905
  3. IcePhoenix_0101

    IcePhoenix_0101

    Joined:
    Sep 9, 2017
    Posts:
    32
    just make a temporary target position to use that has same y position as the turret.

    var targetPosition = new Vecter3 (target.transform.position.x, turrent.transform.position.y, target.transform.position.z);
    var turrTgtQuatRot = new Quaternion (turrent.transform.position - targetPosition, turrent.transform.up);
     
    shaunimsorry likes this.
  4. shaunimsorry

    shaunimsorry

    Joined:
    Jan 19, 2019
    Posts:
    11
    this is something I really didn't think about, huh ok I will give this a go later tonight
     
  5. shaunimsorry

    shaunimsorry

    Joined:
    Jan 19, 2019
    Posts:
    11
  6. Team2Studio

    Team2Studio

    Joined:
    Sep 23, 2015
    Posts:
    98
    with this script you can rotate the turret base and the turret separately (turret base horizontally, turret vertically). You can also set different distances for the turret base and turret to start tracking.
    If the minimum distance is not met, the base and turret will turn back to resting positions.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class MyScript : MonoBehaviour
    4. {
    5.     public Transform turret;
    6.     public Transform turretBase;
    7.     public float minBaseTrackDistance;
    8.     public float minTurretTrackDistance;
    9.     public Transform target;
    10.  
    11.     Vector3 turretBaseRestPos = Vector3.zero;
    12.     Vector3 turretRestPos = Vector3.zero;
    13.  
    14.     float turretBaseSpeed = 1f;
    15.     float turretSpeed = 2f;
    16.  
    17.     void Update()
    18.     {
    19.         if (target)
    20.         {
    21.             if (Vector3.Distance(turretBase.position, target.position) < minBaseTrackDistance)
    22.             {
    23.                 Quaternion baseRot = Quaternion.LookRotation(target.position - turretBase.position);
    24.                 baseRot = new Quaternion(0f, baseRot.y, 0f, baseRot.w);
    25.                 turretBase.rotation = Quaternion.Slerp(turretBase.rotation, baseRot, Time.deltaTime * turretBaseSpeed);
    26.             }
    27.             else
    28.             {
    29.                 TurnBaseToRestPos();
    30.             }
    31.  
    32.             if (Vector3.Distance(turret.position, target.position) < minTurretTrackDistance)
    33.             {
    34.                 Quaternion turretRot = Quaternion.LookRotation(target.localPosition - turret.localPosition);
    35.                 turretRot = new Quaternion(turretRot.x, 0f, 0f, turretRot.w);
    36.                 turret.localRotation = Quaternion.Slerp(turret.localRotation, turretRot, Time.deltaTime * turretSpeed);
    37.             }
    38.             else
    39.             {
    40.                 TurnTurretToRestPos();
    41.             }
    42.         }
    43.     }
    44.  
    45.     void TurnBaseToRestPos()
    46.     {
    47.         Quaternion lookRot = Quaternion.LookRotation(turretBaseRestPos);
    48.         turretBase.rotation = Quaternion.Slerp(turretBase.rotation, lookRot, Time.deltaTime * turretBaseSpeed);
    49.     }
    50.  
    51.     void TurnTurretToRestPos()
    52.     {
    53.         Quaternion lookRot = Quaternion.LookRotation(turretRestPos);
    54.         lookRot = new Quaternion(lookRot.x, 0f, 0f, lookRot.w);
    55.         turret.localRotation = Quaternion.Slerp(turret.localRotation, lookRot, Time.deltaTime * turretBaseSpeed);
    56.     }
    57. }
    58.  
     
    Ufol likes this.