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

Restrict Z rotation of cannon

Discussion in 'Scripting' started by KazYamof, Nov 28, 2016.

  1. KazYamof

    KazYamof

    Joined:
    Jun 26, 2015
    Posts:
    59
    I'm trying to do a cannon that aims on the player when him enters cannon's field of view.
    It's a 2d game prototype, so the cannon rotates only on Z axis.
    I already made the cannon look for wherever the player is located, like I show below.


    Code (CSharp):
    1.  void Update()
    2.     {
    3.         //tangent between vectors
    4.         tan = (target.position - transform.position);
    5.         Debug.DrawLine(transform.position, tan.normalized, Color.red);
    6.  
    7.         //ok, the cannon always look to target
    8.         transform.right = tan;
    9.     }
    Now I want to restrict the rotation only to a specific range, like this:

    The cannon will follow the player until gets out of view, and then will stay there in stand by. The 0º will be the current rotation of the cannon, so the limits will act over the current rotation. If starts at 90º, the limits are 90+45º and 90-45º.

    I'm looking on some answers about how to limit rotations, but I haven't find a good solution for this scenario.
    What approach should I do to get this works?
     
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    You just have to check the z component of the transform.rotation and restrict it to the range you want. Just remember that rotation's in Unity go from 0->360. So a rotation of -45 is actually 315.

    Now for the z-Rotation to be 0 in your picture you would need to be using transform.up. Though this would require that you have your cannon pointing straight up when you import it. I'm assuming you cannon is pointing directly right in your default picture, so 0 degrees rotation would also point directly right.

    If you change your picture so it point up, it does make the code easier to read and work with:
    This code would restrict it to -45 to 45
    Code (CSharp):
    1.  transform.up = dir.normalized;
    2.  
    3.         Vector3 eulerAngles = transform.rotation.eulerAngles;
    4.         if (eulerAngles.z > 45f && eulerAngles.z <= 180)
    5.             eulerAngles.z = 45f;
    6.         if (eulerAngles.z > 180f && eulerAngles.z < 315f)
    7.             eulerAngles.z = 315f;
    8.         transform.rotation = Quaternion.Euler(eulerAngles);
    To do the same thing with your current code (Using transform.right) you would need to do this:
    Code (CSharp):
    1. transform.right = dir.normalized;
    2.  
    3.         Vector3 eulerAngles = transform.rotation.eulerAngles;
    4.         if (eulerAngles.z < 45f || eulerAngles.z > 315)
    5.             eulerAngles.z = 45f;
    6.         if (eulerAngles.z > 135f && eulerAngles.z <= 315f)
    7.             eulerAngles.z = 135f;
    8.         transform.rotation = Quaternion.Euler(eulerAngles);
    This is because eulerAngles.z = 0 is pointing directly right. So the +45 in your picture is +45 as well. but -45 is 135 degrees. so we need to keep the canon between 45 and 135 (z - rotation in 2D Unity is counterclockwise)

    Note both sets of code have a "Crossover Point" where the cannon will instantly flip from one side to the other if the object it is tracking is allowed to go under it.
     
  3. KazYamof

    KazYamof

    Joined:
    Jun 26, 2015
    Posts:
    59
    Is this matematically impossible to solve, right?
     
    Last edited: Nov 29, 2016
  4. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    No. Imagine your cannon can only point up from -45 to +45. When the object is below it but to the right it will be at the +45 degree point. As the object slowly moves under neath your cannon (where it can't spin to because we've intentionlly limited it from -45 to +45 pointing up), it will eventually cross the 180 degree point.. At that moment the cannon's limiting code will flip it over to -45 degrees.

    You can add extra code so the cannon slowly moves across from +45 to -45 pointing upwards as the object moves below it, but that probably would look even stranger.