Hey everyone.
I’m a beginner, I’m trying to figure some first person controller stuff.
There’s a cube acting as a wall. What I’m trying to do is get the character to move through the wall, the wall’s layer change to “PassThrough”, allowing the character to pass through it, and the wall color to change, and when the player exits the wall, those same parameters to go back to their default state.
I had no problem achieving both goals by using OnTriggerEnter and OnTriggerExit.
But it seems these 2 events are getting called again and again while the player is inside the wall, meaning the wall will rapidly change from (let’s say) green to white.
What am I missing?
Thanks.
You’ll likely want a trigger collider attached to the player which will trigger monobehaviours on other game objects. To keep things simple, just create a child game object of you player and attach a sphere collider set as a trigger. Create and attach a script to this game object which will does the following:
(psudocode that probably won’t work as is)
void OnTriggerEnter(Collider other) {
TryTrigger(other.gameObject, enter: true, sender: this) // or sender as player?
}
void OnTriggerExit(Collider other) {
TryTrigger(other.gameObject, enter: false, sender: this) // or sender as player?
}
void TryTrigger(GameObject otherGameObject, bool enter, Object sender) {
var triggerable = otherGameObject.GetComponent<Triggerable>()
if (triggerable != null) {
triggerable.Trigger(enter, sender)
}
}
Then create an abstract Triggerable
class which inherits from MonoBehaviour
which defines the contract which all triggerables will implement. In this simple example, you’ll need two classes:
public void abstract class Triggerable: MonoBehaviour {
public void Trigger(Bool enter, Object sender);
}
public class PassThroughWall: Triggerable {
public void Trigger(Bool enter, Object sender) {
// do the things that you're currently doing in your existing script
}
}
Now just attach the PassThroughWall script to your wall section which you want to pass though and try to walk though it. I haven’t tested this code so you’ll likely have to tweak it slightly, but the idea is a solid, simple, way to approach the problem.
You can now make all sorts of things triggerable by the player approaching. I’d recommend enhancing this technique and put the trigger and triggerable game objects on special layers which will reduce the overhead which would be caused by the collider calling get component on every game object that it collides with.
Good luck!