I released new package on Asset Store. Its Dynamic Obstacle Avoidance package.
Its usefull for using on npc characters, movable objects without nav mesh carving and behaviour that need local avoidance.
Here is demo video:
… and here is link to demos so you can test how it works. DEMO/S
Usage:
In this demos I made it so chracters can push objects if there is no other way of traversing through obstacle but you assign high repulsion value so characters will not go near. It all depends how you tweek certain object.
Also you can assign character more persistance value in path following so it will more likely to go through obstacles or asssign lower persistance to make them less likely to go through.
Hello,
On objects all it takes is to put ‘Obstacle’ component on it
On npcs, you have to implement IAvoider interface and
add:
’ ObstacleManager.CalculateObstacleAvoidance(ref move, this);’
after all movement ( after path is calculated, being it NavMesh or other )
There is also an option to smooth npc avoidance if you like.
For that you can copy code from examples which is between SMOOTH_MOVEMENT macro.
Look at the _add_local_avoidance() method, its the main one.
Note that in some npc scripts I set movement y to zero because npcs are walking on ground and physics is responsible for Y axis movement ( jumping, falling )
But you dont have to do that if npc if flying.
Hi, I have one more question if you don’t mind. Will this support automatic integration with an existing movement system script that is already being used on an enemy???
It doesn’t matter what movement calculations you are using as long as you put avoidance method/s last ( after movement)
It takes existing movement vector ( velocity ) and modify it to take avoidance into account.
Hi,
Ok, try this:
get world move direction which is:
Vector3 direction= (navAgent.steeringTarget - this.transform.position).normalized.
After that add ObstacleManager.CalculateObstacleAvoidynce( ref direction, this);
Then you’ll get modified direction for avoidance.
And than you can continue like you do:
Thanks, this helps. Now the actors are at least playing the correct directional move animations, however they still move along the path without avoidance. I think it’s because the avoidance velocity isn’t added to the actual navmeshAgent velocity which moves the ai? I’ve looked through the example scripts but couldn’t find anything helpful for my case. Can you tell me what I need to add?
Thank you for your help! I did what you posted but can’t get it to work. Now the navagent moves but the animator avatar won’t follow, it’s keeping its initial position doing animations and turning like it’s supposed to but not changing position.
I’ll study the example scripts some more, maybe i’m overlooking a simple thing.
Oh yes, your character was moved by nav agent.
Lets try implement root motion controlled by script in OnAnimatorMove method
Add this code in OnAnimatorMove():
if (Time.deltaTime > 0 )
{
Vector3 v = (m_Animator.deltaPosition * moveSpeedMultiplier ) / Time.deltaTime;
// IF USING RIGIDBODY
if (m_Rigidbody)
{
if (m_Rigidbody.isKinematic)
transform.position += v * Time.deltaTime;
else
{
if (m_Rigidbody.useGravity) v.y = m_Rigidbody.velocity.y;
m_Rigidbody.velocity = v;
}
}
// ELSE
else
{
transform.position += v * Time.deltaTime;
}
}
Just make sure that Root Transform Position ( XZ )is not baked into pose.
Im my script ‘CharacterObstacleAvoidanceAgent’ you can see how I implemented NavMeshAgent and obstacle avoidance.
Disabling NavMeshAgent update, calculating path manualy ( like here ) and moving character by root motion by script.
Look in ‘CharacterObstacleAvoidanceAgent.cs’ and ‘ThirdPersonCharacter.cs’
Thank you very much for your time. The thing is that I try to avoid rootmotion because the movement is too unprecise for my project.
That’s why I decided to go with something like:
i also dont use root motion, just the navmeshagent using the animator deltaposition like is show in the previous post. I dont have clear if it works with this solution.
To use on NavMeshAgent you have to find the way to modify agent’s position to include avoidance direction.
I did it by using NavMeshAgent.nextPosition.