When I do a build, it make the enemies in my game take extremely long to kill when they don’t in the editor, I don’t know why as I have made sure everything I could find works in the editor when the build just kind of ruins it. I will post the damage code and the enemy code to just show it incase there is an issue there. Thank you if you can help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Entity : MonoBehaviour
{
[SerializeField] private float StartingHealth;
private float health;
public float Health
{
get
{
return health;
}
set
{
health = value;
Debug.Log(health);
if(health <= 0f)
{
Destroy(gameObject);
}
}
}
void Start()
{
Health = StartingHealth;
}
void Update()
{
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DamageGun : MonoBehaviour
{
public float Damage;
public float bulletRange;
private Transform PlayerCamera;
public Camera cam;
public bool useShotgun = false;
public int pellets = 10;
public float spreadAngle = 20f;
void Start()
{
PlayerCamera = Camera.main.transform;
}
public void Shoot()
{
if (useShotgun)
{
ShootShotgun();
}
else
{
ShootSingleBullet();
}
}
private void ShootSingleBullet()
{
Ray ray = cam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
RaycastHit hit;
Vector3 targetPoint;
if (Physics.Raycast(ray, out hit))
targetPoint = hit.point;
else
targetPoint = ray.GetPoint(75);
Ray gunRay = new Ray(PlayerCamera.position, PlayerCamera.forward);
if (Physics.Raycast(gunRay, out RaycastHit hitInfo, bulletRange))
{
if (hitInfo.collider.gameObject.TryGetComponent(out Entity enemy))
{
enemy.Health -= Damage;
}
}
}
private void ShootShotgun()
{
for (int i = 0; i < pellets; i++)
{
float spreadX = Random.Range(-spreadAngle / 2, spreadAngle / 2);
float spreadY = Random.Range(-spreadAngle / 2, spreadAngle / 2);
Vector3 direction = Quaternion.Euler(spreadY, spreadX, 0) * PlayerCamera.forward;
if (Physics.Raycast(PlayerCamera.position, direction, out RaycastHit hit, bulletRange))
{
if (hit.collider.gameObject.TryGetComponent(out Entity enemy))
{
enemy.Health -= Damage;
}
}
}
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, bulletRange);
}
}