Make movement account for rotation

Godot Version

4.2

Question

Hey, this should be fairly simple, but I can’t figure out how to make my movement code account for rotation. I think I need to try and apply basis, but I’m unsure of how to actually do that.
I’m currently using a simple-ish move_toward function setup in my movement code, and I can’t figure out where to add the basis.
Am I on the right train of thought, and if so, how would I do this. Thanks!

var acceleration = 3
var speedcap = 15
var turnspeed = .05

func _physics_process(delta: float) → void:

var movevector = Vector2.ZERO

movevector.x = Input.get_axis("SRight", "SLeft")

if movevector.x == 1:
	velocity.x = move_toward(velocity.x, speedcap, acceleration)
elif movevector.x == -1:
	velocity.x = move_toward(velocity.x, -speedcap, acceleration)
elif movevector.x == 0:
	velocity.x = move_toward(velocity.x, 0, acceleration)

Btw this is a chunk of my code, should be enough to get a bearing on my problem?

IDIDITYAY

It took a lot of info cobbling but it works. Here is what I have so far… (Note the code is trying to emulate old armored core style controls…)

extends CharacterBody3D

var acceleration = 3
var speedcap = 15
var turnspeed = .05
var strafeactual = 0
var advanceactual = 0

func _physics_process(delta: float) → void:

velocity = Vector3()

var inputvector = Vector2.ZERO

inputvector.x = Input.get_axis("SRight", "SLeft")

if inputvector.x == 1:
	strafeactual = move_toward(strafeactual, speedcap, acceleration)
if inputvector.x == -1:
	strafeactual = move_toward(strafeactual, -speedcap, acceleration)
if inputvector.x == 0:
	strafeactual = move_toward(strafeactual, 0, acceleration)

velocity += global_transform.basis.x * strafeactual

inputvector.y = Input.get_axis("Backward", "Forward")

if inputvector.y == 1:
	advanceactual = move_toward(advanceactual, speedcap, acceleration)
if inputvector.y == -1:
	advanceactual = move_toward(advanceactual, -speedcap, acceleration)
if inputvector.y == 0:
	advanceactual = move_toward(advanceactual, 0, acceleration)

velocity += global_transform.basis.z * advanceactual

var turndir

turndir = Input.get_axis("TRight", "TLeft")

if turndir == 1:
	rotate_y(turnspeed)
elif turndir == -1:
	rotate_y(-turnspeed)

move_and_slide()

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.