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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Rotating a Turret in a 2D Game on Z Axis

Discussion in 'Scripting' started by SheldonS, Jan 20, 2017.

  1. SheldonS

    SheldonS

    Joined:
    Jul 23, 2015
    Posts:
    21
    Hello everyone,

    I want to make sure I am doing this correctly. It appears to work, but I know that doesn't always mean it is the proper way to achieve something.

    I have a 2D game. In this game I have a gun turret that I only want the player input to rotate the Z Axis. Using the scene tools I determined the maximum and minimum Z to be 25 and -25.

    Here is my implementation, please let me know what I should do differently.

    Code (CSharp):
    1.  
    2.     public float turretSpeed = 50.0f;
    3.     public float maxAngleDown = -25.0f;
    4.     public float maxAngleUp = 25.0f;
    5.     private float zAxis = 0.0f;
    6.  
    7.     // This function is called every fixed framerate frame, if the MonoBehaviour is enabled
    8.     private void FixedUpdate()
    9.     {
    10.         float zInput = Input.GetAxisRaw("VerticalGun");
    11.         if (zInput != 0)
    12.         {
    13.             zAxis += (Time.deltaTime * zInput * turretSpeed);
    14.             zAxis = Mathf.Clamp(zAxis, maxAngleDown, maxAngleUp);
    15.             transform.rotation = Quaternion.Euler(0, 0, zAxis);
    16.         }
    17.     }
     
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    The code seems to be correct , other than the high turretSpeed. At 50 it will rotate from one end to the other in 1 second. and from straight up to far left or far right in 1/2 a second. This is how I would do it, does it do anyting odd or weird or does it work correctly?

    Just note if you ever try to get the angle its currently at and manipulate it, it won't be a negative value if you pointing to the left. from 0 to -25 will return 0->335. Unity transforms all your negatives it to the positive rotation in 0->360. However since your keeping track of it outside and then just inputting the values , unity will convert it afterwards and everything should work smoothly.
     
  3. SheldonS

    SheldonS

    Joined:
    Jul 23, 2015
    Posts:
    21
    Thank you takatok.

    I definitely agree, the turret speed is too fast, but that was mainly for me to know it was moving; I tried a few different ways until I read more of the Unity docs.

    To answer your question, yes it is behaving as I desired. Except the speed but that will be easy enough to fix.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    Your choice of using a float to in turn drive a rotation is what I consider Goor Practice(tm).

    There are too many tutorials out there that suggest applying successive rotations, which always accumulate more and more error.

    By driving it with a float you avoid all of those problems. Your approach keeps the data flow simple and one-directional by NOT reading back the .eulerAngles of something you are already driving with a float. I approve. :)
     
    SheldonS likes this.
  5. SheldonS

    SheldonS

    Joined:
    Jul 23, 2015
    Posts:
    21
    Thank you very much Kurt-Dekker!