I am pretty new to Godot and have had a few fun experiments with testing out how the engine works but seems to have run into a problem with the camera that has me absolutly stumped. My camera can rotate freely and even zoom in/out but it seems to ignore the direction it is facing and WASD always move irrelevent of the camera facing direction. I have downloaded other peoples templetes to compare with mine to see where I went off, but I couldnt figure it out. The biggest difference seems to be the fact im referanceing my camera in the @onready when noone else is and I am wondering if that is what is messing with things? I tried messing with it, but didnt do much other then break everything. Im not particularly in a rush but im curious what other people think. Cheers to the camera code.
extends Node3D
@onready var camera = $Camera
@export_range(0.0,1.0) var sensitivity=.25
##camera work
var camera_speed:=10
var mouse_position= Vector2(0.0,0.0)
var total_pitch=0.0
## zoom
var desiredFov=70
var fovStep=3
var maxFov=130
var minFov=45
func _ready():
$"/root/Global".register_player(self)
func _process(delta:float)->void:
_update_mouselook()
_update_movement(delta)
func _update_movement(delta):
var mov=Vector3()## mov sets the speed
var move_direction=Vector3.ZERO
##WASD camera movement
mov.x = calculate_movement("move_right", "move_left") * delta
translate(basis.z.normalized() * mov.x)
mov.z = calculate_movement("move_backward", "move_forward") * delta
translate(basis.x.normalized() * mov.z)
position += mov * camera_speed
func _input(event):
##zoom
if event.is_action_pressed("camera_zoom_in"):
desiredFov = desiredFov - fovStep
desiredFov = clamp(desiredFov, minFov, maxFov)
camera.fov = desiredFov
elif event.is_action_pressed("camera_zoom_out"):
desiredFov = desiredFov + fovStep
desiredFov = clamp(desiredFov, minFov, maxFov)
camera.fov = desiredFov
if event is InputEventMouseMotion:## Receives mouse motion
mouse_position = event.relative
if event is InputEventMouseButton:## Receives mouse button input
match event.button_index:
MOUSE_BUTTON_RIGHT: # Only allows rotation if right click down
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED if event.pressed else Input.MOUSE_MODE_VISIBLE)
func _update_mouselook():
# Only rotates mouse if the mouse is captured
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
mouse_position *= sensitivity
var yaw = mouse_position.x
var pitch = mouse_position.y
# Prevents looking up/down too far
pitch = clamp(pitch, -90 - total_pitch, 90 - total_pitch)
total_pitch += pitch
rotate_y(deg_to_rad(-yaw))
rotate_object_local(Vector3(1,0,0), deg_to_rad(-pitch))
func calculate_movement(axis1,axis2):
return Input.get_action_strength(axis1)-Input.get_action_strength(axis2)
This appears to be the core of my problem. I now know that my camera is taking Z and X axis and is only moving based on those imputs (completely ignoring the rotation of the camera). I want it to actually update based on the direction the camera is facing instead. I will look for info related to that information, in the mean time ill try setting up the WASD to the rotation instead of the position? honestly, very new lol.
I compltely overhauled my code. It is closer, but still doing something weird. I gave my camera a node to look at to see if that would change anything. 1- it did, but only if I didnt rotate the camera at all, The moment I rotate it a little the WASD completely ignore the height and just either go straight into the ground or really high. the WASD+ side to side rotation works completely normally though for me. Just 1 last hurdle
extends Node3D
##Setting up camera to call itself
@onready var camera_unique = $"."
@onready var camera = $Camera3D
##Setting up exports to adjust the camera
@export_range(0,100,01.) var camera_move_speed:float=30
@export_range(0,10,.1) var camera_velocity:float=5
@export_range(0.0,1.0)var mouse_sensitivity=.25
@export_range(0,10,0.1) var camera_rotation_speed:float=.20
@export_range(0,10,1) var camera_rotation_x_min:float= -1.20 #this is based on radians and not degrees
@export_range(0,10,1) var camera_rotation_x_max:float= 1.10
var camera_height=get_position()
var velocity=Vector3()
var mouse_position=Vector2()
var mouse_pitch=0
var camera_rotation_direction:int=0
##Setting up variables for the script to adjust and run off of
var camera_can_process:bool=true #turns camera itself on/off
var camera_can_move:bool=true #camera movement on/off
var camera_can_zoom:bool=true #camera zoom on/off
var camera_can_rotate:bool=true #camera roata
##Start conditions
func _ready()->void:
camera.set_position(Vector3(0,3,0)) #set height of camera when loading in
camera.set_rotation(Vector3(deg_to_rad(-45),0,0)) #set rotation of camera when loading in
##run these process contintously
func _process(delta:float)->void:
if !camera_can_process:return #if fail to get camera do nothing?
camera_move(delta) #calls the camera move function (enables it)
camera_zoom(delta) #calls the zoom function (enables it)
camera_rotate(delta) #calls camera rotate (enables it)
#input commands
func _unhandled_input(event:InputEvent)->void:
if event is InputEventMouseMotion: #looking for mouse movement
mouse_position=event.relative #mouse position is now relative
if event is InputEventMouseButton: #looking for mouse button clicks
match event.button_index:
MOUSE_BUTTON_RIGHT: #only looking for right mouse click
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED if event.pressed else Input.MOUSE_MODE_CONFINED)
##Movement
#WASD camera movement controls
func camera_move(delta:float)->void:
if !camera_can_move:return
var velocity_direction_facing:Vector3=Vector3.ZERO
if Input.is_action_pressed("move_forward"):velocity_direction_facing-=transform.basis.z
if Input.is_action_pressed("move_backward"):velocity_direction_facing+=transform.basis.z
if Input.is_action_pressed("move_right"):velocity_direction_facing+=transform.basis.x
if Input.is_action_pressed("move_left"):velocity_direction_facing-=transform.basis.x
position+=velocity_direction_facing.normalized()*delta*camera_move_speed
#fuctional, need to see about adding/taking anything away
##Zoom
func camera_zoom(delta:float)->void:
if !camera_can_zoom:return
var velocity_direction_facing:Vector3=Vector3.ZERO
if Input.is_action_pressed("camera_zoom_in") or Input.is_action_just_pressed("camera_zoom_in"):
velocity_direction_facing-= transform.basis.z
if Input.is_action_pressed("camera_zoom_out") or Input.is_action_just_pressed("camera_zoom_out"):
velocity_direction_facing+= transform.basis.z
position+=velocity_direction_facing.normalized()*delta*camera_move_speed
#need zoom acceleration/deacceleration and min/max zoom
##Rotation
func camera_rotate(delta:float)->void:
if !camera_can_rotate:return
camera_rotate_left_right(delta,camera_rotation_direction*camera_rotation_speed)
func camera_rotate_left_right(delta:float,dir:float)->void:
rotation.y+=dir*camera_rotation_speed*delta
if !camera_can_rotate:return
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
mouse_position *= mouse_sensitivity
var yaw = mouse_position.x
var pitch = mouse_position.y
mouse_position = Vector2(0, 0)
# Prevents looking up/down too far
pitch = clamp(pitch, -90 - mouse_pitch, 90 - mouse_pitch)
mouse_pitch += pitch
rotate_y(deg_to_rad(-yaw))
rotate_object_local(Vector3(1,0,0), deg_to_rad(-pitch))
rotation.y+=dir*camera_rotation_speed*delta
var new_rotation_x:float=camera_unique.rotation.x
new_rotation_x-=dir*delta*camera_rotation_speed
new_rotation_x=clamp(new_rotation_x,camera_rotation_x_min,camera_rotation_x_max)
camera_unique.rotation.x=new_rotation_x