Transform rotate around is obsolete

I’m having this issue…I need to translate this script with vector3 cause at the moment unity say is obsolete

void Update () {
       
        if( down )
        {
            float rotationX = Input.GetAxis("CursorHorizontal") * sensitivity;
            float rotationY = Input.GetAxis("CursorVertical") * sensitivity;
            transform.RotateAroundLocal ( referenceCamera.up    , -Mathf.Deg2Rad * rotationX );
            transform.RotateAroundLocal ( referenceCamera.right ,  Mathf.Deg2Rad * rotationY );

        }

        if( Input.GetMouseButtonDown( 0 ) )
            down = true;
        else if( Input.GetMouseButtonUp( 0 ) )
            down = false;

    }
}

can someone be so kind to help me with this? I’m really strugglin’…thx in advance

You can just use Transform.Rotate.

–Eric

If I use just trasnform.rotate instead transfor rotate around local the rotation doesn’t work anymore

Read the link I posted.

–Eric

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 :frowning: the complicate thing is like this camera reference…

bump? anyoneplz?

You want to use this version of the function suggested:

void Rotate(Vector3 axis, float angle, SpacerelativeTo = Space.Self);

To do this, you need to change

transform.RotateAroundLocal( referenceCamera.up   , -Mathf.Deg2Rad * rotationX )
transform.RotateAroundLocal( referenceCamera.right ,  Mathf.Deg2Rad * rotationY );

To become

transform.Rotate( referenceCamera.up, -rotationX, Space.World )
transform.Rotate( referenceCamera.right, rotationY, Space.World );

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 Mathf.Deg2Rads 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:

using UnityEngine;
using System.Collections;

public class RotateWithMouse : MonoBehaviour {
   
    public float sensitivity = 150.0f;
    public Transform referenceCamera = null;

    void Start() {
       
        //Ensure the referenceCamera variable has a valid value before letting this script run.
        //If the user didn't set a camera manually, try to automatically assign the scene's Main Camera.
        if (!referenceCamera) {
            if (!Camera.main) {
                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'.");
                Destroy(this);
                return;
            }
            referenceCamera = Camera.main.transform;
        }
    }
   
   
   
    void Update () {
       
        if( Input.GetMouseButton(0) )
        {
            float rotationX = Input.GetAxis("Mouse X") * sensitivity * Time.deltaTime;
            float rotationY = Input.GetAxis("Mouse Y") * sensitivity * Time.deltaTime;
            transform.Rotate ( referenceCamera.up    , -rotationX, Space.World );
            transform.Rotate ( referenceCamera.right ,  rotationY, Space.World );
        }
    }
}
1 Like