Nav Mesh Agent destination not lining up with actual target.
I've recently converted my movement script for NPC's in my game from manually moving their rigidbodys to letting the navmesh agent move them. I am simply using the .SetDestination method of the agent. I've also just set the destination property. In both cases the destination doesn't line up with the actual target I'm wanting them to go to.
I am setting the destination to the transform.position of another gameobject. When I debug.drawline to the transform.position of the target, and the agent.destination I get two different lines. The farther away the target is the bigger the offset.
Any ideas?
I've found part of the problem. I had my base gameobjects scaled up. I re-made all my prefabs so that the base gameobject with the navmesh agent was at a scale of 1,1,1 and now the offset is much smaller but there is still an offset. Also when I place effects at the navmesh agent objects transform.position they are also not lining up.
Are these bugs with navmesh?
Navmesh destinations will always get clamped to actually be on the navmesh so if your target gameobject has its position floating in the air above it, then Unity will search for the closest place on the navmesh which may in some cases not be what you want. Do you have a screenshot?
That's not the problem. $$anonymous$$y game is essentially 2D, top down, X and Z only. All objects are on the same plane.
Here is a couple of screenshots. The red line is a debug.drawline from the agent's transform.position to the agent.destination. The black line is from the agent's transform.position to the target's transform.position. When i set the destination I give it the target's transform.position but this is what it turns into. There is a difference in the Y which but the X and the Z are also offset.
Agent destination aside, why would the agent's transform.position be in different places for each set of lines? They should be the same value right?
To eli$$anonymous$$ate the possibility that a difference in Y was causing the problem I've tried just passing the X and Z to the destination and setting the Y to the same as the Y of the navmesh. The result is the same.
Answer by 2-Zons · Nov 27, 2015 at 04:04 AM
I have found the problem. The target in question is a nav mesh obstacle. When given a destination in an inaccessible area it picks a location close to the destination but not right at it. When i give a destination of an object that is not an obstacle they line up perfectly.
Your answer
Follow this Question
Related Questions
Problem with NavMesh 0 Answers
Navmeshs and Trees 1 Answer
NavMeshAgent not teleport on y axis 0 Answers
Click to move NavMeshAgent Script not working... please help! 1 Answer
Need help with Nav Mesh Agent getting "stuck" at high speeds 0 Answers