Player does not face direction of movement

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

When I press “aim” it does not update the animation on my BlendSpace2D. I am trying to put this to work 2 days… Please, send help!!!enter image description here

``
extends KinematicBody

var direction = Vector3.FORWARD
var velocity = Vector3.ZERO

#onready var anim = $AnimationPlayer

var vertical_velocity = 0
var gravity = 20

var movement_speed = 0
var walk_speed = 3
var run_speed = 8
var acceleration = 5
var angular_acceleration = 7

var strafe = Vector3.ZERO
var strafe_dir = Vector3.ZERO

var aim_turn = 0

func _input(event):
if event is InputEventMouseMotion:
aim_turn = -event.relative.x * 0.015

func _physics_process(delta):

if Input.is_action_pressed("aim"):
	$AnimationTree.set("parameters/aim_transition/current", 0)
else:
	$AnimationTree.set("parameters/aim_transition/current", 1)

var h_rot = $Camroot/h.global_transform.basis.get_euler().y

if Input.is_action_pressed("move_fw") || Input.is_action_pressed("move_bw") || Input.is_action_pressed("move_left") || Input.is_action_pressed("move_right"):
	
	direction = Vector3(Input.get_action_strength("move_left") - Input.get_action_strength("move_right"),
				0,
				Input.get_action_strength("move_fw") - Input.get_action_strength("move_bw"))
	
	strafe_dir = direction
	
	direction = direction.rotated(Vector3.UP, h_rot).normalized()
	
	if Input.is_action_pressed("sprint") && $AnimationTree.get("parameters/aim_transition/current") == 1:
		
		movement_speed = run_speed
		$AnimationTree.set("parameters/iwr_blend/blend_amount", lerp($AnimationTree.get("parameters/iwr_blend/blend_amount"), 1, delta * acceleration))
		
	else:
		$AnimationTree.set("parameters/iwr_blend/blend_amount", lerp($AnimationTree.get("parameters/iwr_blend/blend_amount"), 0, delta * acceleration))
		movement_speed = walk_speed
		strafe_dir = Vector3.ZERO
		

else:
	$AnimationTree.set("parameters/iwr_blend/blend_amount", lerp($AnimationTree.get("parameters/iwr_blend/blend_amount"), -1, delta * acceleration))
	movement_speed = 0

if $AnimationTree.get("parameters/aim_transition/current") == 0:
	direction = $Camroot/h.global_transform.basis.z

velocity = lerp(velocity, direction * movement_speed, delta * acceleration)

move_and_slide(velocity + Vector3.DOWN * vertical_velocity, Vector3.UP)

if !is_on_floor():
	vertical_velocity += gravity * delta
else:
	vertical_velocity = 0

if $AnimationTree.get("parameters/aim_transition/current") == 1:
	$Mesh.rotation.y = lerp_angle($Mesh.rotation.y, atan2(direction.x, direction.z), delta * angular_acceleration)
else:
	$Mesh.rotation.y = lerp_angle($Mesh.rotation.y, h_rot, delta * angular_acceleration)

strafe = lerp(strafe, strafe_dir + Vector3.RIGHT * aim_turn, delta * acceleration)
$AnimationTree.set("parameters/strafe/blend_position", Vector2(-strafe.x, strafe.z))

aim_turn = 0

´´

:bust_in_silhouette: Reply From: rballonline

You code is formatted pretty bad but I think you’re looking for something like this.

sprite.flip_h = event.relative.x < 0

to fix the formatting, all you need to do is select all the code, and press the {} icon. =)

Millard | 2020-11-15 05:22

Isn’t my post, so I can’t format it.

rballonline | 2020-11-15 05:59

I know, I was talking to Jm4rcos, I just posted the comment here because it was relevant to your answer. =)

Millard | 2020-11-15 16:37

Thanks but im using 3d

jm4rcos | 2020-11-24 22:16

could you explain what behavior you are trying to get?

Millard | 2020-11-25 04:45

My 3rd person player walks and faces the direction of movement but when i click aim, it walks only forward and doesnt play the animation

jm4rcos | 2020-11-28 16:06