i'm making a small top down open world zombie game and testing out if a player is in range of the zombie and if true then the zombie can attack the player.
|
i need the script to not be taxing as much as possible because worst case scenario the server has to render 460 zombies.
|
as far as i understand it (which is not much), raycast is taxing so i started testing other methods, this is the code i'm trying to test, i haven't tested the rest so it won't work as it is but wanted to include it to give an idea of what i'm trying to do
using UnityEngine;
using System.Collections;
public class ZombieAttack : MonoBehaviour
{
public Transform zombieTransform;
public int damage = 1;
public float attackRange = 0.7f;
public GameObject hitEffect;
public LayerMask player;
void Start()
{
StartCoroutine(attack());
}
public void Update()
{
//want the var in update to check constantly if the player is in range or not
var attackingZone = Physics2D.OverlapCircle(zombieTransform.position, attackRange, player);
}
IEnumerator attack()
{
while (attackingZone)
{
//Calling "Health" class on the player
Health playerHealth = attackingZone.transform.GetComponent();
if (playerHealth = null)
{
yield break;
}
else
{
//play animation here
//wait 0.2 seconds
playerHealth.TakeDamage(damage);
}
//this is just a place holder piece of code for now
Instantiate(hitEffect, Vector2.zero, Quaternion.identity);
//2 seconds till the zombie can attack again
yield return new WaitForSeconds(2f);
}
}
}
|
so now my question is "what is the least taxing way/code i can do this with? and if this code is good then how do i make it work?"
↧