- Home /
check object if grounded
how can i check if the object is already grounded on the floor?
because my AI is flying while following its waypoint... i want him to stay on ground while following its path ...
here is the code
var waypoint:Transform[];
var speed:float=20;
private var cwaypoint : int;
var gravity=20.0;
private var grounded : boolean = false;
function FixedUpdate () {
if(cwaypoint<waypoint.length){ var target:Vector3=waypoint[cwaypoint].position; var moveDirection:Vector3=target-transform.position;
var velocity=rigidbody.velocity;
if(moveDirection.magnitude<1){
cwaypoint++;
} else{ velocity=moveDirection.normalized*speed; }
} rigidbody.velocity=velocity;
transform.LookAt(target);
}
Post the code to get better results. Especially the part where the AI moves.
Answer by A Lee · Dec 14, 2010 at 04:43 PM
Yes, as mentioned, you should add a rigid body component to both the ground floor and the object that you want to check and see if it is grounded. You can then test for a collision between the two objects, and tell your AI what to do when a collision is detected, or not.
im using terrain as the floor is it ok to add rigidbody on it?
Terrain already has a collider, which is good enough. You don't need to add anything else to it. (Rigidbody is needed on objects that are controlled by physics.)
Answer by Joshua · May 09, 2011 at 05:53 AM
I'd say simply cast a ray downward, see what the RaycastHit.distance is, and if it exceeds a certain range add a downward force.
Your answer
![](https://koobas.hobune.stream/wayback/20220613091021im_/https://answers.unity.com/themes/thub/images/avi.jpg)