Opposite to desired character movement

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

So I made a slime script and the slime is supposed to come towards me but it goes the opposite direction I think it has to do something with my crappy knockback line

Here is my code:

extends KinematicBody2D

signal death

var hit = true
var health = 100
var health_max = 100
var health_regeneration = 1

var basic = false
var move1 = false
var move2 = false
var timer = 2
var run_speed = 55
var velocity = Vector2.ZERO
var player = null
var basic_hit_box = null

onready var anim = $AnimationPlayer
onready var sprite = $Node2D/main
onready var ded_sprite = $Node2D/ded
onready var attack_sprite = $Node2D/attack

func _ready():
	hit = false

func _physics_process(delta):
	velocity = Vector2.ZERO
	if player:
		velocity = position.direction_to(player.position) * run_speed
	if player and hit == true:
		velocity = position.direction_to(player.position) * -85
	velocity = move_and_slide(velocity)

func _process(delta):
	health = min(health + health_regeneration * delta, health_max)
	if health <= 0:
		velocity = 0

func _on_Area2D_body_entered(body):
	player = body

func _on_Area2D_body_exited(body):
	player = null

func _on_hitbox_area_entered(area):
	basic_hit_box = area
	hit = true
	health -= 7

func _on_hitbox_area_exited(area):
	basic_hit_box = null
	hit = false

func _on_AnimationPlayer_animation_finished(anim_name = "ded"):
	if anim_name == "ded":

func _on_player_basic_damage(basic_damage):
	health -= basic_damage

func _on_player_move1_damage(move1_damage):
	health -= move1_damage

func _on_player_move2_damage(move2_damage):
	health -= move2_damage
:bust_in_silhouette: Reply From: DaddyMonster

I’m not sure which condition is giving you the opposite movement but I’ll use the one below as an example.

velocity = position.direction_to(player.position) * runspeed

Just do this the other way round and you get a vector in the other direction.

velocity = player.position.direction_to(position) * runspeed

Hope that helps.

ps. If you edit your question, highlight the code and then click the curly braces symbol above then it’ll be reformatted making it a million times easier to read. Just means you’re much more likely to get replies in future and it makes life easier for the person answering. Oh and a specific title would be helpful too eg “Opposite to desired character movement”.

Sorry about that I’m still new

LJ INFINITY | 2021-11-27 04:59

Yes what u said helps, but I still have the problem

LJ INFINITY | 2021-11-27 05:59

Don’t worry in the slightest, it’s just a tip so you know in future.

The only thing that’s moving your slime character is _physics_process - and there are two conditions there. If reversing doesn’t give you the result you need then the other possibility is that your condition if player and hit == true: is evaluating true when you don’t want it to.

I’m not certain what exact behaviour you’re looking for with all your signalled states so I can’t tell you the answer, only how to approach solving it yourself (which is more useful to you as someone new anyway).

So, you can add line breaks to debug (click on the “gutter” to the left of the code so a red dot appears) within these conditions to see when they’re evaluating true and false. After line braking the code will stop at that point and if you mouse over the variables it’ll say what they’re value is and step forward / over if you want to see what happens next.

Or equally, if you prefer you can print the state of player and hit (best to do this is places where it doesn’t spam it 60 times a frame but it’s not a disaster if you do).

The name of the game here is to limit where the problem could be and so hone in on the part of the code producing the undesirable behaviour. Just printing / line breaking will let you know when the signals fire and the variables change. Going through this you’ll soon debug the issue.

Good luck!

DaddyMonster | 2021-11-27 13:41

Thank you, appreciate the advice

LJ INFINITY | 2021-11-27 14:01