using UnityEngine;
using System.Collections;
public class GunScript : MonoBehaviour {
public float FireSpeed = 15; // how fast the gun fires
[HideInInspector]
public float waitTillNextFire = 0;
public GameObject GunShotSound;
public GameObject MuzzelFlash;
public Transform MuzzelFlashSpawn;
public GameObject Bullet; // the bullet that will be fired
public Transform BulletSpawn; // the object where the bullet will be spawned
public Rigidbody BulletShell;
public Transform BulletShellSpawn;
public float ShellForceRight = 10;
public float ShellForceUp = 3;
public string BoltAnimationString;
float shootAngleRandomizationMax = 0.5f; // the max recoil
float shootAngleRandomizationMin = -0.5f; // the min recoil
public Animation BoltAnimation;
public float BulletSpreadX = 6;
public float BulletSpreadY = 6;
[HideInInspector]
public float targetXRotation;
[HideInInspector]
public float targetYRotation;
Vector3 originalShootPoint;
void Start () {
originalShootPoint = BulletSpawn.localPosition; //sets the position of the bullet spawn
}
void Update ()
{
Rigidbody CurrentShell;
SixenseInput.Controller hydraController = SixenseInput.GetController(SixenseHands.RIGHT);
if (hydraController.GetButton(SixenseButtons.BUMPER))
{
if (waitTillNextFire <= 0)
{
if (Bullet)
{
Instantiate(Bullet, BulletSpawn.position, BulletSpawn.rotation);
if (GunShotSound)
Instantiate(GunShotSound, BulletSpawn.position, BulletSpawn.rotation);
if (MuzzelFlash)
Instantiate(MuzzelFlash, MuzzelFlashSpawn.position, MuzzelFlashSpawn.rotation);
if (BulletShell)
{
CurrentShell = Instantiate(BulletShell, BulletShellSpawn.position, BulletShellSpawn.rotation) as Rigidbody;
CurrentShell.AddForce(transform.right * ShellForceRight * 10);
CurrentShell.AddForce(transform.up * ShellForceUp * 10);
}
BoltAnimation.Play(BoltAnimationString);
targetXRotation = originalShootPoint.x;
targetYRotation = originalShootPoint.z;
targetXRotation += (Random.value - 0.5f) * Mathf.Lerp(shootAngleRandomizationMin, shootAngleRandomizationMax, 1); //randomises the recoil
targetYRotation += (Random.value - 0.5f) * Mathf.Lerp(shootAngleRandomizationMin, shootAngleRandomizationMax, 1);
}
waitTillNextFire = 1;
}
}
waitTillNextFire -= Time.deltaTime * FireSpeed;
BulletSpawn.localRotation = Quaternion.Euler(targetXRotation * BulletSpreadX, targetYRotation * BulletSpreadY, 0);
}
}