My code should be stopping initial enemy offscreen movement with set_physics_process(false), but it won't work.

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

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:
	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

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

func _ready():

and didnt work?

Andrea | 2021-01-08 08:56

Yeah I did exactly that

BuddyGames | 2021-01-08 15:49

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

BuddyGames | 2021-01-08 16:11

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

Andrea | 2021-01-08 16:16

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

BuddyGames | 2021-01-08 16:56