How to make the character move in a certain direction?

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

I have a simple code from the standard game for Godot 3:

extends KinematicBody
export var speed = 14
export var fall_acceleration = 75
export var jump_impulse = 20
var velocity = Vector3.ZERO
func _physics_process(delta):
var direction = Vector3.ZERO
if Input.is_action_pressed("move_right"):
if Input.is_action_pressed("move_left"):
if Input.is_action_pressed("move_back"):
	direction.z += 1
if Input.is_action_pressed("move_forward"):
	direction.z -= 1
if is_on_floor() and Input.is_action_just_pressed("jump"):
	velocity.y += jump_impulse
velocity.x = direction.x * speed
velocity.z = direction.z * speed
velocity.y -= fall_acceleration * delta
velocity = move_and_slide(velocity, Vector3(0,180,0))

How can I change it so that the character walks forward and backward depending on his direction?

:bust_in_silhouette: Reply From: magicalogic

Add this line before adjusting velocity.y:

velocity = global_transform.basis * velocity 

This makes velocity to be in local space so that you can move in the correct direction relative to the player.
We put this line before adjusting the y velocity because downwards will always be along negative y axis so we do not need to multiply the downwards velocity by the basis.

I think you misunderstood my question… I want to know how many degrees of Y and move the character in that direction.

roma | 2022-12-27 15:38