This is basically a job, but i’m doing practises so i will make one and will post it here.
Done, here it is: `using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class unity_3OPiEasy5bgXPg : MonoBehaviour{
private Animator animator;
private NavMeshAgent Agent;
private bool idle, walk,shoot,isnearby;
public GameObject Obj;
public Collider colzone;
public bool shooted=false;
public float distancetoshoot;
// Start is called before the first frame update
void Start()
{
idle = true;
walk = false;
shoot = false;
animator = gameObject.GetComponent<Animator>();
Agent = gameObject.GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update()
{ CheckStatus();
animator.SetBool("running", walk);
animator.SetBool("Shoot", shoot);
animator.SetBool("Idle", idle);
}
void OnTriggerEnter(Collider col)
{
if (col.gameObject == Obj) { isnearby = true; }
}
void OnTriggerExit(Collider col)
{
if (col.gameObject == Obj) { isnearby = false; }
idle = true;
walk = false;
shoot = false;
}
void CheckStatus() {
RaycastHit hit;
Vector3 Dir;
if (isnearby)
{
Dir = Obj.transform.position - gameObject.transform.position;
idle = false; walk = true; shoot = false;
Agent.SetDestination(Dir);
Ray r = new Ray(gameObject.transform.position, Dir);
if(Physics.Raycast(r,out hit,distancetoshoot))
{
idle = false; shoot = true;walk = false;
//YourShoot Script
}
}
}
}
OBJ=theTarget Distancetoshoot=the minimal distance to shoot Colzone=its a collider Trigger used as zone Shooted=the animator set this trigger on or off in a shotpoint, use it for intantiate a bullet for exmple, if you dont use it, just delete it; if you dont use a zone, then use this one:using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class unity_3OPiEasy5bgXPg : MonoBehaviour
{
private Animator animator;
private NavMeshAgent Agent;
private bool idle, walk, shoot;
public GameObject Obj;
public Collider colzone;
public bool shooted = false;
// Start is called before the first frame update
void Start()
{
idle = true;
walk = false;
shoot = false;
animator = gameObject.GetComponent<Animator>();
Agent = gameObject.GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update()
{
CheckStatus();
animator.SetBool("running", walk);
animator.SetBool("Shoot", shoot);
animator.SetBool("Idle", idle);
}
void CheckStatus()
{
RaycastHit hit;
Vector3 Dir;
Dir = Obj.transform.position - gameObject.transform.position;
idle = false; walk = true; shoot = false;
Agent.SetDestination(Dir);
Ray r = new Ray(gameObject.transform.position, Dir);
if (Physics.Raycast(r, out hit, distancetoshoot))
{
idle = false; shoot = true; walk = false;
//YourShoot Script
}
}
}`
Here is a video where i show you how to use it https://youtu.be/eWM5ZRCFRuE
it posted like... prety bad. here is a link to Mediafire https://www.mediafire.com/file/ovhn6lijellv94b/unity_3OPiEasy5bgXPg.cs/file
– DankillerCoStore