how do i make a dodge roll skill with navmeshagent?

My game is click to move, when you right click your controlled unit/units will path towards the click navigating around obstacles using the built in navmeshagent. I want to make a player skill that lets their hero unit do a rapid roll in the direction of the cursor and ignore pathing but still collide with an obstacle if they tried to roll into a wall. I do not want the roll to go through walls, but i do want it to be able to be cast in the direction of the wall. Doesn’t this mean that i’d have to use navmeshagent.destination so that the unit doesn’t go through the wall? But if i use navmeshagent.destination the roll won’t be aimed at the wall, it would be aimed down the path that was calculated to get around the wall wouldn’t it? I want the roll to be aimed at the cursor regardless of where the player chose to click, but i don’t want the unit to ever go through an obstacle. Sliding along a rounded surface would be okay as long as the destination of the roll was still in the general direction of the cursor.

when doing the roll you don’t use NavMeshAgent at all.

During the roll, you stop the NavMeshAgent all together, it has no destination, it does not control the movement.

You’ll instead put some sort of collider on it… CharacterController, or Rigidbody (which ever you prefer). And then you’d use the appropriate movement methods to move your character while playing the appropriate animation. Once complete turn the NavMeshAgent back on.

1 Like

I don’t know anything about CharacterController, does it conflict with Rigidbody? I’m using rigidbody on all units because, if i recall, the gameObject needs a rigidbody to detect a collision with my projectiles that are using OnTriggerEnter. Since rigidbody is already on my player would you suggest using addforce in the direction of the cursor?

If you know how to use them correctly, you can use CharacterController and Rigidbody together.

Usually not on the same GameObject. Usually my players are a CharacterController on its root, and then inside it another GameObject with capsule collider and Rigidbody (kinematic) for hitbox.

Though, note, I said you can also use a Rigidbody… it was CharacterController OR Rigidbody. Whichever you’d like.

Have you done any non-ai tutorials? Like a FPS tutorial or something, showing you how to set up a player that is directly controller by input. Because this is effectively what you’re doing… for the moment of the roll, your player controls like a regular directly controlled player, rather than a passively navigating AI unit.

You may want to start there first. Like step away from your ai unit… and go create a player unit that directly responds to user input (follow one of the many tutorials that exist). If you can get that working, then merge the concepts with your AI controlled player unit.

1 Like

My player unit isn’t controlled by an AI, my game is click-to-move so of course the pathing is automated. The first step of every action is to move into range using navmeshagent.destination. Is navmeshagent not designed to be used on players?

I mean that.

Your player is indirectly controlled in that the player tells it to go to a location, and you use pathfinding (a form of ai) to determine the appropriate path.

You need to add a function that is more directly controlling. Where the players input result in a direct reaction of the player avatar. Hit jump button, player jumps. Push forward, player moves forward. Push dodge button, player tumbles.

Create one of those… then merge them.

Navmeshagent is intended for avatars that pathfind… you need to create a functionality unrelated to pathfinding.

1 Like

Okay i’ve gotten my code organised and got the roll working. I tested with and without disabling navmeshagent and determined that it makes no visible difference. I put the velocity gain and loss in a coroutine and have the ability on a cd so you can’t do anything weird to get the unit stuck sliding. Thanks

Alright i spoke too soon, it’s “working” but the direction is wrong. I’m using physics.raycast for almost everything i do in the game. Where to move, who to attack, and the direction to roll. In fact, each action in my game requires a click and the RaycastHit is sent through the pipes to various places. But
rigidBody.velocity = hit.point.normalized * speed;
is giving me a strange direction. Mostly it’s just always shoving me into the top left corner of the map. This confuses me because agent.destination = hit.point works. I’ve never had my player navigate into the top left corner.

OKAY it’s really working this time. Here it is for anyone else:

    public override void CastAbility3 (RaycastHit hit) {
//all that other stuff like mana and CD
        StartCoroutine (PerformRoll (hit));

}

    IEnumerator PerformRoll (RaycastHit hit) {
        rigidBody.velocity = (hit.point - transform.position).normalized * speed;
        yield return new WaitForSeconds (.5f);
        rigidBody.velocity = Vector3.zero;
        rigidBody.angularVelocity = Vector3.zero;
        navMeshAgent.destination = transform.position;
    }