Some help with collision detection

Godot Version

4.2.2

Question

I have my player and an NPC. I have an NPC that walks back and forth using RayCast2D nodes so that it knows to turn around and move until next colliding object. How I currently have the NPC set up, it works just fine with any collision set up, but I found that if my player touches the NPC from behind it, it marks it as a collision and the NPC turns around rather than just keep going (I know I don’t have this set up so no surprise here).

How is something like this set up where the NPC is still an object the player or NPC can’t walk through, but can’t affect the direction the NPC is going while still keeping the NPC collision with the world objects? I’m assuming part of this is the Layer and Mask settings for Player, NPC, and RayCast2D, but I can’t get it right when switching layers for all 3 nodes (player, world/tile, and NPC).

extends CharacterBody2D

const SPEED = 25.0

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var direction = 1
@onready var animated_sprite_2d = $AnimatedSprite2D
@onready var ray_cast_left = $RayCastLeft
@onready var ray_cast_right = $RayCastRight

func _physics_process(delta):
	# Add the gravity.
	velocity.y += gravity * delta
	
		# Flip sprite for right movement
	if direction > 0:
		animated_sprite_2d.flip_h = true
	elif direction < 0:
		animated_sprite_2d.flip_h = false
	
	if ray_cast_right.is_colliding():
		direction = -1
		animated_sprite_2d.flip_h = true
		animated_sprite_2d.play("Hop")
	if ray_cast_left.is_colliding():
		direction = 1
		animated_sprite_2d.flip_h = false
		animated_sprite_2d.play("Hop")
		
	#Set horizontal movement
	velocity.x = -SPEED
	
	position.x += direction * SPEED * delta

RayCast2D for right and left currently have Collison Mask 1
Player is on Layer 1 and Mask 2
Tiles are Collision Layer 1 and 2 with Collision Mask on 1

So if I understand correctly the RayCast should only hit tiles to make the NPC turn around, but it should ignore the player?

In that case the player and tiles need to be on different layers, and the raycast’s mask should only include the tile’s layer. Something like:

RayCast - Mask: 2
Player - Layer: 1, Mask: 2
Tiles - Layer: 2, Mask: 0 (I don’t think tiles need a mask, but not sure…)

So that didn’t seem to help. Here’s what happens and also found out if I push the frog to the wall it actually goes through it?

Current Issue

The NPC’s mask also has to be set to 2, to collide with the Tiles (or whatever the Tile’s layer is, if it’s not 2).

If the ray cast’s mask isn’t include the player’s layer then I don’t really know why it turns around when you bump into it… :face_with_diagonal_mouth:

When I set the Mask to just 2, it works a tiny bit better, but you can see it can still be pushed through a bit

Still same issue