x


Getting a 2d Character to Follow the mouse

Im trying to get my charachter to follow my mouse cursor in a 2d like way. When the cursor is in front of him he looks to the front and if the cursor is in back he turns around. Right now all he does is turn and look at the camera like the mouse pointer is to the side of him. I had an idea of makeing a plane object and just having him transform.LookAt it but I cant get the plane to follow the mouse. So if anyone has any ideas please help. I have NO working code right now so I cant post what I have sorry. Thanks in advance.

more ▼

asked Nov 15 '11 at 01:01 AM

Tracey P gravatar image

Tracey P
113 18 18 24

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

4 answers: sort oldest

Have a look at the "TapControl" script that comes with the Penelope tutorial.

more ▼

answered Nov 15 '11 at 05:54 AM

seedoubleyou gravatar image

seedoubleyou
146 1

Its not an Ipod game

Nov 15 '11 at 11:22 PM Tracey P
(comments are locked)
10|3000 characters needed characters left

Um... Yes, but the code could give you some ideas about how to build a script that follows the mouse click....

(Basically, what I'd try is placing a Empty gameObject at the place on your mesh where the mouse clicks, then have your character lookAt that Empty gameObject position and move to it....)

more ▼

answered Nov 16 '11 at 02:57 AM

seedoubleyou gravatar image

seedoubleyou
146 1

no i dont want it on mouse click i want it always attached to mouse and i only want it to move up and down and left and right

Nov 16 '11 at 01:08 PM Tracey P
(comments are locked)
10|3000 characters needed characters left

Okay, I'm officially confused. I now think you're wanting the character to follow the mouse around, in which case you probably want to use a OnMouseOver function, but honestly, I'd have to see what code you are using to get some idea of what it is you're actually trying to do.

more ▼

answered Nov 16 '11 at 06:12 PM

seedoubleyou gravatar image

seedoubleyou
146 1

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

Has this question been answered already? , in case not to attach an object to your mouse is quite easy. Use Camera.mainCamera.ScreenToWorldPoint(screenPos);

Example code: private void attachObjectToMouse () { Vector3 screenPos = Input.mousePosition; screenPos.z = 5; Vector3 worldPos = Camera.mainCamera.ScreenToWorldPoint(screenPos); gameObject.transform.position = worldPos; }

Call the above function inside 'void Update ();' of the object you want to attach to the mouse. 'screenPos.z' defines the depth at which you want your object to be from the camera. i.e. in this case 5 units in front of my camera. Also make sure that this value is a minimum of 2-3 units greater than the 'near' value of your camera otherwise you wont see the object in your game scene. The above code will work only if the camera you are using is the default MainCamera. In case you wish to use your own camera replace 'Camera.mainCamera' according to your needs.

more ▼

answered Mar 29 '12 at 09:25 PM

Tahelu gravatar image

Tahelu
0

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

x5062
x982
x198
x12
x3

asked: Nov 15 '11 at 01:01 AM

Seen: 1166 times

Last Updated: Mar 29 '12 at 09:25 PM