I'm new to both Stackoverflow and Godot. I have a script for my player which allows me to switch from momentum-based movement (winged) to traditional 8-direction movement (running). However, when I disable (winged) whilst I'm moving, and toggle back to (winged), it remembers the momentum I had and shoots me in that direction. I want my player to reset its speed to zero every time I toggle. I think I'm only missing one or two lines Here's my code
extends KinematicBody2D
export var speed = 300
var velocity = Vector2()
var wings = false
export var MAX_SPEED = 1000
export var ACCELERATION = 1800
var motion = Vector2.ZERO
func _physics_process(delta):
if (!wings):
velocity = Vector2()
if Input.is_action_pressed("right"):
velocity.x += speed
if Input.is_action_pressed("left"):
velocity.x -= speed
if Input.is_action_pressed("down"):
velocity.y += speed
if Input.is_action_pressed("up"):
velocity.y -= speed
if Input.is_action_pressed("shift"):
speed = 70
else:
speed = 300
move_and_slide(velocity)
if (wings):
var axis = get_input_axis()
if axis == Vector2.ZERO:
apply_friction(ACCELERATION * delta)
else:
apply_movement(axis * ACCELERATION * delta)
motion = move_and_slide(motion)
func _process(delta):
if Input.is_action_just_pressed("toggle"):
wings = !wings
func get_input_axis():
if (wings):
var axis = Vector2.ZERO
axis.x = int(Input.is_action_pressed("right")) - int(Input.is_action_pressed("left"))
axis.y = int(Input.is_action_pressed("down")) - int(Input.is_action_pressed("up"))
return axis.normalized()
func apply_friction(amount):
if (wings):
if motion.length() > amount:
motion -= motion.normalized() * amount
else:
motion = Vector2.ZERO
func apply_movement(acceleration):
if (wings):
motion += acceleration
motion = motion.clamped(MAX_SPEED)