x


Have an object follow mouse cursor in 3d space, like an Eyeball looking at the mouse

Hi everyone, I'm new at this stuff but am learning quickly. Basically, I have a (sphere) Eyeball in my scene. All I want to do, is have the eyeball track or look at the mouse when i wave it around the space. I realize i need to use 2d mouse movements with a 3d eyeball, but not sure where to begin. My goal is to have, on an iphone, the user drag their finger around the eyeball, and have the eye point wherever the user's finger is, simple enough.

I've used this script, but when i go vertically, the eye spins on the wrong axis, and never looks up or down. Thanks!

var position = Input.mousePosition;
    newposition = Vector3(position.x,position.y,-camera.main.transform.position.z);
    var lastposition = camera.main.ScreenToWorldPoint(newposition);
    transform.LookAt(lastposition);
}
more ▼

asked Mar 08 '12 at 11:07 PM

thelongdivision gravatar image

thelongdivision
1 1 1 2

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

2 answers: sort voted first

My "EyeBall" code. Look At Mouse :)

public class CustomController : MonoBehaviour {

    GameObject eye;

    void Start () {
       this.eye = GameObject.Find("MyEye");
    }

    void Update () {
       Vector3 pos = Camera.main.ScreenPointToRay(Input.mousePosition).GetPoint(20);
       Vector3 invertedMousePos = new Vector3(-pos.x * (float)0.2, -pos.y * (float)0.2, pos.z);
       this.eye.transform.LookAt(invertedMousePos);
    }
}
more ▼

answered Nov 12 '12 at 08:55 PM

Peu gravatar image

Peu
1

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

I just played around with this for a bit. Couldn't get it to work well with Camera.ScreenToWorldPoint(), so tried Camera.ScreenPointToRay() instead with much better results.

Something like this:

	var depth = 10;
	var pos = Camera.main.ScreenPointToRay(Input.mousePosition).GetPoint(depth);
	transform.LookAt(pos);
more ▼

answered Mar 09 '12 at 12:24 AM

rutter gravatar image

rutter
5.2k 2 11

(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:

x984
x724
x42

asked: Mar 08 '12 at 11:07 PM

Seen: 1532 times

Last Updated: Nov 12 '12 at 08:55 PM