Look_at not looking at mouse position

Godot Version

4.2 Stable


I’m making a game, using 3D but with a 2D view and want the gun to aim where the mouse cursor is, I’m casting a ray from the camera using event EventInputMouseMotion and the look_at() function, however, the gun doesn’t really follow the cursor and depending on the mouse position it will actually aim the wrong way.

What am I doing wrong?

The code that rotates the weapon:

var from = camera.project_ray_origin(event.position)
var to = from + camera.project_ray_normal(event.position) * ray_length
gun.look_at(Vector3.UP, to)

A look at what it does:


Don’t use raycast. Simply rotate the pistol model node along two axes relative to the global position of the pistol and the global coordinates of the mouse

1 Like

That did the trick, thank you!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.