0 votes

I'm learning about platformers, and I have a VisibilityEnabler2D as a child of the root node with physics process parent activated. This means that when The enemy enters and leaves the camera range, it won't move. However at the start of the game, the enemies move because they haven't been viewed yet. I used a tutorial to try to fix it by initially disabling my movement in a _ready function, but that only does something with Physics process parent turned off (disables the movement entirely). My code exactly matches the tutorial. Any clues where I went wrong?

extends KinematicBody2D
class_name Actor
export var speed: = Vector2(50, 600)
var velocity: Vector2 = Vector2()
export var gravity: = 800
func _ready() -> void:
    set_physics_process(false)
    velocity.x = -speed.x

func _physics_process(delta: float) -> void:
    velocity.y += gravity * delta
    if is_on_wall() or not $RayCast2D1.is_colliding() or not $RayCast2D2.is_colliding():
        velocity.x *= -1
    velocity.y = move_and_slide(velocity, Vector2.UP).y
Godot version Regular windows, code in GD script
in Engine by (47 points)
edited by

you mean you added something like this in the actor class script

func _ready():
  set_physics_process(false)

and didnt work?

Yeah I did exactly that

I figured out that it must have simply been to close to the character spawn, as it works now that i moved the enemy way out of the screen lol

you are right sorry, didnt catch that line.

i've done some test and noticed that the visibilityEnabler node is activating even outside of the camera range, if it is closer than 100 pixel to camera border (even if techically not visible).
could your problem come from this "extra margin"? or do your actor keep being activated even at farther distance?


EDIT: you answered while i was writing the comment, i think we arrived to the same conclusion, although i dont know how to change this extra margin

That's alright. It would be pretty lousy design to have an enemy that close the the player character. Anyway, thanks for the help!

Please log in or register to answer this question.

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.