Godot Version
Godot 4.2.2 mono
Question
Im trying to create a game with a top view, and orthographic 3D camera. I created a player, and gave him the ability to pivot on the mouse cursor, for shooting or just looking around. However, if player turn left, or right, there will be a mismatch between the mouse and the player’s direction. Thats looks like:
But, If rotate the mouse cursor up or down, everything will be fine, and the cursor will correspond to the line of sight.
Code of rotate:
var Ray = CameraScript.CheckRay()
if (Ray):
var mousePos = Ray.position
var plyPos = transform.origin
var drc = (mousePos - plyPos)
var front = transform.basis.z
var ang = Vector2(front.x, front.z).angle_to(Vector2(drc.x, drc.z))
var target_angle = atan2(drc.x, drc.z)
rotation.y = lerp_angle(rotation.y, target_angle, 4*delta)
CheckRay():
func CheckRay():
var mousepos = get_viewport().get_mouse_position()
var rayorg = project_ray_origin(mousepos)
var raydrc = project_ray_normal(mousepos)
var space_state = get_world_3d().direct_space_state
var params = PhysicsRayQueryParameters3D.create(rayorg, rayorg + raydrc * 1000)
var result = space_state.intersect_ray(params)
return result
And I also noticed one interesting feature: If put a box about 1-Y high, and point at its top, everything be is almost “normal”.
I’ve tried putting a fixed mousePos.y height, but it just doesn’t work. It doesn’t change in any way at all, only when hovering over the box. And, if i rotate the camera exactly -90 degrees Y, the cursor will perfectly match the mouse. But I’m making the game so that the camera is rotated -35 degrees Y, and I want to keep making it that way.
What can i do, for this fix that? I really don’t know what to do. (im used translator for this topic)