Created
August 11, 2014 02:52
-
-
Save Problematic/a6716407e91850ec56d9 to your computer and use it in GitHub Desktop.
Unity3D mousefollow script
This file contains 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 MouseFollow : MonoBehaviour | |
{ | |
new public Camera camera; | |
public float zValue = 0; | |
public enum CoordinateSystem | |
{ | |
World, | |
Screen, | |
Viewport | |
} | |
public CoordinateSystem convertTo; | |
new Transform transform; | |
void Awake() | |
{ | |
transform = GetComponent<Transform>(); | |
} | |
void Start() | |
{ | |
if (camera == null) | |
{ | |
camera = Camera.main; | |
} | |
} | |
void Update() | |
{ | |
Vector3 converted = Input.mousePosition; | |
if (convertTo == CoordinateSystem.World) | |
{ | |
converted = camera.ScreenToWorldPoint(converted); | |
} else if (convertTo == CoordinateSystem.Screen) | |
{ | |
// Input.mousePosition is already in screen space | |
} else if (convertTo == CoordinateSystem.Viewport) | |
{ | |
converted = camera.ScreenToViewportPoint(converted); | |
} | |
converted.z = zValue; | |
transform.position = converted; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment