Search Unity

Click To Move + Root Motion + Navigation path

Discussion in 'Navigation' started by Raptcha911, Mar 23, 2016.

  1. Raptcha911

    Raptcha911

    Joined:
    Sep 19, 2014
    Posts:
    24
    Hey,
    Like the title says, I want to use mouse click on a terrain, get unity's pathfinding to generate a path to the target and use mecanim root motion to move my player in the path.. Yes!! I have looked around for a solution for this on all the forums, none of them have the solution for what I'm looking for.. I've already implemented this in my game, but I'm having trouble getting it right..

    I just need to orient the player to the path and the root motion will handle all the movement.. I cant use path.Corners[] coz it keeps changing as the navmesh agent moves.. One possible solution for this is to make the player look at the navmeshagent controller (that agent ghost thingy), but I'm finding it very troublesome to set the navmeshagent speed to match my rootmotion movement speed.. So the player is rotation really weird for a brief second as soon as the new target location is created and then it becomes alright.. I need to make a LOL/DOTA 2 click to move functionality..I dont mind changing my entire click to move system if there is a better approach to this..

    Any input is appreciated!!.. Thanks in advance...
     
  2. UziMonkey

    UziMonkey

    Joined:
    Nov 7, 2012
    Posts:
    206
    I'm doing this in my current project. Set agent.updatePosition to false first off, since root motion will handle motion. Next, in your update method you need to get the agent.nextPosition and feed it to your blend tree (if you're using one) so it knows in what direction to animate. There's some code in the docs on how to do this. Rotation is still controlled by the nav mesh agent. Finally, in OnAnimatorMove, you need to assign the animator position to your transform position and to agent.nextPosition to keep them in sync. After that, root motion with nav mesh agent should work.

    One thing to note is that I'm having a problem with accuracy. Agents driven by root motion often fail to land on their marks exactly, so you need to loosen how you detect when an agent has "arrived" a bit, or maybe lerp the last little bit.
     
  3. Raptcha911

    Raptcha911

    Joined:
    Sep 19, 2014
    Posts:
    24
    Hey,
    I got the movement speed synced with the navagent move speed.. Its working fine now, but the rotation is not working right.. I tried it both ways.. First method is by allowing the navmeshagent component to handle the rotation, but somehow the rotation speed is not nearly enough and changing the angular speed is not helping.Even increasing the acceleration is not helping..
    Second method is by handling the rotation through script.. This is what I'm using
    Code (CSharp):
    1.    Vector3 direction = (myNavAgent.nextPosition - transform.position).normalized;
    2.             Quaternion lookRotation = Quaternion.LookRotation(direction, Vector3.up);
    3.             Quaternion targetRotation = Quaternion.Slerp(transform.rotation, lookRotation, step);
    4.             targetRotation.x = 0;
    5.             targetRotation.z = 0;
    6.             transform.rotation = targetRotation;
    But its giving a very weird result.. Object keeps rotating around itself coz the agent.nextposition is almost as same as the transform.position..
    Can anyone help me out??