Search Unity

mouse angle

Discussion in 'Scripting' started by rfdarter, Aug 28, 2012.

  1. rfdarter

    rfdarter

    Joined:
    Aug 28, 2012
    Posts:
    2
    hey, im stuck by this problem quite a while now.
    I expect angles from 0 to 90 degrees, but i get something like 0 to 57 degrees.
    I can`t find a solution for my problem :-(

    Code (csharp):
    1. Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0.0f);
    2.  
    3. float angle = Vector3.Dot(mousePos.normalized, new Vector3(1.0f, 0.0f, 0.0f).normalized) * Mathf.Rad2Deg;
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Vector.Dot with normalized vectors will give you values between -1 and 1. But that is not the angle. It is the cosine of the angle:
    So you need the inverse of Cos to get the actual angle:
    And we got it. But you can also use Vector3.Angle (...).
     
  3. rfdarter

    rfdarter

    Joined:
    Aug 28, 2012
    Posts:
    2
    Hey, thanks.

    i feel a bit stupid now, because i had this not in mind, but thx for the fast and usefull reply ;-)