Hello, i am making a game where player finds an enemy and navigate towards him automatically and shoots ,player has 3 animation idle walk and shoot, i m not able to write proper script.any help?

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class Script : MonoBehaviour
{
private Animator anim;
private NavMeshAgent navMeshAgent;
private bool mrunning = false;
public GameObject enemy;

void Start()
{
    anim = GetComponent<Animator>();
    navMeshAgent = GetComponent<NavMeshAgent>();

}

void Update()
{
    navMeshAgent.SetDestination(enemy.transform.position);
    mrunning = true;
    anim.SetBool("running", mrunning);

}

}

Hello, i am making a game where player finds an enemy and navigate towards him automatically and shoots ,player has 3 animation idle walk and shoot, i m not able to write proper script.any help?

1 Answer

1

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