Search Unity

Setting rotation? why is it so needlessly confusing?

Discussion in 'Editor & General Support' started by MosquitoByte, Mar 11, 2020.

  1. MosquitoByte

    MosquitoByte

    Joined:
    Sep 17, 2018
    Posts:
    213
    I need help setting the rotation of an object. Basically i am using a polygon collider as the view range for an enemy in a stealth game. I need to set its rotation so that it matches up with the way the enemy is moving/facing. Of course, with unity being unity, it cant just be as simple as transform.rotation.z = 90,180, etc. because that throws up an error (Of course it does). So what should i do instead?
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    You have to set the entire Vector3. So instead of
    Code (csharp):
    1. transform.rotation.z = 90
    write
    Code (csharp):
    1. transform.rotation = new Vector3(transform.rotation.x, transform.rotation.y,90);
    or something like that.
     
  3. MosquitoByte

    MosquitoByte

    Joined:
    Sep 17, 2018
    Posts:
    213
    this throws up the error "Cannot implicitly convert type 'UnityEngine.Vector3' to 'UnityEngine.Quaternion'."
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    Oops. It should be

    transform.eularAngles =new Vector3(transform.eularAngles.x, transform.eularAngles.y,90);


    Edit: by the way, the term "Eular Angles" is the name for how we are how we are familiar with seeing a rotation. That's a rotation expressed in terms of 3 angles of rotation (one for each axis). when you see the "Rotation" of an object in the inspector, what you are actually seeing is the transform.localEularAngles.

    The transform.rotation property is the rotation expressed as a quaternion, which is a bit more higher-math esoteric. Don't attempt to update the transform.rotation directly unless you know how quaternions work (I sure don't). Use the transform.eularAngles or transform.localEularAngles instead.
     
    Last edited: Mar 11, 2020
  5. MosquitoByte

    MosquitoByte

    Joined:
    Sep 17, 2018
    Posts:
    213
    Did i screw it up somehow? its telling me "Transform does not contain a definition for eularAngles and no accessible extension method 'eularAngles' accepting a first arguement of type 'Transform' could be found (are you missing a using directive or an assembly reference?)"

    Edit: nvm, it wants it to be spelled euler rather than eular, my bad
     
    Last edited: Mar 11, 2020
  6. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639