trying to turn my gun script into a cross bow script.
ATM the xbow is fired and the dmg is applyed before my arrow gets to the target
any help is always appeciated
using UnityEngine;
using System.Collections;
public class Bow : MonoBehaviour {
public Transform projectile;
public int range = 100;
public float fireRate = 1f;
public float force = 10f;
public float damage = 5f;
public int bulletsPerClip = 40;
public int clips = 20;
public float reloadTime = .5f;
public ParticleEmitter hitParticles;
public Renderer muzzleFlash;
public LayerMask removeWaypoints; //stop from hitting waypoint
private int bulletsLeft = 0;
private float nextFireTime = 0f;
private float m_LastFramesShot = -1f;
// Use this for initialization
void Start () {
hitParticles = GetComponentInChildren<ParticleEmitter>();
//particles emit only when hit something
if(hitParticles)
{
hitParticles.emit = false;
}
bulletsLeft = bulletsPerClip;
}
// Update is called once per frame
void LateUpdate ()
{
if(muzzleFlash)
{
//we shoot this frame enable muzzleFlash
if(m_LastFramesShot == Time.frameCount)
{
muzzleFlash.transform.localRotation =
Quaternion.AngleAxis(Random.Range(0, 359), Vector3.forward);
muzzleFlash.enabled = true;
if(audio)
{
if(!audio.isPlaying)
audio.Play();
audio.loop = true;
}
}
else //we didn't diable muzzleFlash
{
muzzleFlash.enabled = false;
enabled = false;
//play sound
if(audio)
{
audio.loop = false;
}
}
}
}
public void Fire()
{
if(bulletsLeft == 0)
return;
if(Time.time - fireRate > nextFireTime)
nextFireTime = Time.time - Time.deltaTime;
while(nextFireTime < Time.time bulletsLeft != 0)
{
FireOneShot();
nextFireTime += fireRate;
}
}
public void FireOneShot()
{
Vector3 direction = transform.TransformDirection(Vector3.forward);
RaycastHit hit;
Transform instantiatedProjectile = Instantiate(projectile,
transform.position,
transform.rotation) as Transform;
//did we hit anything
if(Physics.Raycast(transform.position, direction, out hit, range, removeWaypoints))
{
projectile.transform.position = hit.point;
//apply force to rigidbody we hit
if(hit.rigidbody)
hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
//spawn particles out of place we hit
if(hitParticles)
{
hitParticles.transform.position = hit.point;
hitParticles.transform.rotation =
Quaternion.FromToRotation(Vector3.up, hit.normal);
hitParticles.Emit();
}
//send damage message to object we hit
hit.collider.SendMessageUpwards("ApplyDamage", damage,
SendMessageOptions.DontRequireReceiver);
}
bulletsLeft--;
m_LastFramesShot = Time.frameCount;
enabled = true;
if(bulletsLeft == 0)
StartCoroutine("Reload");
}
public IEnumerator Reload()
{
//wait for reload time first - then add more bullets
yield return new WaitForSeconds(reloadTime);
//We have a clip left reload
if(clips > 0)
{
clips--;
bulletsLeft = bulletsPerClip;
}
}
public int GetBulletsLeft()
{
return bulletsLeft;
}
}
using UnityEngine;
using System.Collections;
public class SimpleBullet : MonoBehaviour {
public float speed = 10f;
public float lifeTime = 3f;
public float dist = 10000f;
private float spawnTime = 0.0f;
private Transform tr;
public int range = 100;
// Use this for initialization
void Start () {
tr = transform;
spawnTime = Time.time;
}
// Update is called once per frame
void Update () {
tr.position += tr.forward * speed * Time.deltaTime;
dist -= speed * Time.deltaTime;
if (Time.time > spawnTime + lifeTime || dist < 0) {
Destroy(gameObject);
}
}
}