I try to make a FPS but I have the problem that Unity gets an StackOverflowExeption. I ask myself if it is because of the get{} set{} and if so what is my mistake? Or is there a better way if I want to have more than just one gun?
Here is my GunScript that is attached to the player and holds the shooting mechanic:
using UnityEngine;
using System.Collections;
public class GunScript : MonoBehaviour {
private float fireRate;
public float FireRate
{
get{return FireRate;}
set{FireRate = value;}
}
private float damage;
public float Damage
{
get{return Damage;}
set{Damage = value;}
}
private float reloadeTime;
public float ReloadTime
{
get{return ReloadTime;}
set{ReloadTime = value;}
}
private float nextFire;
private int clipSize;
public int ClipSize
{
get{return ClipSize;}
set{ClipSize = value;}
}
public int ammo;
private int ammoLeft;
private int firemode;
private int clipsLeft;
public int ClipsLeft
{
get{return ClipsLeft;}
set{ClipsLeft = value;}
}
private int roundsPerBurst;
public int RoundsPerBurst
{
get{return RoundsPerBurst;}
set{RoundsPerBurst = value;}
}
public bool wait;
public bool isBursting;
void Start ()
{
ammo = clipSize;
}
void Awake ()
{
fireRate = FireRate;
damage = Damage;
clipSize = ClipSize;
reloadeTime = ReloadTime;
roundsPerBurst = RoundsPerBurst;
clipsLeft = ClipsLeft;
}
void Update ()
{
if (Input.GetKey (KeyCode.V))
{
firemode ++;
if(firemode > 3)
firemode = 0;
}
if (firemode == 0)
{
if (Input.GetButton ("Fire1") && Time.time > nextFire && wait == false)
{
if (ammo > 0)
{
nextFire = Time.time + fireRate;
RaycastShot ();
audio.Play ();
ammo--;
}
}
}
if (firemode == 1)
{
if (Input.GetButtonDown ("Fire1") && Time.time > nextFire && wait == false)
{
if (ammo > 0)
{
nextFire = Time.time + fireRate;
RaycastShot ();
audio.Play ();
ammo--;
}
}
}
if (firemode == 2)
{
if (Input.GetButtonDown ("Fire1") && Time.time > nextFire && wait == false && isBursting == false) {
if (ammo > 0)
{
nextFire = Time.time + fireRate;
StartCoroutine(Burst_Fire ());
audio.Play ();
ammo--;
}
}
}
if(Input.GetKeyDown(KeyCode.R) && clipsLeft > 0)
{
StartCoroutine(Reload ());
}
}
public IEnumerator Reload ()
{
if (clipsLeft > 0)
{
if (ammo > 0)
{
clipsLeft--;
wait = true;
//audio.Play();
//animation.play();
yield return new WaitForSeconds (ReloadTime);
ammo = clipSize + 1;
wait = false;
}
else
{
clipsLeft--;
wait = true;
//audio.Play();
//animation.Play();
yield return new WaitForSeconds (ReloadTime);
ammo = clipSize;
wait = false;
}
}
}
IEnumerator Burst_Fire()
{
int shotCounter = 0;
// Keep firing until we used up the fire time
while (nextFire < Time.time)
{
while (shotCounter < roundsPerBurst)
{
isBursting = true;
RaycastShot ();
shotCounter++;
yield return new WaitForSeconds(0.1f);
}
nextFire += fireRate;
}
isBursting = false;
}
private void RaycastShot ()
{
Ray ray = Camera.main.ViewportPointToRay(new Vector3(Screen.width*0.5f, Screen.height*0.5f, 0f));
RaycastHit hit;
if (Physics.Raycast (ray, out hit, 300))
{
if(hit.collider.tag == "EnemyHead")
{
Vector3 hitPoint = hit.point;
float Damage = damage * 5;
hit.transform.SendMessage ("ApplyDammage", Damage, SendMessageOptions.DontRequireReceiver);
Debug.DrawLine (hit.point, hit.point + hit.normal, Color.cyan);
Debug.Log("Hit");
}
if(hit.collider.tag == "EnemyBody")
{
Vector3 hitPoint = hit.point;
float Damage = damage ;
hit.transform.SendMessage ("ApplyDammage", Damage, SendMessageOptions.DontRequireReceiver);
Debug.DrawLine (hit.point, hit.point + hit.normal, Color.cyan);
Debug.Log("Hit");
}
else
Debug.Log("Miss");
}
}
}
And here is the script that holds the values.It is attached to the particular gun:
using UnityEngine;
using System.Collections;
public class AssaultRifle : MonoBehaviour {
private float damage = 28f;
private float fireRate = 0.1f;
private int clipSize = 30;
private int roundsPerBurst = 3;
private int firemode;
GunScript ActualGun = new GunScript ();
void Awake ()
{
ActualGun.ClipSize = clipSize;
ActualGun.Damage = damage;
ActualGun.FireRate = fireRate;
ActualGun.RoundsPerBurst = roundsPerBurst;
}
}
Thank you for your answer :D
↧