Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Get rotation angle between object and Z axis

Discussion in 'Physics' started by brodonza94, Apr 19, 2021.

  1. brodonza94

    brodonza94

    Joined:
    Apr 2, 2021
    Posts:
    4
    I'm working with Hololens 2 on Unity. Giving a point P, I would like to calculate the angle between the Z axis of my local coordinates system (where 0 is the origin: the position where I started the app) and this point P.
    NB: I don't want to get the angle based on the direction I'm facing, only between the axis and the vector3.
    Sorry, I know maybe it's an obvious question, but I'm a newbie of Unity.

    Screenshot_20210419-213827_Samsung Notes.jpg
     
    Last edited: Apr 19, 2021
  2. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
  3. brodonza94

    brodonza94

    Joined:
    Apr 2, 2021
    Posts:
    4
  4. supernameworld

    supernameworld

    Joined:
    Sep 14, 2015
    Posts:
    16
    You can pass in (0, 0, 1) and P.transform.position.

    But this is just dot product. You probably want to know the following fact for the rest of your game development life:
    dot(a, b) = (a.x * b.x + a.y * b.y + a.z * b.z) = length(a) length(b) cos(angle between a and b)

    so in this case you want
    arccos(dot((0, 0, 1), transform.position.normalized))

    which is just
    arccos(transform.position.normalized.z)
     
    brodonza94 likes this.
  5. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    As this is giving an angle between two vectors, you will probably want to supply (0,1) as the origin (i.e. a vector from the origin to y = 0 z = 1 and the position vector of the object (transform.position). The rotation of object P's transform will not affect this, only its position.

    Here is some sample code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AngleBetween : MonoBehaviour
    6. {
    7.     [SerializeField] Vector2 originDirection = new Vector2(0,1);
    8.  
    9.     private void Start()
    10.     {
    11.         Vector2 pos = new Vector2(transform.position.x, transform.position.z);
    12.         Debug.Log(Vector2.SignedAngle(originDirection, pos).ToString("F3"));
    13.     }
    14.  
    15. }
    Edit: This line, Vector2 pos = new Vector2(transform.position.x, transform.position.z); is just for clarity and ease of messing about with the values. You can, of course, use transform.position directly.

    Unfortunately, there is a known documentation error where it states the angle is clockwise, in fact the output follows mathematical convention with counter-clockwise being positive.

    If you want to look at dot products, be aware that you can get Unity to do the work for you using Vector2.Dot

    How much simplification you do depends on the type of output you need and how many quadrants you expect your object P to be in. For example, you can see in shelljump's arccos(transform.position.normalized.z) that you will get the same angle and sign regardless of whether x < 0 or x > 0.