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

[C#] Clamp Rotation At 0?

Discussion in 'Scripting' started by BsseeJ, Jun 22, 2014.

  1. BsseeJ

    BsseeJ

    Joined:
    Mar 12, 2014
    Posts:
    20
    Hello Unity Forum

    I am making a 2D shooter and would like to clamp my player characters rotation to 0 in each axis. It doesn't seem to work out like the clamp I made to the position which works fine. I've been looking at code and I don't understand what I've done wrong. Any suggestions?

    Code (CSharp):
    1.      
    2. Vector3 rot = new Vector3
    3.        (Mathf.Clamp(gameObject.transform.rotation.x, 0, 0),
    4.         Mathf.Clamp(gameObject.transform.rotation.y, 0, 0),
    5.         Mathf.Clamp(gameObject.transform.rotation.z, 0, 0)
    6.         );
    7.      transform.rotation.eulerAngles = rot;
    8.  
    Thanks for any help from Jesse "BsseeJ" Bergerstock.
     
    Rnewbold likes this.
  2. Deequation

    Deequation

    Joined:
    Jan 2, 2014
    Posts:
    16


    What you are doing here is limiting each of the vector-components, to stay within an interval/range of [0, 0]. If you take a look at the clamp function 'Mathf.Clamp(value, minimum, maximum)' what it does is it returns the value, but forces it to fit between the minimum and maximum value. If this is indeed your intended goal, then you can do a couple of other things that makes more sense and are more efficient, depending on the reason behind your unwanted rotation.




    [01] Rotation as a result of physics:


    If your rotation is a result of using a rigidbody with collisions, then you can lock the rotation of the rigidbody by either increasing the angular drag (kind of a hack and unstable solution) or simply by setting the constraint flags. This will prevent it from rotating around the axis that you locked, as a result of forces. You should note that at the time of writing this, the two dimensional rigidbody doesn't have the constraint flags option.




    [02] Manually overwriting rotation:


    If your rotation comes from a manual source that you can't remove, and you insist on overwriting it manually, you can do this by simply setting the transforms rotation to your desired rotation at the end of each frame, eg. in LateUpdate. You can set it as 'transform.rotation = Quaternion.identity' as this corresponds to a rotation in Euler angles of (0, 0, 0), or create a custom rotation with a single axis set to zero if you wish to preserve the other angles. In some sense overwriting the rotation each frame is not ideal, and you should rather find the source of the rotation. But it works and are a pretty fast solution.




    [03] Using the clamp function:


    The clamp function has its uses in situations such as - following that this is a 2D shooter - if you wanted to restrain your character to only aiming upwards, straight ahead and backwards in a [0, 180] degree interval (in positive direction), then you could limit the rotation to stay within that interval. That would look something like this.

    Code (CSharp):
    1. ..
    2. Mathf.Clamp(myRotationAxisInEulerDegrees, 0.0f, 180.0f)
    3. ..




     
    Last edited: Jul 22, 2015
    Thoaril, Rnewbold and sam_fisher3000 like this.
  3. BsseeJ

    BsseeJ

    Joined:
    Mar 12, 2014
    Posts:
    20

    I attempted to setup in the code the 'transform.rotation = Quaternion.identity and it almost works but not entirely. When an object hits the player character object it shifts slightly off the z rotation and slowly goes back to 0. I then froze the rotation by rigidbody2D.fixedangle = true because the froze method is gone from the 2D for whatever reason. Thanks for the suggestion. I am trying to clamp the rotation in a more complicated way as well if you have time I'd like it if you could see anything about this post, this post being where I think I have to setup a sophisticated clamp of some sort on my weapon object.

    http://forum.unity3d.com/threads/c-how-to-make-an-object-anchor-on-another.252949/