Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

C# Unity 2D Turret Angle Arc of Rotation script issue.

Discussion in 'Scripting' started by GonTar_X, Oct 15, 2018.

  1. GonTar_X

    GonTar_X

    Joined:
    May 1, 2018
    Posts:
    7
    Hello There Fellow Unity Users.

    I'm kind of new to C# and i've managed to put together and progress a lot for the basic knowledge i've of C# on this project, i'm working at with a friend.

    But i've been hours already frustrating my self trying to fix an issue with the turret system i'm currently using for our Player Ships (imagine it's like a 2D WarShips MP Game with battleships, in this example using turrets to aim n' shoot)

    //

    The Problem here is, that the Angle Limit (when clamped) is always set to the (world position y)

    Wich from a top down 2d game looks like:
    >all player ships arc of fire, are stuck facin the UPwards or Positive Y coordinates.

    Instead of having this Arc of Fire (AoF) rotating along with the player ship, it gets stuck restricting rotation on the same V shape looking upwards like this (imagine this is the AoF with the ship pointing Up) \V/ instead of 4 example:

    looking like this..

    if i turn the ship to the right, the AoF should look like this: <

    if i turn the ship to the Left, the AoF should look like this: >

    and if we are pointing downwards, it should look like this: /\

    is there a way i can get the Arc of Fire to rotate along with the PlayerObject?

    Thanks in advance and i hope i was clear enough!

    Code (CSharp):
    1.  
    2. public class PlayerShipTurret : NetworkBehaviour {
    3.     public float speed = 15f;
    4.     public Camera _cam;
    5.     public GameObject turret;
    6.     public float maxrot = 25f;
    7.     public float minrot = -25f;
    8.     void FixedUpdate() {
    9.         if (!isLocalPlayer)
    10.         {
    11.             return;
    12.         }
    13.         //get mouse position
    14.         Vector2 direction = _cam.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    15.  
    16.         //get the above mouse pos X/Y values and get the angle
    17.         float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
    18.         Debug.Log(string.Format("angle = {0}", angle));
    19.  
    20.         //clamp (limit the angle between the 2 numbers) the float max & min values
    21.         float firearc = Mathf.Clamp(angle, minrot, maxrot);
    22.      
    23.         //get the new clamped angle
    24.         Quaternion rotation = Quaternion.AngleAxis(firearc, Vector3.forward);
    25.  
    26.         //apply the angle in form of rotation
    27.         turret.transform.rotation = Quaternion.Slerp(turret.transform.rotation, rotation, speed * Time.deltaTime);
    28.      
    29.     }
    30. }
    Please illuminate me, i don' know wich functions i'm missing to make the angle float return 0 at the direction we are actually facing, instead of angle "0" being always Upwards relative to the game world.
     
  2. GonTar_X

    GonTar_X

    Joined:
    May 1, 2018
    Posts:
    7
    I guess the problem i'm struggling with is, the fact that the point where the "rad2deg" returns the angle "0" always pointing up?

    or there's a problem with the first function i'm using with the Camera screen to gameworld and the mouse.input?

    i can't figure it out it's been a long day and my brain is killing me, please help!
     
  3. GonTar_X

    GonTar_X

    Joined:
    May 1, 2018
    Posts:
    7
    Code (CSharp):
    1.         //clamp (limit between 2 numbers) the Angle float max & min values
    2.         float shipAngle;
    3.         Vector3 axis = new Vector3(0, 0, 0);
    4.         transform.rotation.ToAngleAxis(out shipAngle, out axis);
    5.         Debug.Log(string.Format("shipAngle = {0}", shipAngle));
    6.  
    7.         float firearc = Mathf.Clamp(angle, minrot - shipAngle, maxrot - shipAngle);
    8.  
    9.         Debug.Log(string.Format("c.angle = {0}", firearc));
    My bro, changed these lines, and we came up with this, wich moves the Arc of Fire with the Ship.
    Untill we reach + or - 180º, then things start to get funny,

    The turret AoF turns on the right side angle just fine, but when turning left, it inverts itself, and when reaching 180º it just breaks.

    any help/hints? anyone?
     
unityunity