What is the 3D equivalent of using Physics2D.OverlapArea to check for if a character is grounded?
In this Unity tutorial, http://unity3d.com/learn/tutorials/modules/beginner/2d/2d-controllers at 53 minutes in, they use Physics2D.OverlapCircle to check if the character is touching the ground so that they can enable jumping. Since I have a game of all 3D objects, it isn't working (at least that's my guess why its not working). So I am looking for a 3D alternative to program this same kind of mechanic.
Answer by OncaLupe · Nov 13, 2015 at 01:53 AM
You are right about why Physics2D.OverlapCircle isn't working. Unity has 2 physics engines, one for 2D and one for 3D. The two systems cannot interact with each other.
This is the 3D equivalent you're looking for:
http://docs.unity3d.com/ScriptReference/Physics.OverlapSphere.html
Thank you for not only giving me the right place to look for the solution but also explaining the two engines don't interact! The OverlapSphere worked but I ended up using a microscopic OverlapCapsule for pinpoint results.
Answer by killer-zog · Nov 12, 2015 at 10:59 PM
have you tried attaching a character controller? this is how to do it in 3D
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
void Update() {
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded)
print("We are grounded");
}
}
I'm trying to avoid the character controller because it doesn't simulate the physics I'd like.
well unity also has Physics.OverlapSphere
, you can try it. There are some other way to do it though, you can manually check for collision
void OnCollisionEnter(Collision collision)
{
}
and then use :
if (collision.gameObject != null)
{
if (collision.collider.shared$$anonymous$$aterial != null && collision.collider.shared$$anonymous$$aterial.name != "Physics material name")
{
isGrounded = true;
}
}
you can check if it is ground by using not equal
operator with the assigned Physics $$anonymous$$aterial
. this will make sure that only the collider that has the material assigned will trigger isGrounded
to true.
You can also replace that material checking to check of certain Tags
or Layers
too.
also whenever you tryto use something from 2D counterpart, just remove 2D
from behind and unity should have the same behavior for 3D
counterpart for most of the time. eg.
// 2D to 3D
OnCollisionEnter2D > onCollisionEnter
Physics2D > Physics
Also you can use raycasts to check the distance from the ground. Although i found the combination of collider(the code i posted above) and raycast works best. Using Raycast to check the distance from ground and also check if it colliding with ground collider!
Your answer
Follow this Question
Related Questions
Need Help with Jump Coding 0 Answers
Collision not detecting with collider and rigid body 0 Answers
How to check if dragged object collide with other object? 0 Answers
2D colliders doesnt work with Gravity Scale = 0 0 Answers
Objects not being destroyed 1 Answer