My bullets aren’t detecting collision consistently, infact its every other bullet registers as a collision.
I have a rigid body on the bullet and have tried applying a rigidbody to the object but it gives the same results with or without.
The code below shows the collisions working, I have tried a linecast and a spherecast but both don’t seem it improve anything. However I know the issue is due to the bullet travelling too fast but what bullet travels slow? ![]()
Is there any workarounds for this? I’m having no luck browing the forums
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletControl : MonoBehaviour {
[Range(0, 50)]
public float bulletSpeed;
public float deactivateTimer;
float tempDeactivateTimer;
bool bulletHasHit = false; // bullet has hit something
void Start()
{
tempDeactivateTimer = deactivateTimer;
}
Vector3 prev = Vector3.zero;
// Update is called once per frame
void FixedUpdate()
{
if (!bulletHasHit)
{
BulletHitDetection();
}
// transform.Translate(transform.forward * bulletSpeed);
}
void LookForCollision()
{
Collider[] hitDetection = Physics.OverlapSphere(gameObject.transform.position, .5f, 1 << 9, QueryTriggerInteraction.Collide);
if (hitDetection.Length > 0)
{
float closestObjectDist = 10000f;
GameObject closestObj = hitDetection[0].gameObject;
for (int i = 0; i < hitDetection.Length; i++)
{
if (Vector3.Distance(gameObject.transform.position, hitDetection[i].transform.position) < closestObjectDist)
{
Debug.Log(hitDetection[i].name);
closestObjectDist = Vector3.Distance(gameObject.transform.position, hitDetection[i].gameObject.transform.position);
closestObj = hitDetection[i].gameObject;
}
}
BulletHitObject(closestObj);
}
}
public void BulletHitDetection()
{
RaycastHit hit;
if (!bulletHasHit)
{
LookForCollision();
if (prev != Vector3.zero)
{
if (Physics.Linecast(prev, transform.position, out hit, 1 << 9, QueryTriggerInteraction.Collide))
{
BulletHitObject(hit.collider.gameObject);
}
}
else
{
prev = transform.position;
}
}
}
public void BulletHitObject(GameObject hitObject)
{
bulletHasHit = true;
// Debug.Log(bulletHasHit);
if (hitObject.tag == "Wall")
{
Debug.Log("Wall");
}
this.gameObject.SetActive(false);
}
void OnDrawGizmos()
{
Gizmos.color = Color.yellow;
Gizmos.DrawSphere(transform.position, .5f);
}
}