Hello,
I seem to be in a bit of a pickle, When for example my spaceship shoots at a wall or enemy, The Bullet sometimes dose not register a collision and goes straight through the target, The bullet is supposed to deal damage to its target based on velocity of the bullet, So you can see the problem i am having. I will show you both the Weapon script and Bullet script to see if you can see what i can not, Thanks in advance.
projectileScript goes on the Bullet
using UnityEngine;
using System.Collections;
public class projectileScript : MonoBehaviour
{
public Vector3 muzzleVelocity;
public float TTL;
public bool isBallistic;
public float Drag; // in metres/s lost per second.
// Use this for initialization
void Start ()
{
if (TTL == 0)
TTL = 5;
print(TTL);
Invoke("projectileTimeout", TTL);
}
// Update is called once per frame
void Update ()
{
if (Drag != 0)
muzzleVelocity += muzzleVelocity * (-Drag * Time.deltaTime);
if (isBallistic)
muzzleVelocity += Physics.gravity * Time.deltaTime;
if (muzzleVelocity == Vector3.zero)
return;
else
transform.position += muzzleVelocity * Time.deltaTime;
transform.LookAt(transform.position + muzzleVelocity.normalized);
//Debug.DrawLine(transform.position, transform.position + muzzleVelocity.normalized, Color.red);
}
void projectileTimeout()
{
DestroyObject(gameObject);
}
public void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Static")
Debug.Log("i hit the wall");
//DestroyObject(gameObject);
}
}
weaponScript goes on the Weapon
using UnityEngine;
using System.Collections;
public class weaponScript : MonoBehaviour
{
// public
public float projMuzzleVelocity; // in metres per second
public GameObject projPrefab;
public float RateOfFire;
public float Inaccuracy;
public AudioClip ShootSoundFX;
// private
private float fireTimer;
// Use this for initialization
void Start ()
{
fireTimer = Time.time + RateOfFire;
}
// Update is called once per frame
void Update ()
{
Debug.DrawLine(transform.position, transform.position + transform.forward, Color.red);
if (Time.time > fireTimer && Input.GetMouseButton(0))
{
GameObject projectile;
Vector3 muzzlevelocity = transform.forward;
if (Inaccuracy != 0)
{
Vector2 rand = Random.insideUnitCircle;
muzzlevelocity += new Vector3(rand.x, rand.y, 0) * Inaccuracy;
}
muzzlevelocity = muzzlevelocity.normalized * projMuzzleVelocity;
projectile = Instantiate(projPrefab, transform.position, transform.rotation) as GameObject;
projectile.GetComponent<projectileScript>().muzzleVelocity = muzzlevelocity;
fireTimer = Time.time + RateOfFire;
audio.PlayOneShot(ShootSoundFX);
}
else
return;
}
}