Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How can I store transform.rotation z value in this case?

Discussion in '2D' started by DevGulpi, Aug 17, 2021.

  1. DevGulpi

    DevGulpi

    Joined:
    Aug 6, 2021
    Posts:
    17
    I want to save rotation z value when mouse button down and rotate while drag with angle(float).
    With this code, gameObject which I want to rotate's rotation z value is change right after click.
    How can I get z value?
    FYI, the value 'angle' is 0 when drag start.

    Code (CSharp):
    1. private void Update()
    2.     {
    3.         if (Input.GetMouseButtonDown(0))
    4.         {
    5.             zValue = transform.localEulerAngles.z;
    6.         }
    7.         if (Input.GetMouseButton(0))
    8.         {
    9.             transform.rotation = Quaternion.Euler(0, 0, zValue - angle);
    10.         }
    11.     }
     
  2. DevGulpi

    DevGulpi

    Joined:
    Aug 6, 2021
    Posts:
    17
    Never mind
    I changed my code like below and it works
    Code (CSharp):
    1. if (Input.GetMouseButtonDown(0))
    2.         {
    3.             zValue = transform.rotation.eulerAngles.z;
    4.         }
    5.         if (Input.GetMouseButton(0))
    6.         {
    7.             transform.eulerAngles = Vector3.forward * (zValue - angle)
    8.         }
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    Please note that this isn't related to 2D at all. A better place for such as post would be the scripting forum here.
     
  4. DevGulpi

    DevGulpi

    Joined:
    Aug 6, 2021
    Posts:
    17
    Oh I didn't think about that at all. Thank you for your advice.
     
    MelvMay likes this.