call trigger when player gets to the center of the trigger box?

Hey all! I am trying to work on a mechanic that requires triggers to work, but I need them to work when the player hits the center of the trigger, not when they immediately tough the trigger. How would I make this work? I am not finding any info out there when I google it, but I might also be looking for the wrong thing. Any help pointing me in the right direction would be great! Thanks!

2 triggers

I did try that at one point, but it became such a pain to constantly have to make 2 triggers every time I just need 1, and make opposite trigger codes.

I assume you are making your trigger a large object, but you don’t want anything to happen until the player reaches the centre of the trigger?

So why not just make the trigger much smaller and at the centre of where you would have placed the larger trigger, so that the player won’t activate it until it reaches the position you want?

With floating point numbers (eg, positions), they will never be truly equal, so you’re always “off” a little bit, even if you’re truly standing dead-center.

One simple way is to listen to the OnTriggerStay() method, and in that method check how far from center the player is, and ONLY when that distance is below what you want, activate the thing that should happen.

Since you would be continuously getting called in Stay(), it may be necessary to make sure “the thing that should happen” doesn’t happen more than once, if you care.

But still, I’d recommend the two-trigger approach listed above. Why not make a prefab for it so you just drag the prefab and you’re done? Or else make a custom editor script on the object itself that can injects both triggers, connects them up to the right places, etc.

Better living through custom editor scripts!

Essentially yes, having the player move into the trigger then turn at the center of the trigger.

I probably could, but then I have to make a second trigger for when the player goes back the opposite direction. I want to avoid laying down 2 triggers for 1 trigger if I can.

It is sounding like I will have to work with 2 triggers. Not how I wanted it to work, but I suppose if that’s what has to be done then I’ll have to make it work.

Are you just implementing some kind of hallway movement or move-along-a-line process? If so, this might help:

Bomberman / Hallway / corner movement behavior:

https://discussions.unity.com/t/850473/4

Oh dang! You worked on Atomic Bomberman? That game was great! I tip my cap to you!

As to your question:
Not making one exactly like that, it’ll be more dynamic than 90 degree turns. I probably should have specified on my original posting haha. But I am trying to make a movement system based off how an old asset I used worked. The asset is no longer available on the asset store, but I have a copy of it on some older projects. It has a lot of extra stuff I don’t want, just need the trigger system but I can’t quite figure out how his code works. So I am trying it from the other direction and trying to build from the ground up.

Basically its a platformer, you move in a straight line but when you get to the center of a trigger box your character then spins to a new direction (spins around the y axis) but you continue to move in straight lines. The cool part was that you could set the in/out angle to anything you might want so going forward or backward you would always be lined up with those 2 angles.

https://www.youtube.com/watch?v=_xXD_9vS56E

Here’s the youtube video that was on the asset store. You can see right away that it uses a corner system, which looks awesome, but the “corners” are not 90 degree angles. There’s actually a trigger box that gets called when the player hits the middle, then it changes the direction. That’s the system that I want to recreate (though I suppose if I can only do 90 degree angles that wouldn’t be the worst either)

So I did manage to find there’s a thing for Bounds.center which will find the center of an object. Maybe with that I can compare the current x and z and when both are larger, or smaller, than the x/z of the trigger I can hit the trigger?

Always wanted to try my hand at this kinda movement… here was my take:

All source is in my Proximity Buttons project… Look for the DemoLevel25D scene.

The setup is super-simple, just a linear line of GameObject points, a Gizmo to visualize them.

Includes a cheese left/right mover plus a cheese camera follower… replace with your own easily!

proximity_buttons is presently hosted at these locations:

https://bitbucket.org/kurtdekker/proximity_buttons

https://github.com/kurtdekker/proximity_buttons

That’s impressive! It looks so good too! Thanks so much! You’re a life saver!

Project works just fine and feels really good to use! This is awesome!

1 Like

You’re welcome! I’ve been suggesting people try this approach for years and years but I had never done it until now. It seems to work fairly well. I’ll probably go back and and put in jump, but that should be pretty straightforward, just controlling the Y axis independent of X/Z.

Its a very unique way to go, but the results speak for themselves.

Throw in jumping and multiple paths and you could sell it on the app store :smile:

1 Like

I was thinking about that… I was also thinking about how hard this might be to use in a big hairy full-size level.

I think this is a good core… BUT… I think if I was to use it “for reals” I would add a separate thing that smoothly transitioned you from segment to segment on these lines. That way you could break the level into logical parts and keep each path nice and short to easily edit it, even put it into a prefab.

As long as you place the endpoints close enough, it would transition to the next string in the line when you went off the end of one.

OR… the scripts could all find each other, then destroy every one of them and produce one master out of all the endpoints strung together, “weld” themselves into one at Start() time.

The forking path thing is probably addressable the same way… and in fact you might even get it “for free” once it supports chaining end to end. Presently you can already have a platform that you could go above or below, at least with some tweaks to the raycast ground finder, and then on the upper platform you would have the start of a fresh separate line that goes a different direction, and another script would sense you are near it and transition to / from it smoothly, but ONLY when you’re on the platform, up near the branching line.

Hm that is true, a large level might have a hard time. But the system would work very well as long as the levels aren’t massive. It does eliminate one of the problems with the asset I was showing before which was when you start doing stuff like combat, you can get knocked out of alignment which would be great for bug abusers but a nightmare for designers.

I might even try a separate chain, and some jumping, see if it works well enough on its own right. I’ll see if I can toss in some jumping. Its a system that looks great so far though!

1 Like

I did a little playing around at lunch (when all my game design gets done haha) and got REAL close to what I was seeing before, all except one small part now. Which is great because now I have 2 methods!

public float angle1;
    public float angle2;

    public List<GameObject> objects = new List<GameObject>();

    void Update()
    {
        if (objects.Count > 0)
        {
            for (int i = 0; i < objects.Count; i++)
            {
                float distX = Mathf.Abs(transform.position.x - objects[i].transform.position.x);
                float distZ = Mathf.Abs(transform.position.z - objects[i].transform.position.z);
                if (Mathf.Abs(distX - distZ) < 0.3)
                {
                    if (objects[i].transform.rotation.y == angle1)
                    {
                        objects[i].transform.rotation = Quaternion.Euler(objects[i].transform.rotation.x, angle2, objects[i].transform.rotation.z);
                    }
                    else
                    {
                        objects[i].transform.rotation = Quaternion.Euler(objects[i].transform.rotation.x, angle1, objects[i].transform.rotation.z);
                    }
                }
            }
        }
    }

    void OnTriggerEnter(Collider _other)
    {
        objects.Add(_other.gameObject);
    }

    void OnTriggerExit(Collider _other)
    {
        objects.Remove(_other.gameObject);
    }[CODE]

Actually works just fine except only for one direction. After I use the trigger it will turn the player, but when walking back through the trigger again it doesn't set me back to angle2. So close!