x


How can I get an object on the Z-plane to follow the mouse cursor?

I know there's a simple answer to this I'm just not seeing. I've got a cube that I want to appear right behind the mouse cursor, constrained to the Z-plane (i.e. z=0). It'd basically be a 3D-rendered mouse pointer. What's the right way to do this?

I thought Camera.main.ScreenToWorldPoint(Input.mousePosition) would be key, but that gives me the same coordinates no matter where the mouse is.

more ▼

asked Apr 01 '11 at 02:03 AM

Capt_Swayworn gravatar image

Capt_Swayworn
2 1 1 2

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Here's my shot:

var cursor : Transform;

var horizontalSpeed = 10;
var verticalSpeed = 10;

function Update() {
    //Move the 3D cursor with the mouse

    var x : float = horizontalSpeed * Input.GetAxis ("Mouse X");
    var y : float = verticalSpeed * Input.GetAxis ("Mouse Y");

    cursor.transform.position = Vector3(cursor.transform.position.x+x, cursor.transform.position.y+y, cursor.transform.position.z);
}

Note: This will only move the cursor on the X and Y coordinates. It does not take into account anything else. You'll want to create a second camera to render only this cube. Make sure it's not rotated at all, and is located close to the cursor. You'll also want to play around with the initial speed values to get them just right.

Good luck, hope this helps somehow. :)

more ▼

answered Apr 01 '11 at 02:26 AM

e.bonneville gravatar image

e.bonneville
5.7k 100 116 165

I don't think this will keep the mouse right under the cursor (even if I get it calibrated perfectly on one computer, I imagine it might get different results on a second), but it's probably the best way to solve this problem. Thanks a bunch!

Apr 01 '11 at 10:47 PM Capt_Swayworn

That's true. If it works for you, would you mind accepting and upvoting my answer so that other users can find this solution easier? Thanks,

Apr 01 '11 at 11:08 PM e.bonneville

This is probably going to work for me, I'm looking for a Space Runner look. Hopefully I'm on the right track :) I'd upvote your answer if I could.

May 07 '12 at 06:14 PM moonpix
(comments are locked)
10|3000 characters needed characters left

I don't think this will keep the mouse right under the cursor (even if I get it calibrated perfectly on one computer, I imagine it might get different results on a second), but it's probably the best way to solve this problem. Thanks a bunch!

more ▼

answered Apr 01 '11 at 10:47 PM

Capt_Swayworn gravatar image

Capt_Swayworn
2 1 1 2

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x983
x37

asked: Apr 01 '11 at 02:03 AM

Seen: 1144 times

Last Updated: May 07 '12 at 06:14 PM