- Home /
Question by
exp626stitch · Jan 11, 2021 at 07:35 PM ·
c#ainavmesh
How to get my navmeshagents to do certain things?
I am using the navmesh, and would like it if the drone would circle around the target. I would also like it to get health when the health script: myHealth, hp gets to below 30, using the getHealth function. I would also like it to defend the base when its team has less than 10 members, using the Defend function. But it needs to also be able to attack when its defending but not get health if it is defending. How can I do those things? Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class WarDroneAI : MonoBehaviour
{
public NavMeshAgent agent;
public GameObject[] allEnemyDrones;
public Transform currentTarget;
public float timeBetweenAttacks;
public float myPoints;
bool alreadyAttacked;
public GameObject projectile;
public GameObject spawn;
public GameObject muzzleFlash;
GameObject curMuzzleFlash;
public AudioSource blast;
public AudioClip blastSound;
public Vector3 currentDistance;
public Vector3 greatestDistance;
public Transform MyTeamHealth;
public EnemyHealth myHealth;
public GameObject[] myTeam;
public int teamDestroyed;
private bool hasSpot;
public GameObject objectToDefend;
public int curSpotInd;
public Vector3 curSpot;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
AttackEnemyTeam();
}
public void FreezeDrone()
{
agent.SetDestination(transform.position);
}
private void AttackEnemyTeam()
{
if (!hasSpot)
{
ChooseTarget();
Vector3 curDistToTarget = transform.position - currentTarget.position;
if (curDistToTarget.magnitude < 5)
{
FreezeDrone();
}
else
{
agent.SetDestination(currentTarget.position);
}
}
transform.LookAt(currentTarget.position);
if (!alreadyAttacked)
{
Instantiate(projectile, spawn.transform.position, spawn.transform.rotation);
alreadyAttacked = true;
if(currentTarget == null)
{
myPoints += 10;
}
Invoke(nameof(ResetAttack), timeBetweenAttacks);
}
}
private void ResetAttack()
{
alreadyAttacked = false;
}
private void ChooseTarget()
{
if(currentTarget == null)
{
int randomTargetInt = Random.Range(0, allEnemyDrones.Length);
currentTarget = allEnemyDrones[randomTargetInt].transform;
}
}
public void GetHealth()
{
agent.SetDestination(MyTeamHealth.position);
}
public void Defend()
{
if (!hasSpot)
{
for (int i = 0; i < objectToDefend.GetComponent<Defendable>().availableSpots.Length; i++)
{
if (objectToDefend.GetComponent<Defendable>().availableSpots[i] == null)
{
objectToDefend.GetComponent<Defendable>().availableSpots[i] = gameObject;
curSpotInd = i;
curSpot = objectToDefend.GetComponent<Defendable>().positions[i].position;
hasSpot = true;
return;
}
if (hasSpot)
{
return;
}
}
}
if (hasSpot)
{
curSpot = objectToDefend.GetComponent<Defendable>().positions[curSpotInd].position;
agent.SetDestination(curSpot);
}
if (curSpot == null)
{
hasSpot = false;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Need help detecting barriers for my game 0 Answers
How to get a velocity unit vector from a NavMeshAgent? 1 Answer
Make a flying enemy ? 0 Answers
Navmesh agent forward movement 1 Answer