Skip to content

Instantly share code, notes, and snippets.

@RepComm
Created January 30, 2024 03:33
Show Gist options
  • Save RepComm/2dbf64bbe7bcaf6ef83b2a9d4b0fd88f to your computer and use it in GitHub Desktop.
Save RepComm/2dbf64bbe7bcaf6ef83b2a9d4b0fd88f to your computer and use it in GitHub Desktop.
Godot 4 C# physics hole cutter (does nothing visually, just disables cutLayer or re-enables it based on Area3D contact)
using Godot;
// Attach to an Area3D
// listens to collisions with area3d (use area3d's collision mask to select what objects should walk thru holes)
// sets or unsets the cutLayer (see in editor after attaching this script)
// dont forget to build C# project once to see the editor field for PhysicsHole)
public partial class PhysicsHole : Area3D {
[Export]
/**The individual layer in which objects are "cut" physically speaking*/
public uint cutLayer = 2;
public override void _Ready() {
this.BodyEntered += this.OnBodyEntered;
this.BodyExited += this.OnBodyExited;
}
public override void _ExitTree() {
base._ExitTree();
this.BodyEntered -= this.OnBodyEntered;
this.BodyExited -= this.OnBodyExited;
}
private void OnBodyEntered (Node3D n) {
//do you even physics?
if (n is not PhysicsBody3D) return;
//yeah, we physics
var b = n as PhysicsBody3D;
//disable collision with the cut layer
b.CollisionMask &= ~cutLayer;
}
private void OnBodyExited (Node3D n) {
//do you even physics?
if (n is not PhysicsBody3D) return;
//yeah, we physics
var b = n as PhysicsBody3D;
//enable collision with the cut layer
b.CollisionMask |= this.cutLayer;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment