Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using NavMeshSurface2d changes z position of an agent moving on the navmesh #21

Open
DamodarSojka opened this issue Mar 10, 2020 · 8 comments
Assignees

Comments

@DamodarSojka
Copy link

I have a player gameobject placed on a navmesh with NavMeshSurface2d rotated -90 on X just like the WIKI says. When the "game" starts the z of the transform holding the player gameobject is set to --0.7708252.
The runtime navmesh generation works, colliding works, agent.SetDestination works.
Manual movement (keyboard arrows) don't work, the player object get's stuck from time to time when moving up or down, probably to the Z missmatch.
As a workaround I have code in the update to set the Z to 0 whenever it's not (the value is getting set to the -0.7708252 constantly actually), but that results in some other issues and just feels bad.

Using NavMeshSurface doesn't generate the issue, but of course doesn't work for the 2d case anyway so quite hard to say if it's really NavMeshSurface2d but I have been randomly disabling components and that's my best bet now.

Let me know if you can help and if I can provide any more clues here.
Cheers

@h8man h8man self-assigned this Mar 11, 2020
@h8man
Copy link
Owner

h8man commented Mar 11, 2020

Hi,

So NavMeshSurface2d bakes navmesh, after that, its 'native' for unity. So lets look what surface do we get. The Z offset is kind of strange, what version of unity you are using? Rotate your scene and check is it flat against grid?

On demo scene I see some bubble, maybe I have some defect in matrix transformation
image

Also, check your offset. Agent sits flat on navmesh, but Z is 0.5, because of offset
image

Please look as this issue #20
Also, you can ask questions on Navigation part of unity forum.

@DamodarSojka
Copy link
Author

Unity version - 2019.3.0f6
Rotate your scene and check is it flat against grid? - Not sure if that's what you mean but I used camera to just check if things are flat when looking at the from the side in 3D scene view, they are
Base Offest - was indeed set to 0.6875, no idea why, setting it to 0 doesn't help

"Giving the velocity.x a tiny value (0.0001f,Y,0) 'fixes' this - the agent will then move as expected." - I guess I can use this hack, but given my lack of experience I am not really able to distinquish if the other navigation related issues I am having are related to this or no.

Also, you can ask questions on Navigation part of unity forum. - it's quite hard to tell when the forum should be used vs the issues here, in general I think GitHub issues are much better UI, search and can be tied more closely to an actual piece of code that might be in question (as it's now). Any advice on that? I am quite new to the ecosystem.

@h8man
Copy link
Owner

h8man commented Mar 11, 2020

Hi,

If navmesh is okay, and only thing that doesn't work is agent movement with keys - its better to ask question of forum, as I don't have Agent 2d wrapper for you.

Post new thread in https://forum.unity.com/forums/navigation.79/ with code snippet of how do you move agent.

@WoodlandDevelopment
Copy link

I have a player gameobject placed on a navmesh with NavMeshSurface2d rotated -90 on X just like the WIKI says. When the "game" starts the z of the transform holding the player gameobject is set to --0.7708252. The runtime navmesh generation works, colliding works, agent.SetDestination works. Manual movement (keyboard arrows) don't work, the player object get's stuck from time to time when moving up or down, probably to the Z missmatch. As a workaround I have code in the update to set the Z to 0 whenever it's not (the value is getting set to the -0.7708252 constantly actually), but that results in some other issues and just feels bad.

Using NavMeshSurface doesn't generate the issue, but of course doesn't work for the 2d case anyway so quite hard to say if it's really NavMeshSurface2d but I have been randomly disabling components and that's my best bet now.

Let me know if you can help and if I can provide any more clues here. Cheers

I know this was a while ago but did you end up finding a solution to this problem? I am having the exact same issue and have yet to find a solution to the Z axis being thrown off of 0.0f.

@h8man h8man reopened this May 27, 2022
@h8man
Copy link
Owner

h8man commented May 27, 2022

@WoodlandDevelopment To set your Agent flat to the surface you can use Agent offset, but anyway you will have drift 0.0001f.

The workaround is to disable position update and move gameobject by script

The nextPosition is coupled to Transform.position. In the default case the navmesh agent's Transform position will match the internal simulation position at the time the script Update function is called. This coupling can be turned on and off by setting updatePosition.

@jcpickett1
Copy link

jcpickett1 commented Aug 20, 2022

Hi @h8man, thanks for making this package, super handy and easy to use.

I tried disabling updatePosition, but found that essentially broke collisions and let me travel through anything, not sure if this is unavoidable due to the coupling, or am I missing a step?

Using the drift solution I had some small problems with stuttering (maybe due to float precision/rounding to zero?) but I added some dithering on the horizontal velocity so object tends back to Y axis, which let me user a more aggressive horizontal vel and eliminated the stuttering+drift.

Vector2 dither = Vector2.right * 0.001f * (Random.Range(0,1) >= 0.5f ? 1 : -1);
_myNavAgent.velocity = _movementInput.ReadValue<Vector2>() * _playerSpeed + dither;

@h8man
Copy link
Owner

h8man commented Aug 20, 2022

@jcpickett1 Thanks for feedback.

As I mentioned few times, I don't use Agent so I didn't get deep into it. But it seems Agent.NextPosition is simulated position and should include navmesh and obstacle avoidance. It is mentioned in manual

https://docs.unity3d.com/ScriptReference/AI.NavMeshAgent-nextPosition.html

@Dragoonian
Copy link

Whenever I hit play, my agent gets sent to the other side of the 2d plane. No matter the offset, its the same distance on the other side

JacksonPepperoni added a commit to psw1305/TeamProject-2DRoguelike that referenced this issue Dec 18, 2023
임시로 해결했습니다. 지금은 완벽히 고칠 수 없나 봅니다...
h8man/NavMeshPlus#21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants