Created
December 6, 2018 23:12
-
-
Save TheCuttlefish/64f3e208a10f8bf59c6fad25fcaac88f to your computer and use it in GitHub Desktop.
Look at 2D Unity (object and 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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class LookAt2d : MonoBehaviour { | |
public GameObject target; | |
void LookAtObject () { | |
transform.right = transform.position - target.transform.position; | |
} | |
void LookAtMouse () { | |
var camZ = Mathf.Abs (Camera.main.transform.position.z); | |
var mousePos = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, camZ); | |
mousePos = Camera.main.ScreenToWorldPoint (mousePos); | |
transform.right = (transform.position - mousePos); | |
} | |
void Update () { | |
//LookAtObject (); | |
LookAtMouse (); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome using "transform.right" to set the direction haha