- Home /
2 questions about Terrain(AI on terrain)
Hi there!
So I just have a couple questions.
I made a waypoint script which makes my enemy walk towards it. Which works amazing. However.... It also makes my enemies walk right through terrain to get to said waypoint. How can I make it so they can't go through terrain and instead have to walk over hills or go down them.
When I put on a Rigidbody and use gravity, they just fall through the terrain... I have a trigger collider but i need it. because its the hitbox
If you could help me that would be great!
Thank you!
-Joel
This probably isn't a Terrain or an AI question. The movement AI just picks the waypoints, but actually walking towards it uses the same code as walking to a random point. Likewise, I'd guess that if the ground was made of huge sloped cubes, you'd have the same problems with falling through and walking through.
A common question here is players falling through the ground. Lots of reasons it does that, and lots of fixes, some simple. Also lots of "Why am I moving through objects" questions here. $$anonymous$$ost common problem is using Translate to move.
Answer by YoungDeveloper · May 05, 2014 at 04:09 PM
Hi, in my opinion:
Most optimized way of keeping any ai above the ground would be using single raycast down. From it you get terrain or other object hit point, and you can set needed height for object itself.
Haven't worked with a terrain for a while, as i remember there might be couple problems. First is, player is a bit inside/lower the terrain itself so it's just falling through. Other was terrain and colliders related, there was something terrain didnt like about mesh colliders, but i think this is not the case.
How would I do this? And how effective would this be?
Its just a box colliders... would that be effecting it?
//Call this every frame after you move the NPC
void StayOnGround()
{
Ray downwardsRay = new Ray(transform.position, Vector3.down);
RaycastHit hit;
if(Physics.RayCast(downwardsRay, out hit, 1.0f))
{
//You could check hit.collider.transform/tag to make sure
//you're only trying to stand on the terrain
transform.position = hit.point;
}
}
Physics calculations will always take more than just single raycast. Just cast it down from your ai, best is to create new gameobject, child it and place it somewhere in the middle of your ai, and cast from it.
Effecting what? You said that your player/ai is falling if gravity enabled, it is probably because collider is inside terrain.
Your answer
![](https://koobas.hobune.stream/wayback/20220613144315im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Does anyone know ow to import World Machine tiled meshes into Unity? 0 Answers
AI line/field of sight with Raycast 0 Answers
Terrain: Change color/alpha of Layer (or how to let my texture-grass wither, become dry and brown) 0 Answers