Search Unity

clamping my rotation

Discussion in 'Scripting' started by calisk, Mar 3, 2010.

  1. calisk

    calisk

    Joined:
    Oct 24, 2008
    Posts:
    81
    I have my characters rotation linked to the iphones accelerometer and currently it works perfectly.

    the problem is he can rotate 360 degrees in all directions.

    I'd like to clamp the rotations but the only way i can think of is Euler angels, since I'm very unfamiliar with quaternions an how they work.

    can anyone help me find some better more efficient ways to clamp the rotation?
     
  2. GamersHeaven

    GamersHeaven

    Joined:
    Feb 15, 2010
    Posts:
    88
    Do you want to restrict specific angles?
    Dont rotate on z or y angles?
    If thats the case so could you do something i did with my turret script :D

    Code (csharp):
    1.  
    2. /*
    3. Turret targetting uses a linecast to check if the turret have an -
    4. free line of sight towards the player.
    5. The turret and player must be set to ignore raycast ! in the layer panel in the inspector.
    6. RaycastHit = Used to get information back from a raycast.
    7. */
    8. private var hit : RaycastHit;
    9. var speed : int = 3;
    10.  
    11. function Update () { //main loop start
    12.  
    13.  var target = GameObject.FindWithTag("Player") ;
    14.   if (Physics.Linecast(transform.position, target.transform.position, hit)) {
    15.    // to se wath the ray hits = print ("Blocked by " + hit.collider.name);
    16.       }
    17.        else {
    18. // Put the barrel on the top of the model as this look at rotation is restricted to z axis !
    19. //Remove ( , Vector3.forward ) at the end so will you get an all axis smooth look at rotation.
    20. // Also remove  these ( newRotation.x = 0.0;   newRotation.y = 0.0;  )
    21. // Get the target rotation
    22. var newRotation = Quaternion.LookRotation(transform.position - target.transform.position, Vector3.forward);
    23. newRotation.x = 0.0;
    24. newRotation.y = 0.0;
    25. // Smoothly rotate towards the target .
    26. transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, speed*Time.deltaTime);  
    27. }
    28.  
    29. } // main loop end  //
    30.  
    Sorry for all the text statements! as iam still early in learning unity and its scripting :oops:
     
  3. calisk

    calisk

    Joined:
    Oct 24, 2008
    Posts:
    81
    more along the lines of i only want it to rotate 30 degree's below the starting x-axis and 70 degrees above the starting x- axis.
     
  4. GamersHeaven

    GamersHeaven

    Joined:
    Feb 15, 2010
    Posts:
    88
  5. calisk

    calisk

    Joined:
    Oct 24, 2008
    Posts:
    81
    thanks gamer, i've never used a boo script before, but it says on the wiki that it hsould work with java and c# so it should work easy enough.