Move the 3d character to the new rotation?

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

I am moving my 3d character with the arrow keys and when I press the left/right keys the character rotates, but when I press the forward key it moves to the old forward not the new one after the rotation, how can I fix that?
this is the script:

extends KinematicBody

export var speed = 10.0
export var friction = 0.05
export var acceleration = 0.1
var velocity = Vector3()

func _physics_process(_delta):
	var wishdir = Vector3()

	if Input.is_key_pressed(KEY_LEFT):
		wishdir += Vector3.LEFT
		get_node("mesh").rotate_y(deg2rad(0.2))
	if Input.is_key_pressed(KEY_RIGHT):
		wishdir += Vector3.RIGHT
		get_node("mesh").rotate_y(deg2rad(-0.2))
	if Input.is_key_pressed(KEY_UP):
		wishdir += Vector3.FORWARD
	if Input.is_key_pressed(KEY_DOWN):
		wishdir += Vector3.BACK

	wishdir = wishdir.normalized() * speed
	if wishdir.length() > 0:
		velocity = velocity.linear_interpolate(wishdir, acceleration)
	else:
		velocity = velocity.linear_interpolate(wishdir, friction)
	velocity = move_and_slide(velocity)

Sorry my answer wasn’t working, so I removed it.

Adam_S | 2020-10-07 16:52

:bust_in_silhouette: Reply From: Adam_S

You can use the the xform() method for this.

Change this:

wishdir = wishdir.normalized() * speed

to this:

wishdir = $mesh.global_transform.basis.xform(wishdir.normalized() * speed)

thanks a lot that worked, I didn’t see your answer until now because I found another solution but it was long and yours is better, but my character is rotated in the x axis so my forward is now rotated is there a way to catch only the z axis information in wishdir = $mesh.global_transform.basis.xform(wishdir.normalized() * speed)

Shlopynoobnoob | 2020-10-08 15:51