#How to get the local velocity of CharacterBody3D

1 messages · Page 1 of 1 (latest)

stone sky
#

i was able to get the body to move and rotate in 3d but when the body is rotated, it doesnt go forward in the local axis, only global. I looked everywhere but havent found anything useful

``extends CharacterBody3D

@onready var plrBody = self
var currentSpeed = Vector3.ZERO
var plrSpeed = 2
var acceleration = 8
@export var plrMouseSens = 1.2
@onready var plrCamera = $PlrCamera

func _physics_process(delta):
keyInput()
move_and_slide()

func keyInput():

var direction = Vector3.ZERO

if Input.is_action_pressed("move_forward"):
    direction.z -= plrSpeed
if Input.is_action_pressed("move_back"):
    direction.z += plrSpeed
if Input.is_action_pressed("move_left"):
    direction.x -= plrSpeed
if Input.is_action_pressed("move_right"):
    direction.x += plrSpeed

CharaMovement(direction)

func CharaMovement(direction):

plrBody.set_velocity (plrBody.velocity.lerp(direction, get_process_delta_time() * acceleration))
print(plrBody.velocity)

func _input(event):
if event is InputEventMouseMotion:
mouseLook(event)

func mouseLook(event):
plrBody.rotation.y += event.relative.x * -1 * plrMouseSens * get_process_delta_time()
plrCamera.rotation.x += event.relative.y * -1 * plrMouseSens * get_process_delta_time()``

ocean marten
#

can you repost your code with backticks around it so it typesets in a monospaced font?

#

oh, i guess you'd have to rotate the direction variable inside of keyInput() in the exact same way that you rotate the player inside of mouseLook(event). to do this, i think you could define a variable outside the scope of those functions to record that rotation, and then just apply the rotation to direction directly??

stone sky
ocean marten
#

yeah, there might also be some way to use a Basis ?? but i haven't done that in godot myself. anyway, glad it's working for now!

stone sky