![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | PumpkinSeedMan |
I’d like to create a 2D game where an enemy continues to walk or jump once the players stand on it.
This is the behaviour of the enemies in the sample Godot project (2d platformer - kinetic). In my game, however, the enemy stops completely once the player is on top.
I’ve compared the code between the sample game and mine, and I can’t figure out what is different (except that I use move_and_slide
and not move_and_slide_with_snap
inside player.gd, because for me, move_and_slide_with_snap
caused the player to stop responding to input.
This is my very first Godot project, so I’m still learning the ropes. (I’ve been coding for years, so I am familiar with a number of concepts.)
Here is the enemy.tscn file
[gd_scene load_steps=5 format=2]
[ext_resource path="res://sprites/baddies/enemy.png" type="Texture" id=1]
[ext_resource path="res://src/Enemy.gd" type="Script" id=2]
[sub_resource type="RectangleShape2D" id=1]
extents = Vector2( 24, 31.75 )
[sub_resource type="RectangleShape2D" id=2]
extents = Vector2( 32, 10 )
[node name="Baddie" type="KinematicBody2D"]
collision_layer = 8
collision_mask = 36
script = ExtResource( 2 )
[node name="Baddie" type="Sprite" parent="."]
position = Vector2( -9.09495e-13, -40 )
scale = Vector2( 0.740741, 0.811828 )
texture = ExtResource( 1 )
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2( 0, -32.25 )
shape = SubResource( 1 )
[node name="VisibilityEnabler2D" type="VisibilityEnabler2D" parent="."]
visible = false
position = Vector2( -0.5, -28.5 )
scale = Vector2( 4.45, 2.85 )
process_parent = true
physics_process_parent = true
[node name="PickupDetector" type="Area2D" parent="."]
collision_layer = 8
collision_mask = 4
[node name="CollisionShape2D" type="CollisionShape2D" parent="PickupDetector"]
position = Vector2( 0, -72 )
shape = SubResource( 2 )
[node name="FloorDetectorLeft" type="RayCast2D" parent="."]
position = Vector2( -40, -42 )
enabled = true
collision_mask = 32
[node name="FloorDetectorRight" type="RayCast2D" parent="."]
position = Vector2( 40, -42 )
enabled = true
collision_mask = 32
[node name="WallDetectorLeft" type="RayCast2D" parent="."]
position = Vector2( 8, -40 )
rotation = 1.5708
enabled = true
collision_mask = 32
[node name="WallDetectorRight" type="RayCast2D" parent="."]
position = Vector2( -16, -40 )
rotation = -1.57079
enabled = true
collision_mask = 32
[connection signal="body_entered" from="PickupDetector" to="." method="_on_PickupDetector_body_entered"]
And my Enemy.gd
class_name Baddie
extends Character
enum State {
NORMAL,
DEAD
}
onready var floor_detector_left = $FloorDetectorLeft
onready var floor_detector_right = $FloorDetectorRight
onready var wall_detector_left = $WallDetectorLeft
onready var wall_detector_right = $WallDetectorRight
var affected_by_gravity = true # set to false for flying enemies
func _ready() -> void:
motion.x = -50.0
var _State = State.NORMAL
func _on_PickupDetector_body_entered(body):
### nothing here yet....
func _physics_process(delta):
if not floor_detector_left.is_colliding() or not floor_detector_right.is_colliding():
motion.x *= -1
if wall_detector_left.is_colliding() or wall_detector_right.is_colliding():
motion.x *= -1
if affected_by_gravity:
motion.y += GRAVITY * delta
# commented this if-statement out to see if this
# was somehow causing the enemy to stop moving
# when the player was on top #######
# if is_on_wall():
# motion.x *= -1.0
motion.y = move_and_slide(motion, FLOOR_NORMAL).y