# How do i make my 3d character to move to the direction of the camera (FPS game)

Attention Topic was automatically imported from the old Question2Answer platform.

var gravity = 9.8
var location = Vector3()
var capncrunch = Vector3()
export var speed = 7
export var jump_height = 9
var velocity = Vector3()

func get_input():
velocity = Vector3()
if Input.is_action_pressed(‘ui_right’):
velocity.x += 1
if Input.is_action_pressed(‘ui_left’):
velocity.x -= 1
if Input.is_action_pressed(“ui_forward”):
velocity.z -= 1
if Input.is_action_pressed(“ui_backwards”):
velocity.z += 1
velocity = velocity.normalized() * speed

func _physics_process(delta):
get_input()
velocity = move_and_slide(velocity)
move_and_slide(capncrunch, Vector3.UP)

``````if Input.is_action_just_pressed("jump"):
if is_on_floor():
capncrunch.y = jump_height

if not is_on_floor():
capncrunch.y -= gravity * delta
move_and_slide(capncrunch, Vector3.UP)
``````

Use `global_transform.basis.xform(Vector3)` to get a local vector.

``````move_and_slide(global_transform.basis.xform(capncrunch), Vector3.UP)

#or for a third person camera
move_and_slide(Camera.global_transform.basis.xform(capncrunch), Vector3.UP)
``````

it didn’t work just when i pressed a button to move forward it would just move north not actually where the character is facing

James122333 | 2020-11-19 18:22

Is the camera a child rotating in the kinematicbody parent? You need to reference the node want to move relative to.

Magso | 2020-11-19 18:25

Yes it is but ive been stuck on this problem for hours

James122333 | 2020-11-19 19:20

Here’s the code I use:

``````if Input.is_action_pressed("move_forward"):
direction += transform.basis.z
elif Input.is_action_pressed("move_backward"):
direction -= transform.basis.z
if Input.is_action_pressed("move_left"):
direction += transform.basis.x
elif Input.is_action_pressed("move_right"):
direction -= transform.basis.x
``````

Then I use the move_and_slide() function with direction(which is a vector3). Out of total curiosity why do you have a variable called capncrunch? =)

thank you so much!!! i retried it and it worked

James122333 | 2020-11-19 22:08

glad to hear it! I’m still curious as to why you named your variable after a breakfast cereal though, =D

Millard | 2020-11-20 00:19

it was in a random tutorial

James122333 | 2020-11-20 16:30