Skip to content

Instantly share code, notes, and snippets.

@yoshapihoff
Last active October 21, 2016 06:58
Show Gist options
  • Save yoshapihoff/b958a866ac27fbfa1285b56ec3483a21 to your computer and use it in GitHub Desktop.
Save yoshapihoff/b958a866ac27fbfa1285b56ec3483a21 to your computer and use it in GitHub Desktop.
Normal 2d game crosshair rotation by mouse
using UnityEngine;
public class AimingExample : MonoBehaviour
{
public Transform Crosshair;
private Transform ThisTransform;
void Start ()
{
ThisTransform = GetComponent<Transform> ();
}
void Update ()
{
Crosshair.position = ThisTransform.position;
var mousePos = Input.mousePosition;
mousePos.z = 10f;
var thisTransformScreenPos = Camera.main.WorldToScreenPoint (ThisTransform.position);
mousePos = mousePos - thisTransformScreenPos;
float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
Crosshair.rotation = Quaternion.Lerp (Crosshair.rotation, Quaternion.Euler (0f, 0f, angle), FollowMouseSpeed * Time.deltaTime);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment