Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

Navmesh follow and stop at a distance

$
0
0
Hi so what im trying to create is. the play can right click on an enemy and he will follow at a certain distance. which is working fine. but what i want it to also do is stop at that distance too. currently if the enemy stops he will try and go to its exact position instead of stopping a little bit away this is what i have currently using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; public class Attacking : MonoBehaviour { NavMeshAgent agent; Transform target; public float distance; public float followDistance; // Use this for initialization void Start () { agent = GetComponent(); } // Update is called once per frame void Update () { if (Input.GetMouseButton(0)) { target = null; } if (Input.GetMouseButton(1)) { RaycastHit hit; if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100)) { if (hit.collider.gameObject.tag == "enemy" || hit.collider.gameObject.tag == "Player") { target = hit.collider.transform; } } } if(target != null) { distance = Vector3.Distance(transform.position, target.position); if (followDistance <= distance) agent.destination = target.position; } } }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>