0 votes

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"):
    rotate_y(-25)
if Input.is_action_pressed("move_left"):
    rotate_y(25)
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?

Godot version 3
in Engine by (90 points)

1 Answer

0 votes

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.

by (2,017 points)

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.