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… ;-(