hello all
i need some help developing a shot gun script, the only part that im stuck with is ray casting in a wide cone effect. similar to what you would do in for an AI script. only that multiple rays are going to exists within that cone(via ‘for’ loops). ive tried using other vector3’s other than Vecotr3.forward. when i try to implement this, the cast some how hits in one one spot where ever i shoot the gun.
using UnityEngine;
using System.Collections;
public class SPAS12 : MonoBehaviour {
public AudioClip[] FireS;
public AudioClip ReloadS;
public AudioClip EMptyS;
public const int FULLCartridge = 10;
public int CurrentShells;
public int ReserveShells;
public int ShellPellets;
public int CartridgeCount;
public GameObject Child;
public int damage = 10;
public float range = 100.0f;
public float force = 2.0f;
public float reloadTime = 1.0f;
public Renderer MuzzleFlash;
public ParticleEmitter hitParticles;
public Transform Target;
public float spread = 12.0f;
float recoil = 1.0f;
// Use this for initialization
void Start () {
if (hitParticles)
hitParticles.emit = false;
}
// Update is called once per frame
void LateUpdate () {
transform.LookAt(Target.transform);
MuzzleFlash.enabled = false;
if (Input.GetButtonDown("Reload"))
{
Reload();
}
if (Input.GetButtonDown("Fire1") CurrentShells > 0)
{
FireShell();
}
CartridgeCount = (int)(ReserveShells / FULLCartridge);
if (CurrentShells <= 0)
{
// Reload();
StartCoroutine(Reload());
}
}
void FireShell() {
ShellPellets = Random.Range(8, 16);
for (int i = 0; i < ShellPellets; i++)
{
float Angle = Random.Range(-spread, spread);
float height = Vector3.Distance(transform.position, Target.position );
Vector3 direction = transform.TransformDirection(Vector3.forward);
// float RNGX = Random.Range(-spread, spread);
// float RNGY = Random.Range(-spread, spread);
RaycastHit hit;
if (Physics.Raycast(transform.position, direction, out hit, range))
{
if (hit.rigidbody)
hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
// Place the particle system for spawing out of place where we hit the surface!
// And spawn a couple of particles
if (hitParticles)
{
hitParticles.transform.position = hit.point;
hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
hitParticles.Emit();
}
// Send a damage message to the hit object
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
}
CurrentShells--;
Recoil();
// Register that we shot this frame,
// so that the LateUpdate function enabled the muzzleflash renderer for one frame
MuzzleFlash.enabled = true;
audio.clip = FireS[1];
audio.Play();
// Reload gun in reload Time
if (CurrentShells <= 0)
{
StartCoroutine(Reload());
}
}
IEnumerator Reload()
{
// Wait for reload time first - then add more bullets!
yield return new WaitForSeconds(reloadTime);
// We have a clip left reload
if (CartridgeCount > 0 CurrentShells == 0)
{
ReserveShells = ReserveShells - FULLCartridge;
CurrentShells = FULLCartridge;
audio.clip = ReloadS;
audio.Play();
}
}
void Recoil()
{
Recoil Rcoil = Target.GetComponent("Recoil") as Recoil;
Rcoil.RecoilTransForm(recoil);
}
}
thank you in advance, ive got to say making a shot gun has go to be the hardest gun to make. ive read through the ray casting and vector 3 Docs, none help me with this particular script.