Why Do Raycasts Ignore NavMesh Agents
Asked Answered
V

7

0

I built a simple game using Unity’s built in NavMesh Agent AI system.

My player has a laser that uses a raycast to apply a force to the enemy to not it back. This works… for the first couple seconds of the game. Then, it is as if the NavMesh Agent gets “locked” into movement and is no longer effected by the raycast. I have found a few (very small number) other questions on UA, but none have answers that apply to my situation.

Is this a bug in Unity?

Or Am I missing some aspect of NavMesh Agents.

Vadnais answered 11/2 at 22:17 Comment(1)

I to am experiencing this issue and have yet to find a solution. It seems that my raycast collides for some arbitrary amount of seconds and then stops working. I'm currently using Physics.Raycast() but I have also tested with a sphere cast. My code was set up as follows Physics.Raycast(mainPosition, dirOne, out hitOne); Please help!

Swear
S
0

I believe I have a solution to the problem.

I believe the problem occurs when you have both a Navmesh Agent on the object as well as a Rigidbody. Remove the Rigidbody and it should work fine.

Swear answered 11/2 at 22:13 Comment(2)

I will try this. I have a temporary fix right now. Triggers still work fine for navMesh agents so I have been just using triggers, but i will try your solution.

Vadnais

Meatlof you just saved me a lot of nerves and time :)

Heat
F
0

Try replacing the BoxCollider on your object with a MeshCollider.
This is probably due to mesh sort order; RaycastAll() will also work.

Fairminded answered 11/2 at 22:15 Comment(0)
F
0

I realize this is years after the fact, but I was having this same problem.

The only thing that was fixing it was what @Swear said about removing the Rigidbody, but then that made the navigation wonky. So what I did that seemed to fix it was I changed the Collision Detection on the Rigidbody component from Discrete, to Continuous instead.

That seemed to fix it.

Footpace answered 11/2 at 22:14 Comment(0)
M
0

Changing from Discrete to Continuous does not solve the issue. Rigidbody still needs to be removed.

Mendez answered 11/2 at 22:16 Comment(0)
C
0

Hey, I know this is a really old post. But I had this issue and fixed it by setting the Rigidbody as Kinematic.

I was trying to get a ragdoll to work so I used:

foreach( Rigidbody rigidbody in GetComponentsInChildren<Rigidbody>() )
    rigidbody.isKinematic = true;
Candleberry answered 11/2 at 22:15 Comment(1)

I've been wandering about this for days. Thank you

Coquito
M
0

I had the same problem. Changing Interpolate option to Interpolate and setting Collision Detection to Continuous Dynamic solved my mine.

Milker answered 11/2 at 22:16 Comment(0)
R
0

The raycast works fine, the problem is that you have two entities that try to take control of your gameObject position - the dynamic rigidbody and the navMeshAgent. They both have their internal positions and try to synchronize it with transform. It’s quite possible that when both are present, they get desynced and their relative positions change. Just go to the scene view when this error manifests and see if the navMeshAgent, rigidbody+collider and the visible mesh (or whatever you are using) are in the same position or not.

This makes sense - the NavMeshAgent tries to interpolate the position on a path on a navMesh, the rigidbody has its own opinion on where the object should be, and they are incompatible. So you must pick one of them to “own” the position. (Or, you could disable position updates of both and handle it entirely in your script)
I’d say that if you want to have a rigidbody behaviour, then you want to keep the rigidbody behaviour… and disable the navmesh position update. You can actually disable the position update on the navMeshAgent, but it will still internally move towards the target - it just won’t move the gameObject. Now I am not sure what’s the best way to handle this, but one thing I’d suggest is to set the navMeshAgent velocity and acceleration to zero, disable the position update, then every FixedUpdate tell the navMesh your new position from the physics, and finally, take the navMeshAgent’s desired direction that comes from its path and calculate forces to move your rigidbody that way.
If there’s some built-in solution to this conflict, anyone pls enlighten me. But I am not aware of it.

Rockie answered 12/2 at 3:54 Comment(1)

Yeah I've just noticed it's a 10 year old question. Anyway, I hope it still helps someone looking for the answer...

Rockie

© 2022 - 2024 — McMap. All rights reserved.