I want to create overlap sphere bullets due to it’s lower cost in performance(I don’t want rigidbodies and colliders). It’s done for mobile.
The problem is that most of the time projectile detects the collisions. But sometimes it’s not. It’s very evident with automatic weapons(with higher rounds per minute). Projectile just goes through the collider and not always detects the collision.
Code:
protected virtual void Update()
{
if (!PauseMenu.paused&&gameObject.activeSelf)
{
if (!collided)
{
if (direction != Vector3.zero && direction != null)
{
if (nonPhysics)
{
NonPhysicsDetection();
}
transform.Translate(direction * speed * Time.deltaTime);
}
}
}
}
protected virtual void NonPhysicsDetection()
{
if (collided) { return; }
Collider[] colliders = Physics.OverlapSphere(transform.position, sphereRadius, detectionLayer);
if (colliders.Length > 0)
{
trail.enabled = false;
foreach (Collider collider in colliders)
{
Debug.Log($"{collider.name}");
if (collider.TryGetComponent<Health>(out Health health))
{
health.TakeDamage(dmg);
break;
}
collided = true;
}
gameObject.SetActive(false);
}
}