Search Unity

Transform rotate around is obsolete

Discussion in 'Scripting' started by ThisIsSparta, Jul 22, 2014.

  1. ThisIsSparta

    ThisIsSparta

    Joined:
    May 4, 2014
    Posts:
    142
    I'm having this issue...I need to translate this script with vector3 cause at the moment unity say is obsolete

    Code (CSharp):
    1. void Update () {
    2.        
    3.         if( down )
    4.         {
    5.             float rotationX = Input.GetAxis("CursorHorizontal") * sensitivity;
    6.             float rotationY = Input.GetAxis("CursorVertical") * sensitivity;
    7.             transform.RotateAroundLocal ( referenceCamera.up    , -Mathf.Deg2Rad * rotationX );
    8.             transform.RotateAroundLocal ( referenceCamera.right ,  Mathf.Deg2Rad * rotationY );
    9.  
    10.         }
    11.  
    12.         if( Input.GetMouseButtonDown( 0 ) )
    13.             down = true;
    14.         else if( Input.GetMouseButtonUp( 0 ) )
    15.             down = false;
    16.  
    17.     }
    18. }
    can someone be so kind to help me with this? I'm really strugglin'...thx in advance
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
  3. ThisIsSparta

    ThisIsSparta

    Joined:
    May 4, 2014
    Posts:
    142
    If I use just trasnform.rotate instead transfor rotate around local the rotation doesn't work anymore
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Read the link I posted.

    --Eric
     
  5. ThisIsSparta

    ThisIsSparta

    Joined:
    May 4, 2014
    Posts:
    142
    I read it believe me...but I suck so much at scripting that I just can't figure it out...I tried differnt settings but the object or didn't move or move very strangely...I don't know really how to translate that :( the complicate thing is like this camera reference...
     
  6. ThisIsSparta

    ThisIsSparta

    Joined:
    May 4, 2014
    Posts:
    142
    bump? anyoneplz?
     
  7. nesis

    nesis

    Joined:
    Jan 22, 2014
    Posts:
    6
    You want to use this version of the function suggested:
    Code (csharp):
    1.  
    2. void Rotate(Vector3 axis, float angle, SpacerelativeTo = Space.Self);
    3.  
    To do this, you need to change
    Code (csharp):
    1.  
    2. transform.RotateAroundLocal( referenceCamera.up   , -Mathf.Deg2Rad * rotationX )
    3. transform.RotateAroundLocal( referenceCamera.right ,  Mathf.Deg2Rad * rotationY );
    4.  
    To become
    Code (csharp):
    1.  
    2. transform.Rotate( referenceCamera.up, -rotationX, Space.World )
    3. transform.Rotate( referenceCamera.right, rotationY, Space.World );
    4.  
    You'll also want to make sure that you've set referenceCamera to the correct camera in the editor. Seeing it to the wrong camera could be making the strange rotation occur. Also, using Space.World instead of Space.Self (or for example using transform.Rotate(referenceCamera.up,-rotationX) without saying what version of Space you're using, since it defaults to Space.Self in that case) is important.

    Also, the
    Code (csharp):
    1. Mathf.Deg2Rad
    s needed to be removed, since the transform.Rotate() uses degrees instead of radians. This also explains why you weren't noticing any movement when simply putting in transform.Rotate() instead of transform.RotateAroundLocal(). Converting from degrees to radians is done by multiplying by PI/180, which is roughly 0.01745.

    I've also added in multiplying rotationX and rotationY by Time.deltaTime, since that's used to smooth out certain kinds of numbers that change value over time (like how velocity changes position over time, or how acceleration changes velocity over time).

    I've also tweaked another bit of your code that removes the down variable, since you can use Input.GetMouseButton(0) to tell whether the first mouse button is being held down.

    The properly functioning version of the code is here:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class RotateWithMouse : MonoBehaviour {
    6.    
    7.     public float sensitivity = 150.0f;
    8.     public Transform referenceCamera = null;
    9.  
    10.     void Start() {
    11.        
    12.         //Ensure the referenceCamera variable has a valid value before letting this script run.
    13.         //If the user didn't set a camera manually, try to automatically assign the scene's Main Camera.
    14.         if (!referenceCamera) {
    15.             if (!Camera.main) {
    16.                 Debug.LogError("No Camera with 'Main Camera' as its tag was found. Please either assign a Camera to this script, or change a Camera's tag to 'Main Camera'.");
    17.                 Destroy(this);
    18.                 return;
    19.             }
    20.             referenceCamera = Camera.main.transform;
    21.         }
    22.     }
    23.    
    24.    
    25.    
    26.     void Update () {
    27.        
    28.         if( Input.GetMouseButton(0) )
    29.         {
    30.             float rotationX = Input.GetAxis("Mouse X") * sensitivity * Time.deltaTime;
    31.             float rotationY = Input.GetAxis("Mouse Y") * sensitivity * Time.deltaTime;
    32.             transform.Rotate ( referenceCamera.up    , -rotationX, Space.World );
    33.             transform.Rotate ( referenceCamera.right ,  rotationY, Space.World );
    34.         }
    35.     }
    36. }
    37.  
     
    Last edited: Aug 1, 2014
    Gdoublee23 likes this.