Search Unity

Reset GameObject's Transform Value

Discussion in 'Scripting' started by Cleo_willo876, Jan 18, 2020.

  1. Cleo_willo876

    Cleo_willo876

    Joined:
    May 18, 2017
    Posts:
    10
    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)

    Code (CSharp):
    1. public class DevMovement : MonoBehaviour
    2. {
    3.     public CharacterController controller;
    4.  
    5.     public float speed = 12f;//movement speed
    6.  
    7.     public float rise = 1f;//for making the User rise from the floor
    8.  
    9.     public float rotate = 1f;//responsible for rotating camera
    10.  
    11.     public GameObject userBody;
    12.  
    13.    
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         float x = Input.GetAxis("Side");
    19.         float z = Input.GetAxis("Forward");
    20.         float y = Input.GetAxis("UP"); // For moving the user up and down off the floor
    21.         float rotateZ = Input.GetAxis("Rotate"); //Rotates object on the z axis
    22.  
    23.         Vector3 move = transform.right * x + transform.forward * z;
    24.         controller.Move(move * speed * Time.deltaTime);
    25.  
    26.         transform.Translate(0, rise * y * Time.deltaTime, 0f);//for Rising off the ground & vice-versa
    27.         transform.Rotate(0, 0, rotate * rotateZ * Time.deltaTime);//Rotates the camera
    28. // `````````````````````````````````````````````````````````````````````````````````````````````````
    29.         //Problem zone
    30.         if (Input.GetKeyDown(KeyCode.R))//Button doesn't register the reset action
    31.         {
    32.             userBody.transform.Rotate(0, 0, 0);//Reset the camera's rotation
    33.         }
    34.  
    35.     }
    36. }
    I appreciate all the help in advance
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Add a debug inside the input call to make sure it's not working.
     
  3. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,322
    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.

    Code (CSharp):
    1. userBody.transform.localEulerAngles = Vector3.zero;
     
    nobluff67 and Cleo_willo876 like this.
  4. Cleo_willo876

    Cleo_willo876

    Joined:
    May 18, 2017
    Posts:
    10
    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
     
    Last edited: Jan 19, 2020
  5. Cleo_willo876

    Cleo_willo876

    Joined:
    May 18, 2017
    Posts:
    10
    Hey, so what do I do then if debug.log does return true then?
     
  6. Cleo_willo876

    Cleo_willo876

    Joined:
    May 18, 2017
    Posts:
    10
    Ok, Everyone! I've resolved the issue!

    Code (CSharp):
    1. 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:
    https://answers.unity.com/questions/1195593/how-to-get-rotation-of-an-object-using-c.html

    @SisusCo your code helped a lot.
     
    SisusCo likes this.
  7. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,322
    No problem, glad to hear you were able to find a solution!