0

I have a NavMesh on the floor (with some hills and things) and my "soldiers stand" (I'm replicating a table top wargame) have NavMeshAgent. Everything works fine and smooth but the problem is that when agent goes uphill it stays vertical, which I guess is Ok for one person, but for instance for a tank makes no sense.. half of the thank floats in the air and the other half is inside the hill

This is an example:

enter image description here

Is there any way to ensure that the gameObject is tangent to the NavMesh ? the vertical axis should follow the normal of the NavMesh in the center point of the agent or something. To be honest I guess I'm not the first one to notice, but I wasn't able to find a solution

javirs
  • 1,049
  • 26
  • 52
  • I'm starting to think that navmesh.sampleposition just doesn't add the normal data in the NavMeshHit struct, or perhaps it can't based on the data definition for the navmeshes. Adding the normal data also seems sort of counter intuitive to the declaration of the function aswell. – moon cheese Dec 21 '17 at 16:02

1 Answers1

0

This seems like a hack way of doing it, but you can raycast onto the navmesh from your robot, and then you can use the normal of the rayhit to rotate your tank/robot.

Raycasting to navmesh: https://answers.unity.com/questions/325929/raycasting-on-navmesh.html

Then use this to rotate your robot based on the normal direction of the hit surface:

tranform.rotation = Quaternion.FromToRotation(Vector3.up, rayhit.normal);

This is an okay base to start with; additionally you can start adding in more raycasts to get a more additive/accurate rotation. For instance, if you had 4 tires on your robot, each ray would be fired from those points.

moon cheese
  • 471
  • 5
  • 15
  • from where to where would you raycast? From the center of the robot to .. where? something like the closest point of the terrain? and by the way.. how do you specify you wanna raycast only against the navmesh did not find it in the link ... :( – javirs Dec 21 '17 at 13:52
  • as I did not know from where to raycast I tried NavMesh.SamplePosition instead. I got a point of contact between the robot and the mesh but since the distance is zero (they touch) the hit.Normal is (0,0,0) .. any other solution? – javirs Dec 21 '17 at 14:02
  • Yeah, sorry, i'll admit, i jumped the gun here. I've been doing more tests with the normals and I can't seem to get it working either. I can get all of the properties except the normal. However, I can't think of any substantial case where you'd need to raycast onto the navmesh instead of the actual collider on the mesh. So maybe you could try just a normal Physics.raycast. – moon cheese Dec 21 '17 at 15:48
  • If raycasting directly onto the collider would work for your case, let me know and i'll update my answer. – moon cheese Dec 21 '17 at 15:50
  • this is not what I´d like to do because the collider is bigger than the mesh, due to some game mechanics that need it .. I could add two different colliders in two different layers but .. everything is too hacky .. – javirs Dec 21 '17 at 21:04
  • Yeah, i can't think of any other way other than hacky solutions. I tried one solution that worked pretty well. I made a separate transparent mesh with a collider, which was a low fidelty copy of the visible mesh. Then i just used the transparent mesh to map the navmesh. It's not really optimal but it decouples the navigation from the visible mesh, which is really nice to work with. Then i just used a Physics.raycast on the low fidelity transparent mesh. – moon cheese Dec 22 '17 at 08:08
  • where did you raycast from - to ? – javirs Dec 22 '17 at 09:24
  • I ray casted from the robot, downwards onto the low fidelity transparent mesh. – moon cheese Dec 22 '17 at 10:08