My bullets are occasionally flying through objects and I really can’t figure out why. I’m using raycasting as opposed to colliders. Strangely, the speed of the bullet doesn’t seem to stop this from happening, can anyone see anything wrong with my code?
using UnityEngine;
using System.Collections;
public class BulletBase : MonoBehaviour {
public const string BulletHit = "bullethit";
public const string BulletExplode = "bulletExplode";
public float speed = 10.0F;
public float lifeTime = 2.0F;
protected Transform _tr;
protected float _spawnTime = 0.0F;
private void Awake() {
_tr = transform;
}
public virtual void Start() {
_tr = transform;
_spawnTime = Time.time;
}
private void FixedUpdate() {
// Check for collisions
RaycastHit hit;
if (Physics.Raycast(_tr.position, _tr.forward, out hit, 1.0F)){
if(hit.transform){
Messenger<RaycastHit>.Broadcast(BulletBase.BulletHit, hit);
DestroyBullet();
return;
}
}
}
private void Update() {
_tr.position += _tr.forward * speed * Time.deltaTime;
if (Time.time > _spawnTime + lifeTime){
DestroyBullet();
}
}
public void DestroyBullet(){
ObjectPoolManager.DestroyPooled( gameObject );
gameObject.SetActiveRecursively(false);
}
}