Skip to content

Instantly share code, notes, and snippets.

@TheCuttlefish
Created December 6, 2018 23:12
Show Gist options
  • Save TheCuttlefish/64f3e208a10f8bf59c6fad25fcaac88f to your computer and use it in GitHub Desktop.
Save TheCuttlefish/64f3e208a10f8bf59c6fad25fcaac88f to your computer and use it in GitHub Desktop.
Look at 2D Unity (object and mouse)
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 ();
}
}
@Ale10xp
Copy link

Ale10xp commented Apr 29, 2021

Awesome using "transform.right" to set the direction haha

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment