Search Unity

Rotate in certain direction

Discussion in 'Scripting' started by amirivala, Jan 28, 2015.

  1. amirivala

    amirivala

    Joined:
    May 29, 2013
    Posts:
    57
    Hi all,
    Im new to unity scripting and I've tried to write a code to rotate object with ease on it in the only certain Axis, and I've wrote a code but it has a bug thats after two 360 degree of rotation its start to rotate in other axis.
    I've tried to change the code but there was no luck
    can you please guide me and tell me how can i fix it?
    Thank you, really appreciate it
    Code (CSharp):
    1. void OnPress() {
    2.      
    3.         isDragging = true;
    4.     }
    5.  
    6.     void Update() {
    7.  
    8.      
    9.         if (Input.touchCount > 0 && isDragging) {
    10.             theSpeed = new Vector3(-Input.touches[0].deltaPosition.x, 0.0F, 0.0F);
    11.             avgSpeed = Vector3.Lerp(avgSpeed, theSpeed, Time.deltaTime * 5);
    12.         } else {
    13.             if (isDragging) {
    14.                 theSpeed = avgSpeed;
    15.                 isDragging = false;
    16.             }
    17.             float i = Time.deltaTime * lerpSpeed;
    18.             theSpeed = Vector3.Lerp(theSpeed, Vector3.zero, i);
    19.         }
    20.  
    21.         transform.Rotate(gameObject.transform.forward * theSpeed.x * rotationSpeed, Space.World);
    22. }
     
  2. amirivala

    amirivala

    Joined:
    May 29, 2013
    Posts:
    57
    Ive Found it, You have to change the
    Code (CSharp):
    1. transform.Rotate(gameObject.transform.forward * theSpeed.x * rotationSpeed, Space.World);
    to:
    Code (CSharp):
    1. transform.Rotate(0.0f,gameObject.transform.forward.y * theSpeed.x * rotationSpeed,0.0f, Space.World);
    Cheers
     
    draco_- likes this.