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

Rotating a GameObject

Discussion in 'Scripting' started by Kevinpptx, Feb 24, 2020.

  1. Kevinpptx

    Kevinpptx

    Joined:
    Nov 30, 2019
    Posts:
    11
    Hi,
    I am new to coding with unity but I thought I would get a simple code like this to work but nevermind.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RotateByTouch : MonoBehaviour {
    6.  
    7.     public GameObject Objekt;
    8.     private Quaternion pos;
    9.  
    10.     // Update is called once per frame
    11.     void Update()
    12.     {
    13.         pos = Objekt.transform.rotation;
    14.        
    15.  
    16.         foreach (Touch touch in Input.touches) {
    17.  
    18.             if (touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary) // || or    | and
    19.             {
    20.                 if (pos.z.Equals(0))
    21.                 {
    22.                     pos.SetEulerRotation(0, 0, -45);
    23.                 }
    24.             }else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
    25.             {
    26.                 if (pos.z.Equals(-45))
    27.                 {
    28.                     pos.SetEulerRotation(0, 0, 0);
    29.                 }
    30.                
    31.             }
    32.         }
    33.     }
    34. }
    35.  
    I am trying to rotate a GameObject but it doesn't work.
     
  2. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    The most obvious thing here is that Quaternion is a struct, which means that it's copied by value and not reference. Or, to put it more plainly, lines 22 and 28 are modifying a copy of the rotation, and not the GameObject's rotation itself. You'll have to assign the rotation to the desired value like:
    Code (csharp):
    1. //do this after you set the value of pos:
    2. Objekt.transform.rotation = pos;
    3. //or just do it directly on lines 22/28:
    4. Objekt.transform.rotation = Quaternion.Euler(0,0,-45);
    Also, on lines 20 and 26, you're checking against the Z component of a rotation, which is not what you think it is. Quaternions have 4 numbers (x,y,z,w) and none of them are clear analogs to the X, Y, and Z that you see in the inspector. A little more reading about this here, but the short version is, don't use rotation.x if you haven't studied quaternions. rotation.eulerAngles.x is more like the number you want.

    But wait! There's more!
    You're currently checking to see if (after using the correction above) rotation.eulerAngles.x == 45. The numbers in the rotation object (like most numbers in Unity) are floating point numbers, and there's something important to know about floating point numbers: they are imprecise. What this means is that, with very rare exceptions, you should never use == (or .Equals) with floating point numbers, because if you do any math on them, then it can very easily be 44.9999999999, and give you a false negative. (Though it may not seem like you're "doing math" on this number, your Euler angles are being converted to a Quaternion and then back to a Euler number behind the scenes, which counts.) In a lot of cases you can check for a range, like:
    Code (csharp):
    1. if ( Mathf.Abs (rotation.eulerAngles.x - 45) < 0.001)
    However, there's an easier way in this case, because in this case you're basically just flipping between two possible states:
    Code (csharp):
    1. bool flipped = false;
    2. if (Input.touch blah blah blah) {
    3.    flipped = !flipped; //reverse the value between true and false
    4. }
    5. ...
    6. if (flipped) {
    7.    transform.rotation = Quaternion.Euler(-45,0,0);
    8. }
    9. else {
    10.    transform.rotation = Quaternion.Euler(0,0,0);
    11. }
     
    Kevinpptx likes this.
  4. Kevinpptx

    Kevinpptx

    Joined:
    Nov 30, 2019
    Posts:
    11
    Ok wow thank you so much! That is how much you can do wrong in 34 lines of code.
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Or put another way: this is how many lessons can be taught with 34 lines of code :)
     
    orionsyndrome and Kevinpptx like this.
  6. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    yeah, I agree with StarManta, also you could do a lot more wrong in 34 lines of code.

    in fact only one would be enough
    nuclearMissileRange.GetSilo(Random.Range(0, 16)).Launch(disallowAbort: true);

    just imagine 34 of them....