0 votes

I want to make eyes that stay in sockets and Follow the mouse pos ..
As if the character is looking at the mouse.. (not angle but pos)
I have no idea how to get this done.. plz help :(

in Engine by (227 points)
edited by

1 Answer

+2 votes
Best answer

First, you need a sprite for eye that will be able to visibly rotate (so e.g. if you set rotation to specific value, the eye will look at that direction).

Then it's simple vector math:

#eye variable should be set to your eye sprite node
eye.rotation = (get_viewport().get_mouse_position() - eye.position).angle()

If you move your camera, this may not always work properly, so instead it could look like this:

#camera variable should be set to your current camera
var mouse_position = get_viewport().get_mouse_position() + camera.global_position
eye.rotation = (mouse_position - eye.global_position).angle()
by (141 points)
selected by

I dont want the rotation though, i want the eyeball to move out of centre to the edges, like, 50pixels lets say and go up, if mouse is above, go left if mouse is to the left.. : 0

Well, the process is the same. Either make the eye a child of node that will rotate, and if the eye is off center of the parent, it will move along edges. Or use normalized rotation vector to set the position. Something like:

var direction = (get_viewport().get_mouse_position() - eye.position).normalized()
position = direction * distance

where distance is how far you want the eye to be from center.

Thanks it works like a charm! <3

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.