- Home /
Question by
conguerror · Apr 05, 2021 at 11:41 AM ·
c#collisionoverlapsphereprojectileswall collision
Collision detection with overlap sphere
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);
}
}
Comment
Best Answer
Answer by conguerror · Apr 05, 2021 at 04:35 PM
Solved: transform.translate() literally teleports the bullets. So you need to replace it.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Why are my projectiles facing the wrong way? 1 Answer
Multiple Cars not working 1 Answer
Error CS1501, Projectiles spawning. 1 Answer