Hello, I am struggling to get my objects to do what I want them to do. My ultimate goal is to have two groups, Ally
and Enemy
that are fighting eachother. However, after nearly a week of seeking help, reading, and watching any related thing I can find, I have made very little progress.
Right now I am trying to get Ally
to seek-out the nearest Enemy
. The method that I have had the most success is was telling one of my objects to seek out others with a certain tag
. This worked, but not well. They would rush toward one Enemy
, kill it, and then idle. Other Ally
objects would spawn and then rush to another one, kill it, and go idle themselves.
The code for this is:
using UnityEngine;
using System.Collections;
public class AlliedAttack : MonoBehaviour
{
public float timeBetweenAttacks = 0.5f;
public int AlliedAttackDamage = 10;
RaycastHit shootHit;
PlayerShooting playerShooting;
Animator anim;
GameObject enemy;
PlayerHealth playerHealth;
EnemyHealth enemyHealth;
bool EnemyInRange;
float timer;
void Awake()
{
enemy = GameObject.FindGameObjectWithTag("Enemy");
enemyHealth = enemy.GetComponent<EnemyHealth>();
anim = GetComponent<Animator>();
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject == enemy)
{
EnemyInRange = true;
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject == enemy)
{
EnemyInRange = false;
}
}
void Update()
{
timer += Time.deltaTime;
if (timer >= timeBetweenAttacks && EnemyInRange && enemyHealth.currentHealth > 0)
{
Attack();
}
}
void Attack()
{
timer = 0f;
if (enemyHealth.currentHealth > 0)
{
enemyHealth.TakeDamage(AlliedAttackDamage, shootHit.point);
}
}
}
//
//
//
using UnityEngine;
using System.Collections;
public class AlliedMovment : MonoBehaviour
{
Transform enemy;
EnemyHealth enemyHealth;
UnityEngine.AI.NavMeshAgent nav;
void Update()
{
enemy = GameObject.FindGameObjectWithTag("Enemy").transform;
nav = GetComponent<UnityEngine.AI.NavMeshAgent>();
nav.SetDestination(enemy.position);
}
}
//
//
//
My second attempt was to use Vector3.MoveTowards
. All I have managed to do with this one is to get my objects to run from one side of the room to the other and sit there.
//
//
using UnityEngine;
using System.Collections;
public class AlliedMovement2 : MonoBehaviour
{
public Transform target;
public float speed;
void Update()
{
transform.position = Vector3.MoveTowards(transform.position, target.transform.position * Time.deltaTime);
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
}
//
//
//
Finally, I tried this method:
//
//
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AlliedMovement2 : MonoBehaviour
{
public Transform enemy;
public Transform myTransform;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
transform.LookAt(enemy);
transform.Translate(Vector3.forward * 5 * Time.deltaTime);
}
}
It does the exact same thing as my attempt #2, but instead of crossing the entire room, they rush toward the 0, 0 , 0 point and…act strange.
At no point did I manage to figure out how to make my objects find the nearest enemy, either. They sort of just pick the oldest one to spawn and charge it. Could anyone please suggest what I could try or point out what I’m doing wrong?