Search Unity

Changing Transform.Rotate to LocalRotation

Discussion in 'Scripting' started by amirivala, Oct 23, 2014.

  1. amirivala

    amirivala

    Joined:
    May 29, 2013
    Posts:
    57
    hi, I'm new to unity coding and i have a problem
    i want to rotate my object with mouse but everything works perfect but its rotating on a wrong anchor, and as i think i should change the rotate to Transform.LocalRotation, I've search the google but i couldn't find my answer, can you please help me to make it work fine
    really appreciate it

    Here is my code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DragRotateSlowdownLocal : MonoBehaviour {
    5.    
    6.     public float rotationSpeed = 10.0F;
    7.     public float lerpSpeed = 1.0F;
    8.    
    9.     private Vector3 theSpeed;
    10.     private Vector3 avgSpeed;
    11.     private bool isDragging = false;
    12.     private Vector3 targetSpeedX;
    13.    
    14.     void OnPress() {
    15.        
    16.         isDragging = true;
    17.     }
    18.    
    19.     void Update() {
    20.  
    21.        
    22.         if (Input.touchCount > 0 && isDragging) {
    23.             theSpeed = new Vector3(-Input.touches[0].deltaPosition.x, Input.touches[0].deltaPosition.y, 0.0F);
    24.             avgSpeed = Vector3.Lerp(avgSpeed, theSpeed, Time.deltaTime * 5);
    25.         } else {
    26.             if (isDragging) {
    27.                 theSpeed = avgSpeed;
    28.                 isDragging = false;
    29.             }
    30.             float i = Time.deltaTime * lerpSpeed;
    31.             theSpeed = Vector3.Lerp(theSpeed, Vector3.zero, i);
    32.         }
    33.        
    34.         transform.Rotate(Camera.main.transform.up * theSpeed.x * rotationSpeed, Space.World);
    35.         //transform.Rotate(Camera.main.transform.right * theSpeed.y * rotationSpeed, Space.World);
    36.     }
    37. }