# Making a 3D Object the Cursor

How would I make a mesh follow the cursor, just along the X axis. Thank you!

This is what we’ve used. There’s probably a simpler way:

``````	/**
* Where does our ray intersect a plane?
*
* From [url]http://www.gamespp.com/algorithms/collisionDetectionTutorial02.html[/url]
* @param	ray
* @param	planeNormal
* @param	planeD
*/
static function DistanceRayPlane(ray:Ray, planeNormal:Vector3, planeD:float)
{
var cosAlpha:float;

cosAlpha = Vector3.Dot(ray.direction, planeNormal);

if(cosAlpha == 0)
return -1.0;

deltaD = planeD - Vector3.Dot(ray.origin, planeNormal);

}
``````
``````function Update()
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var mouse = ray.GetPoint(Helpers.DistanceRayPlane(ray, Vector3.forward, 0));

transform.LookAt(mouse);
}
``````

That’ll move an object around with Z=0.

The simpler solution is to do a raycast to a collider, but that can depend on your setup. I’ve found doing things purely mathematically is more portable.

That seems a little overly complicated to me, and it also gives me an “Unknown Identifier, Helpers”

Try this:

``````// CursorIcon.js
// Place this script onto the model you want to follow the cursor around.

enum Type {X, Y, XandY}
var type = Type.XandY;

// This is only used if you are using Type.Y.  It is the x position of it.
var xSpot = 0.0;

// This is always used.
var depth = 0.0;

// This is only used if you are using Type.X. It is the y position of it.
var height = 0.0;

private var helperCollider : GameObject;
function Start ()
{
Screen.showCursor = false;
helperCollider = GameObject ("HelperCollider");
helperCollider.collider.isTrigger = true;
helperCollider.transform.localScale = Vector3 (50, 50, 0);
helperCollider.transform.parent = camera.main.transform;
helperCollider.transform.position = Vector3 (0, 0, depth);
if (collider)
Destroy (collider);
}

function Update ()
{
var ray : Ray = camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit))
{
if (hit.collider == helperCollider.collider)
{
if (type == Type.X)
transform.position = Vector3 (hit.point.x, height, depth);

else
if (type == Type.Y)
transform.position = Vector3 (xSpot, hit.point.y, depth);

else
transform.position = Vector3 (hit.point.x, hit.point.y, depth);
}
}
}
``````

Thanks dan.

This is what I used for my cake deal:

http://forum.unity3d.com/viewtopic.php?t=6431

``````var distanceFromCamera : float;

private var X : int;
private var Y : int;

// position a 3D object where the mouse cursor would be
function Update () {
Screen.showCursor = false;
X = Input.mousePosition.x;
Y = Input.mousePosition.y;
transform.position = camera.main.ScreenToWorldPoint(Vector3(X,Y,distanceFromCamera));
}
``````

Instead of using Input.mousePosition.y, just insert the height up the game window you want the object to be. I would probably recommend using ViewportToWorldPoint so that it looks basically the same at all resolutions.

This is the easiest method I know of. Let me know if I can help out more.

Put the DistanceRayPlane() function in a “Helpers” script.

Jessy’s script is probably better than mine. I use an unnessasy collider.

Here’s a version of Jessy’s that I modifyed so you can choose wether you want it to follow on a certain mouse axis:

``````enum Type {X, Y, XandY}
var type = Type.XandY;

// This number ranges between 0 and 1.
// 1 is at the right of the screen, and 0 is a the left of the screen.
// This is only used if you are using Type.Y.
var xOffset = 0.5;

// This number ranges between 0 and 1.
// 1 is at the top of the screen, and 0 is a the bottom of the screen.
// This is only used if you are using Type.X.
var height = 0.5;

// This is the z distance from the camera.
var depth = 10.0;

function Start ()
{
Screen.showCursor = false;
}

function Update ()
{
var x = Input.mousePosition.x;
var y = Input.mousePosition.y;
var mainCam = camera.main;
if (type == Type.X)
transform.position = mainCam.ScreenToWorldPoint (Vector3 (x, height * Screen.height, depth));

else
if (type == Type.Y)
transform.position = mainCam.ScreenToWorldPoint (Vector3 (0, y, depth));

else
transform.position = mainCam.ScreenToWorldPoint (Vector3 (x, y, depth));

height = Mathf.Clamp01 (height);
xOffset = Mathf.Clamp01 (xOffset);
}
``````

Whoah! Nice!

Hopefully I’ll get a chance to try that out soon myself!