Enemy not rebounding off of walls multiple times.
-
My problem is pretty simple. When I start the game, the enemy starts moving left, facing left like it's supposed to. It hits the wall and then turned around, facing right and moving right. It hits the wall, and then it stops. Here is my code:
extends KinematicBody2D export (int) var run_speed = 50 export (int) var jump_speed = -400 export (int) var gravity = 1200 var velocity = Vector2() var jumping = false var direction = 0 func get_input(): if direction == 1: $Sprite.flip_h = 1 velocity.x += run_speed if direction == -1: $Sprite.flip_h = 0 velocity.x -= run_speed if is_on_wall() and direction == 1: velocity.x = 0 direction = -1 velocity.x -= run_speed if is_on_wall() and direction == -1: velocity.x = 0 direction = 1 velocity.x += run_speed func _physics_process(delta): get_input() velocity = move_and_slide(velocity, Vector2(0, -1))
Please help me I have tried for two hours and don't know what to do
-
What happen if you do it like this
extends KinematicBody2D export (int) var run_speed = 50 export (int) var jump_speed = -400 export (int) var gravity = 1200 var velocity = Vector2() var jumping = false var direction = 0 func get_input(): if direction == 1: $Sprite.flip_h = 1 velocity.x += run_speed return if direction == -1: $Sprite.flip_h = 0 velocity.x -= run_speed return func _physics_process(delta): if is_on_wall(): direction *= -1 get_input() velocity = move_and_slide(velocity, Vector2(0, -1))
-
Not on my pc right now but I'm suspicious about these lines:
if is_on_wall() and direction == 1: velocity.x = 0 direction = -1 # <- This velocity.x -= run_speed if is_on_wall() and direction == -1: # <- And this velocity.x = 0 direction = 1 velocity.x += run_speed
Assume it is now direction == 1, and is on wall is true. It will go to the first IF, the direction is now == -1, and then goes to the second IF, within same frame?
Try to continuously check for wall collisions on every physics step and reverse the direction whenever a collision is detected.
func _physics_process(delta): get_input() if is_on_wall(): direction *= -1 velocity = move_and_slide(velocity, Vector2(0, -1))
-
This is my code now:
extends KinematicBody2D # Declare member variables here. Examples: # var a = 2 # var b = "text" # Called when the node enters the scene tree for the first time. func _ready(): pass # Replace with function body. # Called every frame. 'delta' is the elapsed time since the previous frame. #func _process(delta): # pass export (int) var run_speed = 50 export (int) var jump_speed = -400 export (int) var gravity = 1200 var velocity = Vector2() var jumping = false var direction = -1 func get_input(): if direction == 1: $Sprite.flip_h = 1 velocity.x += run_speed if direction == -1: $Sprite.flip_h = 0 velocity.x -= run_speed func _physics_process(delta): get_input() if is_on_wall(): direction *= -1 velocity = move_and_slide(velocity, Vector2(0, -1))
And it keeps turning around but doesn't go anywhere
-
What happen if you do it like this
extends KinematicBody2D export (int) var run_speed = 50 export (int) var jump_speed = -400 export (int) var gravity = 1200 var velocity = Vector2() var jumping = false var direction = 0 func get_input(): if direction == 1: $Sprite.flip_h = 1 velocity.x += run_speed return if direction == -1: $Sprite.flip_h = 0 velocity.x -= run_speed return func _physics_process(delta): if is_on_wall(): direction *= -1 get_input() velocity = move_and_slide(velocity, Vector2(0, -1))
-
I think the problem might be because of the += and -= lets say the velocity is run speed then when it hits a wall you -= run speed meaning the velocity becomes zero and the enemy stands still, try just setting it directly using
velocity.x = run_speed velocity.x = -run speed
-
It worked, thank you Altria!
-
-
-
@Muhammad-Aregbesola Glad it help