Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Calculating Y angle from 3D object

Discussion in 'Scripting' started by Nihao, Sep 17, 2014.

  1. Nihao

    Nihao

    Joined:
    Oct 22, 2012
    Posts:
    13
    Hi,
    I need to calculate a projection of a 3D object on a plane and then take the Y angle of it. Similarly as you would calculate the Y angle of an object in world's coordinates and then project it onto a map.

    Imagine an object rotated as this:
    upload_2014-9-17_13-49-29.png

    This is an image depicting the angle I am looking for.


    2dprojection.png

    Can you help me with this?
    Thank you
     
  2. Cpt Chuckles

    Cpt Chuckles

    Joined:
    Dec 31, 2012
    Posts:
    86
    there was an example of how to do this in the Angry Bots demo (which i don't think new versions of unity come with, i don't know where to find it but i haven't looked). it allowed you to find the angle between two vectors along an arbitrary axis represented by a third vector. but it looks like you just need the worldspace Y vector as the axis, so we don't need that.

    i would just create a copy of the transform.forward vector, reduce its Y component to 0, and then do Vector3.Angle(transformCopyWithoutY, Vector3.forward);

    now just remember that this fuction returns an absolute acute angle between the two vectors, it will not tell you if that's counter-clockwise or clockwise from forward. you can determine that by using a dot product between transformCopyWithoutY and Vector3.right, and if the result is + then it's clockwise, if it's - then it's counter-clockwise.
     
    Nihao likes this.
  3. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    First you should understand how to operate quaternions and vectors.

    If you multiply a quaternion by a vector, you rotate the vector.

    So knowing this, first multiply a quaternion with Y angle rotation to a forward vector with distance magnitude.

    Then, the result will multiply by rotation of the object to pass it orientation.

    If you also know that multiplying two quaternions get the sum of two rotations, you can do it all in one step, pre-multiplying the quaternions before multiplying the forward vector with distance magnitude.

    The final position is obtained by adding the position of the object.

    Hope this helps.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class temp : MonoBehaviour {
    5.  
    6.     public float distance = 1f;
    7.     public float YAngle = 25f;
    8.     public Transform projectPosition1step;
    9.     public Transform projectPosition2step;
    10.  
    11.  
    12.     void Update(){
    13.  
    14.         //One step
    15.         projectPosition1step.position = transform.position + (transform.rotation*Quaternion.Euler(0f,YAngle,0f))*(Vector3.forward*distance);
    16.  
    17.         //Two steps
    18.         Vector3 forward = Quaternion.Euler(0f,YAngle,0f) * (Vector3.forward * distance);
    19.         projectPosition2step.position = transform.position + transform.rotation * forward;
    20.  
    21.     }
    22.  
    23. }
     
    Nihao likes this.
  4. Nihao

    Nihao

    Joined:
    Oct 22, 2012
    Posts:
    13
    Yes, that's it. Had to modify it a bit this is the idea in general. Thanks!

    Thanks for the reply, got it working through Cpt Chuckles