# Making a 3D Object the Cursor

Discussion in 'Editor & General Support' started by nickavv, Sep 6, 2007.

1. ### nickavv

Joined:
Aug 2, 2006
Posts:
1,801
How would I make a mesh follow the cursor, just along the X axis. Thank you!

2. ### MatthewW

Joined:
Nov 30, 2006
Posts:
1,356
This is what we've used. There's probably a simpler way:

Code (csharp):
1.
2.     /**
3.     * Where does our ray intersect a plane?
4.     *
5.     * From [url]http://www.gamespp.com/algorithms/collisionDetectionTutorial02.html[/url]
6.     * @param    ray
7.     * @param    planeNormal
8.     * @param    planeD
9.     */
10.     static function DistanceRayPlane(ray:Ray, planeNormal:Vector3, planeD:float)
11.     {
12.         var cosAlpha:float;
14.
15.         cosAlpha = Vector3.Dot(ray.direction, planeNormal);
16.
17.         if(cosAlpha == 0)
18.             return -1.0;
19.
20.         deltaD = planeD - Vector3.Dot(ray.origin, planeNormal);
21.
23.     }
24.
Code (csharp):
1.
2. function Update()
3. {
4.     var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
5.     var mouse = ray.GetPoint(Helpers.DistanceRayPlane(ray, Vector3.forward, 0));
6.
7.     transform.LookAt(mouse);
8. }
9.
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.

3. ### nickavv

Joined:
Aug 2, 2006
Posts:
1,801
That seems a little overly complicated to me, and it also gives me an "Unknown Identifier, Helpers"

4. ### User340

Joined:
Feb 28, 2007
Posts:
3,001
Try this:
Code (csharp):
1.
2. // CursorIcon.js
3. // Place this script onto the model you want to follow the cursor around.
4.
5. enum Type {X, Y, XandY}
6. var type = Type.XandY;
7.
8. // This is only used if you are using Type.Y.  It is the x position of it.
9. var xSpot = 0.0;
10.
11. // This is always used.
12. var depth = 0.0;
13.
14. // This is only used if you are using Type.X. It is the y position of it.
15. var height = 0.0;
16.
17. private var helperCollider : GameObject;
18. function Start ()
19. {
20.     Screen.showCursor = false;
21.     helperCollider = GameObject ("HelperCollider");
23.     helperCollider.collider.isTrigger = true;
24.     helperCollider.transform.localScale = Vector3 (50, 50, 0);
25.     helperCollider.transform.parent = camera.main.transform;
26.     helperCollider.transform.position = Vector3 (0, 0, depth);
27.     if (collider)
28.         Destroy (collider);
29. }
30.
31. function Update ()
32. {
33.     var ray : Ray = camera.main.ScreenPointToRay (Input.mousePosition);
34.     var hit : RaycastHit;
35.     if (Physics.Raycast (ray, hit))
36.     {
37.         if (hit.collider == helperCollider.collider)
38.         {
39.             if (type == Type.X)
40.                 transform.position = Vector3 (hit.point.x, height, depth);
41.
42.             else
43.                 if (type == Type.Y)
44.                     transform.position = Vector3 (xSpot, hit.point.y, depth);
45.
46.             else
47.                 transform.position = Vector3 (hit.point.x, hit.point.y, depth);
48.         }
49.     }
50. }

Joined:
Aug 2, 2006
Posts:
1,801
Thanks dan.

6. ### Jessy

Joined:
Jun 7, 2007
Posts:
7,325
This is what I used for my cake deal:

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

Code (csharp):
1. var distanceFromCamera : float;
2.
3. private var X : int;
4. private var Y : int;
5.
6. // position a 3D object where the mouse cursor would be
7. function Update () {
8.      Screen.showCursor = false;
9.      X = Input.mousePosition.x;
10.      Y = Input.mousePosition.y;
11.      transform.position = camera.main.ScreenToWorldPoint(Vector3(X,Y,distanceFromCamera));
12. }
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.

7. ### MatthewW

Joined:
Nov 30, 2006
Posts:
1,356
Put the DistanceRayPlane() function in a "Helpers" script.

8. ### User340

Joined:
Feb 28, 2007
Posts:
3,001
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:

Code (csharp):
1. enum Type {X, Y, XandY}
2. var type = Type.XandY;
3.
4. // This number ranges between 0 and 1.
5. // 1 is at the right of the screen, and 0 is a the left of the screen.
6. // This is only used if you are using Type.Y.
7. var xOffset = 0.5;
8.
9. // This number ranges between 0 and 1.
10. // 1 is at the top of the screen, and 0 is a the bottom of the screen.
11. // This is only used if you are using Type.X.
12. var height = 0.5;
13.
14. // This is the z distance from the camera.
15. var depth = 10.0;
16.
17. function Start ()
18. {
19.     Screen.showCursor = false;
20. }
21.
22. function Update ()
23. {
24.     var x = Input.mousePosition.x;
25.     var y = Input.mousePosition.y;
26.     var mainCam = camera.main;
27.     if (type == Type.X)
28.         transform.position = mainCam.ScreenToWorldPoint (Vector3 (x, height * Screen.height, depth));
29.
30.     else
31.         if (type == Type.Y)
32.             transform.position = mainCam.ScreenToWorldPoint (Vector3 (0, y, depth));
33.
34.     else
35.         transform.position = mainCam.ScreenToWorldPoint (Vector3 (x, y, depth));
36.
37.     height = Mathf.Clamp01 (height);
38.     xOffset = Mathf.Clamp01 (xOffset);
39. }

9. ### Jessy

Joined:
Jun 7, 2007
Posts:
7,325
Whoah! Nice!

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