How do I get my cameras y rotation to change the players rotation to it

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

I have some simple scripts I want to be able to change the players facing position when I press the up key and the camera is a different position I want the player to change to that direction!

This is my player script:

    extends KinematicBody

var velocity = Vector3(0,0,0)
const speed = 8
var gravity = Vector3.DOWN * 12
var jump = false

func _ready():
	pass 
	
func _physics_process(delta):
	get_input()
	move_and_slide(velocity, Vector3.UP)
	velocity += gravity * delta #Adds gravity to the player
	if jump and is_on_floor():
		velocity.y = 10
	jump = false
	
func get_input():
	if Input.is_action_pressed("Move Up") and Input.is_action_pressed("Move Down"):
		velocity.z = 0
	elif Input.is_action_pressed("Move Up"):
		velocity.z = -speed
		
	elif Input.is_action_pressed("Move Down"):
		velocity.z = +speed
	else:
		velocity.z = lerp(velocity.z, 0, 0.2)
	if Input.is_action_pressed("Move Left") and Input.is_action_pressed("Move Right"):
		velocity.x = 0
	elif Input.is_action_pressed("Move Left"):
		velocity.x = -speed
	elif Input.is_action_pressed("Move Right"):
		velocity.x = +speed
	else:
		velocity.x = lerp(velocity.x, 0, 0.2)
	if Input.is_action_just_pressed("Jump"):
		jump = true

This is my Camera script


    extends Spatial

var rotation_speed = 0.09
var max_zoom = 3.0
var min_zoom = 0.5
var zoom = 1.5
var zoom_speed = 0.09

func _ready():
	pass

func _physics_process(delta):
	$InnerGimbal.rotation.x = clamp($InnerGimbal.rotation.x, -1.4, 0.1)
	scale = lerp(scale, Vector3.ONE * zoom, zoom_speed)
	

func _unhandled_input(event):
	if event is InputEventMouseMotion and Input.is_action_pressed("Right_Mouse_Button"):
		rotate_y(deg2rad(-event.relative.x * rotation_speed))
	if event is InputEventMouseMotion and Input.is_action_pressed("Right_Mouse_Button"):
		$InnerGimbal.rotate_x(deg2rad(-event.relative.y * rotation_speed))
	if event.is_action_pressed("zoom_in"):
		zoom -= zoom_speed
	if event.is_action_pressed("zoom_out"):
		zoom += zoom_speed
	zoom = clamp(zoom, min_zoom, max_zoom)
:bust_in_silhouette: Reply From: klaas

Hi,
i think you want to build a FPS style camera/player system.

Just parent the camera setup to the player so its stays with them.
Then just turn the player instead of the camera when rotating around y axis.

and you can get the rotations of an object like so

var localRotation = object.rotation.y
#or in global relation
var globalRotation = object.global_transform.rotation.y

I should of been more specefic, the camera is a third person so when i move the camera i am able to see the front of the player but when I press the up key it moves to that direction!

Dragon20C | 2020-09-23 15:39

so, the player should run in the direction the camera is viewing

then you have to turn your player in the camera direction when pressed UP

is this what you want to do?

klaas | 2020-09-23 15:44

what is exactly is “player” in the line of code, when I put it into my move up script godot says it isnt declared in current scope

Dragon20C | 2020-09-23 15:47

player would be your player object. If you put this into a script of the player you have to …

my original code wont work so i removed it 

klaas | 2020-09-23 15:49

can you send me to some documentation, obviously I don’t understand, so I think I should start from there, thanks for replying and sorry for well… asking these questions.

Dragon20C | 2020-09-23 15:56

arg, ive told you rubbish. that not gonna work. try this

var camera

func _ready():
   camera = $path/to/camera 

func get_input():
    if Input.is_action_pressed("Move Up") and Input.is_action_pressed("Move Down"):
        velocity.z = 0
    elif Input.is_action_pressed("Move Up"):
        var cam_vector = camera.global_transform.origin
        cam_vector.y = global_transform.origin.y
        var away_vector = global_transform.origin + cam_vector.direction_to(global_transform.origin )
        look_at( away_vector )
        velocity.z = -speed

    elif Input.is_action_pressed("Move Down"):
        var cam_vector = camera.global_transform.origin
        cam_vector.y = global_transform.origin.y
        var away_vector = global_transform.origin + direction_to(cam_vector.global_transform.origin )
        look_at( away_vector )
        global_transform.rotation.y = -camera.global_transform.rotation.y
        velocity.z = +speed

klaas | 2020-09-23 16:16