Detect collision from StaticBody3D instead of CharacterBody3D - C#

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

I’m trying to find a way I can write a script that detects a collision directly from StaticBody3D or CollisionShape3D. Currently I’m using CharacterBody3D physics update to check for collision against a stage exit portal.

private void CheckForStageExit() {
		for (int index = 0; index < GetSlideCollisionCount(); index++) {
			// We get one of the collisions with the player.
			KinematicCollision3D collision = GetSlideCollision(index);
			// If the collision is with a mob.
			if (collision.GetCollider() is StaticBody3D) {
				StaticBody3D temp = (StaticBody3D)collision.GetCollider();
				if (temp.Name == "StageExit") {
					GD.Print("StageExit");
				}
			}
		}
	}

This method does work but it will become cumbersome with the more stages I add to the game. I’d like a way to add a script directly to my static body that can raise an event when the player body enters, like with rigid body enter event. I’m not sure if this is even possible if it I’d like to know how it’s done.

This seems like a function of the player (or one of its components) to check for body_entered as it would any other body, switch on collision type/group. Perhaps if you wish to keep this method, use groups instead of individual names or even the type (e.g. if (collision.GetCollider() is StageExit stageExit) { /* stuff */ }.

Unless I’ve misunderstood?

spaceyjase | 2023-05-27 19:28

I’d like to have something similar to body enter on my collision shape. This way I could check if the play has entered when the event is raised. I know it can be done by adding a rigid body but since the object is static it doesn’t need a rigid body.

If you have used Unity you may be familiar with the on collision enter method. It doesn’t require a rigid body just a mesh with a shape. Something similar to this.

joyfulsquirrel | 2023-05-27 20:35

Sure, but the other way around; a static body doesn’t have these event but a rigid body colliding with them does, e.g. the player or some other thing monitoring a rigid body. Switch the logic around, only the thing moving really matters.

If the logic must go in the static body thing then an area (e.g. like a Unity trigger) can detect bodies and might be worth considering.

spaceyjase | 2023-05-27 20:51