Search Unity

  1. New Unity Live Help updates. Check them out here!

    Dismiss Notice

Limit Sprite Rotation

Discussion in 'Scripting' started by Frostbite23, Jun 29, 2014.

  1. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    430
    Hi guys, I'm trying to limit the arm rotation on my player on the x axis, for example i want to clamp it at 100 (max angle) and then clamp it at -96 (min angle)
    https://www.dropbox.com/s/bhgjked3k7bnyoj/Screenshot 2014-06-28 22.57.11.png
    now thats the default position, i want the players arm to clamp at this angle (100)
    https://www.dropbox.com/s/t0zkbay00aecstf/Screenshot 2014-06-28 22.57.40.png
    and clamp it at this angle (-96)
    https://www.dropbox.com/s/5arwggbq5jb1wlo/Screenshot 2014-06-28 22.57.58.png

    I've looked at unitys mouse look script but it still doesn't work and doesn't help at all.

    P.S scripts must be JS.
     
    Last edited: Jun 29, 2014
  2. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    219
  3. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    430
    Ok thanks but can you write a quick example code? kind of hard to understand the code you have in the link
     
  4. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    219
    7th message in that thread is an example.
     
  5. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    430
    but thats C#, I don't know C#

    And from looking at the code thats just for rotating along the y axis while pressing down X and uses some so called speed up variable i don't need.

    EDIT: Ok i got it converted with no errors but its doing absolutely nothing, I'm assuming it has to be with the aming script i have
    Code (JavaScript):
    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.    
    12.     transform.localScale.x = 1;
    13.     transform.localScale.y = 1;
    14.    
    15.     var pos = mainCamera.WorldToScreenPoint(transform.position);
    16.     var dir = Input.mousePosition - pos;
    17.     var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
    18.     transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
    19.    
    20.         if(transform.localEulerAngles.z > maxUP){
    21.             transform.localEulerAngles.z -= 0.1;
    22.         }
    23.     }
    24.    
    25.     if(flipRight == false && flipLeft == true){
    26.    
    27.     //transform.localScale.x = -1;
    28.     transform.localScale.y = -1;
    29.    
    30.     var pos2 = mainCamera.WorldToScreenPoint(transform.position);
    31.     var dir2 = Input.mousePosition - pos2;
    32.     var angle2 = Mathf.Atan2(dir2.y, dir2.x) * Mathf.Rad2Deg;
    33.     transform.rotation = Quaternion.AngleAxis(-angle2, Vector3.forward);
    34.    
    35.     }
    36. }
    37.  
    38. function PlayerFlipRight () {
    39.     flipRight = true;
    40.     flipLeft = false;
    41. }
    42.  
    43. function PlayerFlipLeft () {
    44.     flipRight = false;
    45.     flipLeft = true;
    46. }
    And remember that i want to clamp it at to angles because i don't want my players arms to go over those to points
     
    Last edited: Jun 29, 2014
  6. kariwmklawm

    kariwmklawm

    Joined:
    Jul 28, 2014
    Posts:
    4
    Hello, I think I understand what you want, I built a mini script to limit the X angle quickly, here it is for you (even converted it to JS, I use C# :) )

    Code (JS):
    1. #pragma strict
    2.  
    3.      var RotationMinimum : float = -96;
    4.      var RotationMaximum : float = 100;
    5.      
    6.      var ReverseRotation = false;
    7.      var RotateAmmount:float = 5;
    8.  
    9.     private var _euler:Vector3;
    10.  
    11.     //reflection performance boost
    12.     private var _transform:Transform;
    13.     function Start()
    14.     {
    15.         _transform = transform;
    16.         //we must get the initial rotation for the euler
    17.         _euler = _transform.rotation.eulerAngles;
    18.     }
    19.  
    20.     function Update()
    21.     {
    22.         if (Input.GetKey(KeyCode.G))
    23.         {
    24.             _euler.x += ReverseRotation ?  RotateAmmount : -RotateAmmount;
    25.         }
    26.         if (Input.GetKey(KeyCode.H))
    27.         {
    28.             _euler.x -= ReverseRotation ?  RotateAmmount : -RotateAmmount;
    29.         }
    30.  
    31.         _euler.x = _euler.x < RotationMinimum ? RotationMinimum : _euler.x > RotationMaximum ? RotationMaximum : _euler.x;
    32.         _transform.rotation = Quaternion.Euler(_euler);
    33.     }
    34.  
    If there's anything you don't understand I'd be happy to explain it.

    The long line at 31 is this logic, just in case:

    Code (JavaScript):
    1.  
    2. if(_euler.x < RotationMinimum) { _euler.x = RotationMinimum; }
    3. else if(_euler.x > RotationMaximum) { _euler.x = RotationMaximum; }
    4. else { _euler.x = _euler.x; }
    5.  
     
    Last edited: Jul 28, 2014
unityunity