Click to Approach & Attack script for NavMeshAgent required

I am making simple 3d RPG game (I want it Diablo style as i am fan of this game, but without multiplayer), and I have a problem. The thing is, when I made a script for “Click to Approach and Attack” (with sword or some other weapon for close fight), it worked almost nice, except it didn’t have NavMeshAgent component, so player could go through enemies or walls etc.

Then I decided to add NavMeshAgent to player/enemies (added some simple runtime level generator which I found on Unity tutorials and just adjusted it to my 3d game), and NavMeshAgent started walking fine on Runtime-baked nav-meshes (it is click to move/attack game).

However, I am struggling with click to approach (plus attack if necessary) script. I tried different approaches like using Raycast hit to detect mouse click on terrain/enemy (also added Animator controller for idle/walk/sword attack etc animations and it also seems to be a little bit buggy sometimes) plus having “attack distance” and “stop walking distance” variables to detect when to move/attack, but this still doesn’t work easy way as I expected it to be.

I tried click-to-move tutorial (Learn game development w/ Unity | Courses & tutorials in game design, VR, AR, & Real-time 3D | Unity Learn) and tried to adjust shootDistance (changed it to sword_attack_distance), but that still doesn’t approach&attack properly (sword_attack_distance changed to something like 1f).

This is a video of what I did before using click-to-move tutorial script:

As you can see, player sometimes doesn’t walk when I click on destination point. It seems to be a NavMeshAgent problem. Is it Path finding problem (I suspect it is)? I don’t use any additional pathfinding scripts, but just NavMeshAgent.setdestination(enemy.transform.position) or NavMeshAgent.setdestination(hit.transform.position) (when use raycast hit for terrain/enemy) and expect agent to walk properly to expected place (previously used Vector3.MoveTowards, but this was without navmeshagent, so passed through walls/enemies etc as I said, so this probably won’t work for a game).

Player can’t walk to destination position and just plays walk animation on one place, but not moving ;-(

I’ld be very happy if someone can provide a solution (link to working Click-Approach-and-Attack with sword etc script from probably available open-source project like RPG tutorial) or just write code suggestions here…

Should I add some special NavMeshAgent pathfining (Etc) script to make player’s agent walk as expected? I’m sad it doesn’t work… ;-(

Try looking at this Unity asset. without seeing your code it would be hard to be helpful otherwise.

Casting an overlap sphere with a small radius is a good way of finding all enemies near player.

Thanks. I’ll take a look at asset and Physics.OverlapSphere (is it like Raycast hit but without need of mouseclick just to detect gameobjects around agent? How this can help for click-to-act mechanics/logic?)

Here is a repository of a game I’m making (it’s open-source at the moment):
https://github.com/ivandonetsunity3d/Kshatriya3d

The player’s scripts are here: https://github.com/ivandonetsunity3d/Kshatriya3d/tree/master/Assets/code/Player

The script without navmeshagent which worked more or less nice expect passing through gameobjects:
https://github.com/ivandonetsunity3d/Kshatriya3d/blob/master/Assets/code/Player/ClickToAttackOrMove.cs

And the same with added navmeshagent:
https://github.com/ivandonetsunity3d/Kshatriya3d/blob/master/Assets/code/Player/ClickToAttackOrMoveNavMesh.cs
This one creates the bug which is recorded on the video