Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

I need help figuring out how to use mathf clamp to get fixed rotatio of an object

Discussion in 'Scripting' started by xxfmxx, Mar 25, 2015.

  1. xxfmxx

    xxfmxx

    Joined:
    May 28, 2014
    Posts:
    32
    so basically what I am trying to achieve is to get an object to rotate upon collision like say if my player shot it
    then it would changed to a specified angle in z only
    what im having trouble with is how to get it so that when i shoot at the mirror object ... upon collision it would change its angle .. and then if i shot it again it would change to the next angle in line

    i essentially want it to act like its shown in the videos




    this is what i got thus far code wise

    Code (CSharp):
    1.     private float Clamp1;                //Clamp function for Front
    2.     private float Clamp2;               //Clamp function for angle
    3.     private float Clamp3;
    4.     public float FrontClamp = 0.0f;     //Clamp for in front of character
    5.     public float AngleClamp = 45.0f;    //Clamp for angle
    6.     public float AboveClamp = 90.0f;
    7.     public float speed = 5.0f;
    8.     private float rotationZ = 0f;
    9.     void Awake()
    10.     {
    11.  
    12.      
    13.         Clamp1 = Mathf.Clamp (FrontClamp, FrontClamp, FrontClamp);        //The clamp to keep the shield locked in front position
    14.         Clamp2 = Mathf.Clamp (AngleClamp, AngleClamp, AngleClamp);        //The clamp to keep the shield locked in angle position
    15.         Clamp3 = Mathf.Clamp (AboveClamp, AboveClamp, AboveClamp);        //The clamp to keep the shield locked in above position
    16.      
    17.     }
    18.  
    19.  
    20.     // Use this for initialization
    21.     void Start ()
    22.     {
    23.         rotatenumber = 0;
    24.  
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update ()
    29.  
    30.     {
    31.  
    32.  
    33.     }
    34.  
    35.     void OnCollisionEnter2D(Collision2D coll) {
    36.         if (coll.gameObject.tag == "Shield")
    37.             Debug.Log("shield object has collided");
    38.         this.transform.rotation = Quaternion.Euler (0.0f, 0.0f, Clamp3);
    39.  
    40.      
    41.        
    42.      
    43.     }
    44. }
     
    Last edited: Mar 25, 2015
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    I wouldn't use Clamp for something like this, that's for ensuring a value falls within a specified range.

    Based on your second video, it looks like you just want to cycle through a known quantity of angles. Why not use a list and just change the target angle every time a collision occurs?

    Something like:
    Code (csharp):
    1.  
    2. public List<float> angles;  // Set the angles in the editor, like 0, 45 and 90 in your second video.
    3. protected int currentAngle = 0; //  Index of the current angle.
    4.  
    5. void Update()
    6. {
    7.      // Use this one if you want a snap, instant rotation.
    8.      transform.rotation = Quaternion.Euler(0.0f, 0.0f, angles[currentAngle]);
    9.      // Use this one if you want to rotate over time to the new position.
    10.      transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0.0f, 0.0f, angles[currentAngle]), 45.0f * Time.deltaTime); // 45 = 45 degrees a second,
    11. }
    12.  
    13. void OnCollisionEnter2D(Collision2D coll)
    14. {
    15.      if(coll.gameObject.tag == "Shield")
    16.      {
    17.           Debug.Log("shield object has collided");
    18.           currentAngle = (currentAngle + 1 == angles.Count) ? 0 : currentAngle + 1;
    19.      }
    20. }
    21.  
     
  3. xxfmxx

    xxfmxx

    Joined:
    May 28, 2014
    Posts:
    32
    excuse my newbness but what is happening on this line
    currentAngle =(currentAngle +1== angles.Count)?0: currentAngle +1;
    i kinda get the the feeling that its simply the way getting to the next angle in line
    but can you explain whats happening specifically at this portion of the code (currentAngle +1== angles.Count)?0: currentAngle +1;
    i havent used operators to such a degree yet i know what == means and does and + and = what I havent used
    too much is the ? operator and the colon in the manner that you have so its still a bit confusing and daunting to me
     
  4. Brominion

    Brominion

    Joined:
    Sep 30, 2012
    Posts:
    48
    its reads like this <condition statement>? < statement if true> : <statement if false>
    so the statement becomes
    Code (CSharp):
    1. if(currentAngle + 1 == angles.Count)
    2. {
    3. currentAngle = 0;
    4. }
    5. else
    6. {
    7. currentAngle = currentAngle +1;
    8. }
    The fact that the ?operator quickly becomes unreadable, is why its normally not allowed in code that needs to be maintained, read or otherwise worked on.