How can I active my function when player reaches certain point

I’m having trouble with this as I’m new to C#. What I’m trying to do is have my function active when my player reaches a certain point on its X axis. I’m guessing you start with an ‘if’ statement but where do I go from there?

Or maybe a bool…

Help would be much appreciated :slight_smile:

So I’m guessing it’s some kind of 2D game if you are trying to get an x position specifically not just a 3D space somewhere in your scene?

For that the transform stuff should work.

Just want to briefly mention if you are new to C# or Unity that another great way to get scripts to execute based on your player reaching a specific position is to set up an area trigger object.

You can do this by creating an empty GameObject and placing any kind of collider on the object (I usually use spheres or box).

Then you create a collider on your player object or a child of the player object. Set that collider to Is Trigger = true in the Unity Inspector. Then attach a rigid body to the same object with the trigger. For my games I always have set these to gravity disabled and Is Kinematic enabled so it doesn’t effect the game play if you aren’t using gravity on your player.

Then you can place a script on the first trigger detection collider you set up and placed in the desired area of your scene. On the script you can do something like:

GameObject newTriggerObject;

void OnTriggerEnter(Collider triggerObject)
{
if (triggerObject.name == "Player")
{
//execute your code 
//you can create a reference for the triggerObject for more complicated functions like:
newTriggerObject = triggerObject.gameObject;

//access the newTriggerObjects script:
newTriggerObject.getComponent<PlayerScript>().triggerExampleFunction();
}
}

You can also do the same exact stuff for OnTriggerStay or OnTriggerExit for complete area detection functionality.

I think this would be more performance friendly than checking for the distances and conditions every Update or FixedUpdate but am not 100% sure.

With this system you can just have the area collider sitting pretty much idle until the OnTriggerEnter activates and then it can shoot off a single clean function calls or other logic stuff.

These kind of triggers can be very useful since they can be put on any collider in your game at any location. Therefore they are not dependent on specific logic based on their position in the scene. They can work exactly the same anywhere in your game.

For example my game so far is mainly flat but there are areas where you can run up hills or down slightly through shallow ponds. Because the game is mostly flat it wouldn’t make sense for me to use my rayCast system I use to determine player height 100% of the time. Instead I created a collider like my example and set this collider into the areas of my game with uneven terrain. Therefor when the player enters the collider it switches on a “elevationBool” so that the player actually uses the rayCast to determine what his height from the ground should be.

That’s just one example of how this sort of positional detection could be useful instead of always checking the transform.position every frame.

This is a little bit more than you probably need but I know I had a bit of trouble getting all this kind of stuff sorted out when I first started so I’m sure this example could be useful for yourself or others with the area detection stuff.

Every GameObject has a Transform component which keeps track of its position in the world.

Let’s suppose you have a script attached directly to the player. During each frame, you could check its position:

function Update() {
    if (transform.position.x > 500) {
        DoSomething();
    }
}

function DoSomething() {
    //todo: what do you want to do, here?
}

In the above example, DoSomething() would be called many times – once per frame while the player is more than 500 units down the world’s x-axis. If you want to call the function just once, you could keep track of whether or not you’ve called it:

var didSomething = false;

function Update() {
    if (!didSomething && transform.position.x > 500) {
        DoSomething();
        didSomething = true;
    }
}