First year of programming, and so far I'm just practicing and trying to make the mechanics I wanna make,
My character has a flashlight that uses an Area2D and when the enemy enters the Area2D it's supposed to gradually slow it's speed before coming to a complete stop and then dying.
I tried to using interpolation, but when I did it with the enemy's speed, it would just immediately lower the speed, but never come to a stop.
Script for enemy "Imp" (KinematicBody2D):
extends KinematicBody2D
export var speed = 25
export var default_speed = 25
var velocity = Vector2.ZERO
var player
var rng = RandomNumberGenerator.new()
func _ready():
player = get_tree().root.get_node("Root/Player")
rng.randomize()
func _physics_process(delta):
velocity = Vector2.ZERO
if player:
velocity = position.direction_to(player.position) * speed
velocity = move_and_slide(velocity)
Script for flashlight "Light" (Area2D):
extends Area2D
var friction = 0.60
func _ready():
pass
func _on_Light_body_entered(body):
if "Imp" in body.name:
body.speed = lerp (body.speed, 0, friction)
func _on_Light_body_exited(body):
if "Imp" in body.name:
body.speed = body.default_speed