Godot Version
4
Question
enemy does colide with player and can go throgh portals ,but not suroundings , and enemy is on same colision layer with my tile set.
the big colision shape is the detection area for enemy to detect player.
thx
4
enemy does colide with player and can go throgh portals ,but not suroundings , and enemy is on same colision layer with my tile set.
the big colision shape is the detection area for enemy to detect player.
thx
Is your enemy masking the same layer as your tilemap? i.e if the tilemap is on layer 1, you should include 1 and 2 in the mask.
they are on the same layer.
but thanks anyway.
Being on the same layer is different from masking the other’s layer.
I notice your physics process is directly moving the enemy’s position, rather than calling using velocity move_and_slide
or move_and_collide
, so it does nothing to react to collisions.
Make sure to paste code, not screenshots
but my colision shape works for my player thow.
and my player is also on collision and mask 1 and 2 just like my tile map layer
Does your player script use velocity and move_and_slide
? could you paste your player’s script?
so shouldnt they act the same?
extends CharacterBody2D
@onready var animated_sprite_2d: AnimatedSprite2D = $Area2D/AnimatedSprite2D
const SPEED = 90
var isAttaking=false
func get_input ():
var input_direction := Input.get_vector(“ui_left”, “ui_right”,“ui_up”,“ui_down”)
velocity=input_direction*SPEED
if Input.is_action_just_pressed("space")and isAttaking == false and (velocity.x>1 || velocity.x<-1):
isAttaking=true
animated_sprite_2d.animation="slice"
await get_tree().create_timer(0.25).timeout
isAttaking=false
if Input.is_action_just_pressed("space")and isAttaking == false and (velocity.y<-1):
isAttaking=true
animated_sprite_2d.animation="slice up"
await get_tree().create_timer(0.25).timeout
isAttaking=false
if Input.is_action_just_pressed("space")and isAttaking == false and (velocity.y>1 ||velocity.y==0 && velocity.x==0):
isAttaking=true
animated_sprite_2d.animation="slice down"
await get_tree().create_timer(0.25).timeout
isAttaking=false
if (velocity.x>1 || velocity.x<-1) and isAttaking==false:
animated_sprite_2d.animation="move side"
else :
if (velocity.y<-1) and isAttaking==false:
animated_sprite_2d.animation="move up"
if (velocity.y>1) and isAttaking==false:
animated_sprite_2d.animation="move down"
if (velocity.x==0 && velocity.y==0)and isAttaking==false:
animated_sprite_2d.animation="default"
func _physics_process(delta):
get_input()
move_and_slide()
var isLeft = velocity.x<0
animated_sprite_2d.flip_h = isLeft
this is my player script it has move and slide()
but how should i add move and slide to my enemy code
instead of position +=
you should set velocity to the direction you want, then use move_and_slide()
func _physics_process(delta: float) -> void:
velocity = position.direction_to(player.position) * SPEED
move_and_slide()
it worked thank you!!!