Search Unity

Finding the angle between a point on a circle and the circle's center

Discussion in 'Scripting' started by GamaGui, May 28, 2012.

  1. GamaGui

    GamaGui

    Joined:
    Apr 21, 2012
    Posts:
    15
    I want to be able to tap somewhere on a circle and calculate the angle of that tap in relation to the circle's center (using a sphere collider). Is there some convenient code that would help with this?
    I looked at this code but am not sure if it will help much...

    Code (csharp):
    1. var target : Transform;
    2. function Update () {
    3.     var targetDir = target.position - transform.position;
    4.     var forward = transform.forward;
    5.     var angle = Vector3.Angle(targetDir, forward);
    6.     if (angle < 5.0)
    7.         print("close");
    8. }
     
    unity_GfVYf6zRZIZEbg likes this.
  2. GamaGui

    GamaGui

    Joined:
    Apr 21, 2012
    Posts:
    15
    It looks like this might be doing the trick:

    Code (csharp):
    1. var angle = 0.0;
    2. angle = Mathf.Atan2(hit.point.y-object.transform.position.y, hit.point.x-object.transform.position.x)*180 / Mathf.PI;
     
    unity_GfVYf6zRZIZEbg likes this.