Home > Net >  Distance between game object and ground in Unity
Distance between game object and ground in Unity

Time:06-05

I recently started having a look at game development with Unity and was trying to make a simple 2D character with basic movement abilities. This character is supposed to jump and move from side to side, but only if it is standing on something.

Now my question is: How do you check if a player is standing on something? / Get the distance to the next game object / collider beneath the player game object?

Would greatly apreciate any helpful answers and especially explanations on how exactly it works. Thanks!

CodePudding user response:

To do this, you need to send a ray to detect the point of impact on the ground and then calculate the distance. The code below sends a ray from the center of your object down to the maximum height (3) and gives the size.

public LayerMask groundLayer;
public float maxRayLength = 3;
public void Update()
{
    var hit = Physics2D.Raycast(transform.position, Vector3.down, maxRayLength, groundLayer.value);
    
    if (hit) Debug.Log(hit.distance); // it will print current distance from pivot
}

enter image description here

If you want to calculate the height of the ray from the character's foot, there are two methods, one is subtracting half the height of the character from it.

Physics2D.Raycast(transform.position-transform.up*height, ....)

Next one is to use an empty object at the base of the character, which we refer to instead of the center.

public Transform pivot;

Then..

Physics2D.Raycast(pivot, ....)

CodePudding user response:

There are a few ways of actually doing this.

The most usual although a bit complicated way of doing it for a beginner is using Raycasts. A Raycast is basically a small invisible line that starts and ends where you tell it to. If anything of a specific tag or layer is caught in it's crossfire you can basically pull that object from your code. Raycasts are used for a lot of things, most notably in Shooter games to shoot or in games like Skyrim to pickup objects and interact with them.

Another way to do this, which is a bit more popular in 2D games is to create a "feet" GameObject and make it the child of the player in the hierarchy. You can add a box collider on that GameObject and check the "IsTrigger". You can add a Tag to your ground objects and through your code using the OnTriggerEnter() and OnTriggerExit() Methods you can basically tell when your character is floating on air and when he is on ground.

Another popular method is to use the Physics.OverlapBox() Method which is pretty much the same as the Trigger Method but you are creating an invisible box (much like a raycast) and instead of only getting notified when Triggered (something enters or exits) you check if the invisible box is colliding with another object/tag/collider (which could be your ground).

There are also a few different things you can do with a Nav Mesh (mostly in 3D) but I think that for now these 3 should suffice!

  • Related