Skip to content

Instantly share code, notes, and snippets.

@Problematic
Created August 11, 2014 02:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Problematic/a6716407e91850ec56d9 to your computer and use it in GitHub Desktop.
Save Problematic/a6716407e91850ec56d9 to your computer and use it in GitHub Desktop.
Unity3D mousefollow script
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