Reset GameObject's Transform Value

Hi there all, so I’m working on a movement and camera rotation system, that successfully rotates, however, when trying to reset it using the GameObject it’s from… nothing happens, could someone help me to resolve this please(script below)

public class DevMovement : MonoBehaviour
{
    public CharacterController controller;

    public float speed = 12f;//movement speed

    public float rise = 1f;//for making the User rise from the floor

    public float rotate = 1f;//responsible for rotating camera

    public GameObject userBody;

   

    // Update is called once per frame
    void Update()
    {
        float x = Input.GetAxis("Side");
        float z = Input.GetAxis("Forward");
        float y = Input.GetAxis("UP"); // For moving the user up and down off the floor
        float rotateZ = Input.GetAxis("Rotate"); //Rotates object on the z axis

        Vector3 move = transform.right * x + transform.forward * z;
        controller.Move(move * speed * Time.deltaTime);

        transform.Translate(0, rise * y * Time.deltaTime, 0f);//for Rising off the ground & vice-versa
        transform.Rotate(0, 0, rotate * rotateZ * Time.deltaTime);//Rotates the camera
// `````````````````````````````````````````````````````````````````````````````````````````````````
        //Problem zone
        if (Input.GetKeyDown(KeyCode.R))//Button doesn't register the reset action
        {
            userBody.transform.Rotate(0, 0, 0);//Reset the camera's rotation
        }

    }
}

I appreciate all the help in advance

Add a debug inside the input call to make sure it’s not working.

Transform.Rotate rotates the transform by the given amount, so rotating by 0 degrees would have no effect. Use Transform.localEulerAngles to reset the rotation instead.

userBody.transform.localEulerAngles = Vector3.zero;
3 Likes

Hey Thanks, it does reset the rotations, but it also resets the positions on the X and Z axis aswell. This at least gives me a lead that I can try scrummaging around to find the exact thing that will work. Thanks again!

Edit:

The issue I found was that it resets all axis rotation while the transform values remained the same, so now I am trying to write a piece of code that will keep the Y-axis rotation relative while resetting the Z-axis’s rotation

Hey, so what do I do then if debug.log does return true then?

Ok, Everyone! I’ve resolved the issue!

userBody.transform.localRotation = Quaternion.Euler(0,userBody.transform.rotation.eulerAngles.y,0);//Reset the camera's rotation in Z Axis

userBody is the game object that I need to keep it’s rotation on Y for, so by including it’s current position this way I was able to keep it relative while changing the Z just like I needed to.

Thanks alot @SisusCo

Based on this forum here(and numerous different tries and fails) I got the concept needed to complete the script:

@SisusCo your code helped a lot.

1 Like

No problem, glad to hear you were able to find a solution!