Skip to content

Instantly share code, notes, and snippets.

@Problematic
Created March 7, 2014 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Problematic/9422647 to your computer and use it in GitHub Desktop.
Save Problematic/9422647 to your computer and use it in GitHub Desktop.
Get the position on a circle for the direction you're aiming in Unity
Vector2 mousePos = _camera.ScreenToWorldPoint(Input.mousePosition);
Vector2 aimDirection = mousePos - (Vector2)_transform.position;
float aimAngle = Vector2.Angle(_transform.up, aimDirection);
if (Vector3.Cross(_transform.up, aimDirection).z > 0)
{
aimAngle = 360 - aimAngle;
}
Vector3 newPos = new Vector3(_transform.position.x + bulletSpawnRadius * Mathf.Sin(aimAngle * Mathf.Deg2Rad),
_transform.position.y + bulletSpawnRadius * Mathf.Cos(aimAngle * Mathf.Deg2Rad),
0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment