How to make an entity go across another entity?

Godot Version 4

Question

How do I make an entity go across another entity?

extends CharacterBody2D

class_name SlimeEnemy
@onready var slime_sprite = $AnimatedSprite2D
@onready var hitbox = %Hitbox
var speed:int = 100
@onready var player = %Player
@onready var timer = %Prepatk
var health = 80
var health_min = 0
var death:bool = false
var taking_damage:bool = false
var damage_dealt: int = 10
var is_dealing_damage:bool = false
var dir: Vector2
var knockback_force: int = 200
var is_roaming:bool = true
var state: String

func _ready() -> void:
	slime_sprite.stop()
	state = "chase"
	timer.stop()

func _process(_delta):
	if timer.time_left >= 0:
		print(timer.time_left)

func _physics_process(delta: float) -> void:
	animation_handler()
	movement_calc()
	move_and_slide()



func animation_handler():
	if state == "Idle":
		slime_sprite.play("Idle")
	elif state == "attack":
		slime_sprite.play("Attack")
	elif state == "chase":
		slime_sprite.play("Jump")

func movement_calc():
	dir = global_position.direction_to(player.position)
	velocity = dir * speed
	if state == "attack_prep":
		velocity = Vector2(0,0)
	elif state == "attack":
		attack()

func attack():
	dir = global_position.direction_to(player.position)
	speed = 10
	velocity = dir * speed
	hitbox.disabled = true
	velocity = dir * speed
	print(hitbox.disabled)
	state = "chase"




func _on_area_2d_body_entered(body: Node2D) -> void:
	state = "attack_prep"
	velocity = Vector2(0,0)
	timer.start()


func _on_prepatk_timeout() -> void:
	state = "attack"

So I’m trying to make it so that the slime scene gets the players position and makes a Vector2() that goes ACROSS the player position but I’m not sure how to calculate that.

Not sure what your end goal is, but you can multiply a vector, e.g. by 2. It will make it twice as long, which will make it reach “across” the Player. If I understood correctly what you want.

The goal is to make a sorta lunge attack. Also are you saying I should multiply the Vector by 2 on the dir = global_position.direction_to(player.position) ? When I did that it js got stuck inside the player.

It’s because I’m updating it every frame thank you wchc for the help!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.