- Home /
Stopping navmesh agent next to the enemy
Hello everybody,
I am a Unity student and working on an RTS game.
I have a guy who has 3 weapons: knife, gun and UMP45. He can stab the enemy from a distance if next to the enemy and can gun down them when inside a certain distance of them. Range of the gun and ump obviously. I have issues with preparing the character for the stabbing animation. I would like to stop the main character next to the enemy soldier in a distance like 1 meter when I clicked on the soldier. I cannot stop the guy he keeps going into the position of the soldier and overlaps the enemy instead of stopping next to him by a distance. I have tried stopping distance, by some issues it is not working in my case. Remaning distance gives me weird amounts when I measure it on the scene.
Here is what I have so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.EventSystems;
public class AiMoveToClick : MonoBehaviour
{
// Making the main character to be able to move where the player clicks //
public GameObject player;
// Reference for the navmesh agent component
NavMeshAgent playerNavmesh;
Animator playerAnim;
public bool canStab;
WeaponSelection weaponSelectionScript;
Transform target;
public bool enteredTrigger;
Vector3 offset = new Vector3(4.0f, 0, 4.0f);
// Start is called before the first frame update
void Start()
{
playerNavmesh = GetComponent<NavMeshAgent>();
playerAnim = GetComponent<Animator>();
weaponSelectionScript = FindObjectOfType<WeaponSelection>();
playerAnim.SetBool("IsWalking", false);
canStab = false;
}
// Update is called once per frame
void Update()
{
// if we click with the left mouse button
RayCastCheckAndMove();
if(Input.GetKeyDown(KeyCode.G))
{
playerAnim.SetTrigger("IsStabbing");
}
}
public void RayCastCheckAndMove()
{
if (Input.GetMouseButton(1))
{
target = null;
}
if (Input.GetMouseButton(0))
{
// if the UI is selected do not do anything
if(EventSystem.current.IsPointerOverGameObject())
{
return;
}
// temp storage for Raycast information , new Raycast variable
RaycastHit hit = new RaycastHit();
// IF Raycast inside Physics class
// If the raycast coming from the camera hits the position (where the mouse click happens)
// store the information into hit variable and take a distance for Raycast as 100
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity))
{
if (hit.collider.CompareTag("Plane"))
{
playerNavmesh.stoppingDistance = 0;
// move the navmesh agent to the mouse click's position
playerNavmesh.destination = hit.point;
//Debug.Log("You clicked on: " + hit.collider.tag);
}
if (hit.collider.CompareTag("Soldier1") && weaponSelectionScript.knifeIsSelected)
{
//canStab = true;
target = hit.transform;
playerNavmesh.destination = target.position + offset;
if (playerNavmesh.remainingDistance <= 0)
{
playerNavmesh.speed = 0;
//playerAnim.SetBool("IsWalking", false);
playerAnim.SetTrigger("IsStabbing");
}
}
}
// if remaining distance to destination is more than 0 meaning we are on our way
if (playerNavmesh.remainingDistance > 0)
{
// switch on the walking animation sequence with a boolean
playerAnim.SetBool("IsWalking", true);
}
}
// if the remaining distance is 0, meaning we have arrived to destination
if (playerNavmesh.remainingDistance <= 0)
{
// switch off the walking animation sequence with a boolean
playerAnim.SetBool("IsWalking", false);
}
}
}
Can I get help on this please?
Thank you very much