Search Unity

Limit Rotations

Discussion in 'Scripting' started by Frostbite23, Mar 30, 2014.

  1. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458
    Hi guys, Im creating a 2D shooter, I have the main things down first, but with my 2d aming script I dont want my players arm to go over his head. I want to limit the arm rotation based on two variables, "maxUP" and "maxDown". How can I do this?
    Code (csharp):
    1. #pragma strict
    2. private var flipRight = false;
    3. private var flipLeft = false;
    4. var mainCamera : Camera;
    5. public var distance = 10.0;
    6. var PlayerSprite : Transform;
    7. var maxUP : float = 100;
    8. var maxDown : float = -96;
    9. function Update () {
    10.     if(flipRight == true  flipLeft == false){
    11.     transform.localScale.x = 1;
    12.     transform.localScale.y = 1;
    13.     var pos = mainCamera.WorldToScreenPoint(transform.position);
    14.     var dir = Input.mousePosition - pos;
    15.     var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
    16.     transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
    17.     transform.rotation.z = Mathf.Clamp (transform.rotation.z, maxDown, maxUP);
    18.     }
    19.     if(flipRight == false  flipLeft == true){
    20.     //transform.localScale.x = -1;
    21.     transform.localScale.y = -1;
    22.     var pos2 = mainCamera.WorldToScreenPoint(transform.position);
    23.     var dir2 = Input.mousePosition - pos2;
    24.     var angle2 = Mathf.Atan2(dir2.y, dir2.x) * Mathf.Rad2Deg;
    25.     transform.rotation = Quaternion.AngleAxis(-angle2, Vector3.forward);
    26.     }
    27. }
    28.  
    29. function PlayerFlipRight () {
    30.     flipRight = true;
    31.     flipLeft = false;
    32. }
    33.  
    34. function PlayerFlipLeft () {
    35.     flipRight = false;
    36.     flipLeft = true;
    37. }
    Please reply ASAP, im really getting frustrated trying to find a good answer but most dont help me.
     
    Last edited: Mar 30, 2014
  2. glacius3000

    glacius3000

    Joined:
    Oct 5, 2012
    Posts:
    69
    Just cap the angle.

    some psuedocode below. This could be optimized better if you know a bit more about Quaternions but it'll do.
    Code (csharp):
    1.  
    2. //do your angle calculations
    3. if(transform.rotation.eulerangles.x > 100)
    4. {
    5. Quaternion newRot = Quaternion.euler(100,transform.rotation.eulerangles.y,transform.rotation.eulerangles.z);
    6. transform.rotation = newRot;
    7. }
    8.  
     
  3. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458
    glacius3000, psuedocode wont help me, im not an expert in quaternations but sir i need someone who actually help, by that i mean one who can actually show me an example code or just add a few lines of code to my code.
     
  4. Joker_54

    Joker_54

    Joined:
    Nov 24, 2013
    Posts:
    64
    What is difficult on that piece of code?
    It just says, if the rotation is over 100(°?) in the x-direction, it sets it to 100. Nothing too fancy about it.
    The only noticable thing is: you can't set just a specific amount of rotation or scale to an Object. you always have to built up a complete Quarternion/Vector3.