Last active
October 21, 2016 06:58
-
-
Save yoshapihoff/b958a866ac27fbfa1285b56ec3483a21 to your computer and use it in GitHub Desktop.
Normal 2d game crosshair rotation by mouse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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