Example Analysis of ground Detection Scheme in Unity
This article mainly introduces the example analysis of the ground detection scheme in Unity, which is very detailed and has a certain reference value. Friends who are interested must read it!
1. Ordinary ray
At the character coordinates (usually the soles of the feet), a downward ray is emitted with a length of about 0.2
It is only suitable for simple terrain. The following problems are often encountered in practical use.
When the collider is used to hit the ground, there will be a certain interspersed at some time, so the lowest point of the character may penetrate the ground, the point where you emit the ray may be below the ground, and the ray can't detect the real ground all the time, so the character is always hanging in the air.
When the character is walking the slope, the midpoint of the character may be a short distance from the ground, which is often enough to make the judgment mechanism think that the character has left the ground. If you increase the length of the ray, it will alleviate the ramp problem to some extent, but it will reduce the accuracy of jump judgment: every time the character jumps, there will be a short distance, which is actually off the ground, but still returns isGround = true
Using System.Collections;using System.Collections.Generic;using UnityEngine; public class RaycastTest: MonoBehaviour {private bool isGround = false; private Rigidbody2D myRigidbody2D; void Awake () {myAnimator = GetComponent (); myRigidbody2D = GetComponent ();} void FixedUpdate () {Debug.DrawRay (transform.position, Vector2.down * 0.11f, Color.red); RaycastHit2D hit = Physics2D.Raycast (transform.position, Vector2.down, 0.15f, 1