player movement based on camera rotation

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ArthurER

Hi, I am very beginner to Godot GDscript , and have little experience with writing script. I am trying out a first 3D project using tutorials. what I have is an animated character with script from a tutorial by Jayanam, a camera gimbal from kidscancode, and a simple floor scene with the character and camera instanced. it works , the character can move and the camera can be rotated. the camera is independent of the character.

what I am trying to do is to keep the camera independent but have the character move in relation to the y axis of the camera plane so if I rotate the camera the charterer will still move in the direction pressed within the camera view.( if I rotated the camera and ran forward the player would be running in circles)

the camera gimbal is made of two spacial nodes one to rotate y one to rotate x , then the camera (also the hierarchy) i believe that the node rotating y should define the adjustment to the characters movement but I am unsure how to do that. the rotation of the camera is functioning in global space ( the kidscancode tutorial had it in local but I have been experimenting)

I believe the relevant code is in the Jayanam tutorial for character movement

func _ready():
state_machine = $AnimationTree.get("parameters/playback")
camera = get_node("../CameraPlane/UpDown/Camera").get_global_transform()
player_cat = get_node(".")

the camera in that tutorial ended up child of character which is not really where I want to end up.

	if(Input.is_action_pressed("_right")):
	direction += camera.basis[0]
	is_moving = true
if(Input.is_action_pressed("_left")):
	direction += -camera.basis[0]
	is_moving = true
if(Input.is_action_pressed("_up")):
	direction += -camera.basis[2]
	is_moving = true
if(Input.is_action_pressed("_down")):
	direction += camera.basis[2]
	is_moving = true

func _physics_process(delta):
direction.y = 0
direction = direction.normalized()
get_input()
velocity.y += delta * gravity
var hv = velocity
hv.y = 0
var new_position = direction * SPEED
var _acceleration = DE_ACCELERATION
if (direction.dot(hv) > 0):
	_acceleration = ACCELERATION
hv = hv.linear_interpolate(new_position, _acceleration * delta)
velocity.x = hv.x
velocity.z = hv.z
velocity = move_and_slide(velocity, Vector3(0,1,0))
if is_moving:
	var angle = atan2(hv.x, hv.z)
	var player_rotation = player_cat.get_rotation()
	player_rotation.y = angle
	player_cat.set_rotation(player_rotation)

that code is somewhat beyond me, I believe that the direction determined by camera.basis is how the tutorial handled it with the camera following the player but will not work for what I am trying to do, I actually expected that the charterer movement would have went haywire with the change to the Camera.

I would appreciate a push in the right direction.

:bust_in_silhouette: Reply From: ArthurER

OK I got it figured out, I built a new camera scene with a first person type of camera movement, at first it did not work right either, but it was located differently in the floor scene and I noticed that the character started off moving in the correct directions even though the camera was at a new angle. the script had all the pieces to work but one of the pieces was just in the wrong place.

I moved

camera = get_node("../CameraPlane/UpDown/Camera").get_global_transform()

into func get_input(): so when an input event happens the variable is updated, it only took all day to figure out :-\ I must have missed part of the tutorial :slight_smile: