Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Major_Monkey_0720 |
My KinematicBody2D works fine when moving left, right, up, and down, but if I move him at an angle his sprite shakes, and jitters violently, and I’m not sure what to do about it…
My code:
extends KinematicBody2D
var input_vec = Vector2.ZERO
var velocity = Vector2.ZERO
var cutscene_vec = Vector2.ZERO
var speed = 0
var room = 1
onready var animationTree = $AnimationTree
onready var animationState = animationTree.get("parameters/playback")
onready var animationPlay = $AnimationPlayer
onready var loop_timer = $loop_timer
const accel = 4
var boost = 0
onready var sprite = $Sprite
func _ready():
animationPlay.play("start_pos")
animationTree.active = true
func _physics_process(delta):
if GlobalVars.move == 1:
input_vec.x = Input.get_action_strength("right") - Input.get_action_strength("left")
input_vec.y = Input.get_action_strength("down") - Input.get_action_strength("up")
input_vec = input_vec.normalized()
if input_vec != Vector2.ZERO:
velocity.x = input_vec.x * speed
velocity.y = input_vec.y * speed
animationState.travel("Run")
animationTree.set("parameters/Idle/blend_position", input_vec)
animationTree.set("parameters/Run/blend_position", input_vec)
if speed < 60:
speed += accel
if velocity.x > 0:
sprite.flip_h = false
else:
sprite.flip_h = true
else:
velocity.x /= 1.2
velocity.y /= 1.2
animationState.travel("Idle")
speed = 0
move_and_slide(velocity)
print(velocity)
else:
velocity = Vector2.ZERO
input_vec = Vector2.ZERO
speed = 0
move_and_slide(cutscene_vec)