runninggame how to check if going of course ?

Hello everyone

I recently started a project for school and i decided to make a running game for young children (like templerun).
Now I’m very new to unity and the programming so I’ve come to you for a little help !

I want to check if my person is walking on the sidewalk and not on the road. What is the best way to approach it? I’ve tried measuring the distance between the sidewalk and the person but that doesn’t seem to be working.

If you could give me tips on how to best do it, that would be great !

thank you in advance !!!

Depending on the layout of the road and sidewalk, you could put trigger objects that cover the road and the sidewalk. When the player comes into contact with the trigger objects, you know where the player is located. If the sidewalk and street are straight, a stretched cube would work. Just make sure that:

  1. The box collider attached to the cube has “Is Trigger” set to true.
  2. The cube has a rigidbody component.
  3. The rigidbody has “Use Gravity” set to false and “Is Kinematic” set to true (this makes it so the physics engine ignores the object but still allows the object to handle collisions).

Some sample code that might help you understand how this works:

using UnityEngine;

public class LocationHandler : MonoBehaviour {
    void OnTriggerEnter(Collider collider) {
        // This is called when an object collides with this object and
        // this object is set as a trigger.
        Debug.Log(collider.name + " has triggered " + this.gameObject.name);
    }
    void OnTriggerExit(Collider collider) {
         // This is called when a collider is no longer
         // colliding with this object.
         Debug.Log(collider.name + " has left " + this.gameObject.name);
    }
}

Attach that script to the trigger objects you have on your sidewalk and road and whenever a gameObject with a collider and rigidbody comes in contact with it, you’ll know.

Another way this can be done is attaching this script to the player, and whenever the player comes in contact with a trigger, that trigger object will call the trigger methods in the script attached to the player.

using UnityEngine;

public class LocationHandler : MonoBehaviour {
    void OnTriggerEnter(Collider collider) {
        // This is called when the player object collides with a trigger object.
        Debug.Log(this.gameObject.name + " has triggered " + collider.name);
    }
    void OnTriggerExit(Collider collider) {
         // This is called when the player object is no longer
         // colliding with a trigger
         Debug.Log(this.gameObject.name + " has left " + collider.name);
    }
}

This might be more useful for your game. Hope this helps!

thanks !!!
I’ll try it out and I’ll let you know.

hey

I got it working !! Now when it hits the side of the sidewalk it goes back to previous coords.
But I would like to make a little effect : I would like to make it “blink” (visible/nor-visible) for a few times

I got the following code :

void OnTriggerExit(Collider collider) {
		
        // Debug.Log(this.gameObject.name + " has left " + collider.name);
		for (int i = 0; i < 8; i++) {
			if(bRenderer == true)
			{
				Timmy.renderer.enabled = false;
				Debug.Log("true");
				bRenderer = false;
				StartCoroutine(WaitOneSecond());
			}
			else
			{
				Debug.Log("false");
				Timmy.renderer.enabled = true;
				bRenderer = true;
				StartCoroutine(WaitOneSecond());
			}
	}

    }
 IEnumerator  WaitOneSecond() {
		float fTime = 10;
		Debug.Log ("test");
		 
		yield return new WaitForSeconds(fTime);
		
	}
	
}

There isn’t a pause between the two, the yield return new WaitforSeconds(fTime) isn’t working.
What am I doing wrong ?

You are using co-routines wrong.

private bool isBlinking = false;
void OnTriggerExit(Collider collider) {
        // Debug.Log(this.gameObject.name + " has left " + collider.name);
		if (!isBlinking)
                StartCoroutine(Blinker());
}
IEnumerator  Blinker() {
	isBlinking = true;
        for (int i = 0; i < 8; i++) {
            if(bRenderer == true)
            {
                Timmy.renderer.enabled = false;
                Debug.Log("true");
                bRenderer = false;
            }
            else
            {
                Debug.Log("false");
                Timmy.renderer.enabled = true;
                bRenderer = true;
            }
		yield return new WaitForSeconds(1f);
    }
	isBlinking = false;
}

thanks that worked great !! Now i got what I did wrong