0 votes

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():
    ded_sprite.hide()
    attack_sprite.hide()
    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
        sprite.hide()
        ded_sprite.show()
        anim.play("ded")
        emit_signal("death")



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
    anim.play("hit")
    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":
        queue_free()


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
Godot version 3.2.3.stable mono
in Engine by (25 points)
edited by

1 Answer

+1 vote

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".

by (2,087 points)

Sorry about that I'm still new

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

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!

Thank you, appreciate the advice

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.