- Home /
How do I get my ship to shoot using firedelegate?
Hey! I have been working on this game similar to Space Shoot em up or Space Schmup and I have been going by the book for help to do the coding. I am almost done with the game but there is one issue I have that I can not resolve. My ship just won't shoot! I have coded correctly but don't have any error messages. Been stuck on this a few days now so some help would be great! Here is my weapon script and my "Hero (Ship)" script.
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Hero : MonoBehaviour { static public Hero S; //Singleton //These fields control the movement of the ship public float speed = 30; public float rollMult = -45; public float pitchMult = 30; [SerializeField] //Ship status information private float _shieldLevel = 1; //Added underscore //Weapon fields public Weapon[] weapons; public bool ____________________; public Bounds bounds; // Declare a new delegate type WeaponFireDelegate public delegate void WeaponFireDelegate(); // Create a WeaponFireDelegate field named fireDelegate public WeaponFireDelegate fireDelegate; public float gameRestartDelay = 2f; void Awake() { S = this; // Set the Singleton bounds = Utils.CombineBoundsofChildren(this.gameObject); } void Start(){ //Reset the weapons to start _Hero with 1 blaster ClearWeapons(); weapons[0].SetType(WeaponType.blaster); } void Update() { //Pull in information from the Input class float xAxis = Input.GetAxis("Horizontal"); //1 float yAxis = Input.GetAxis("Vertical"); //1 //Change transform.position based on the axes Vector3 pos = transform.position; pos.x += xAxis * speed * Time.deltaTime; pos.y += yAxis * speed * Time.deltaTime; transform.position = pos; bounds.center = transform.position; //Keep the ship constrained to the screen bounds Vector3 off = Utils.ScreenBoundsCheck(bounds, BoundsTest.onScreen); //2 if (off != Vector3.zero) { pos -= off; transform.position = pos; } // Rotate the ship to make it feel more dynamic //2 transform.rotation = Quaternion.Euler(yAxis * pitchMult, xAxis * rollMult, 0); // use the fireDelegate to fire Weapons // First, make sure the Axis("Jump") button is pressed //Then ensure that the fireDelegate isn't null to avoid an error if (Input.GetAxis("Jump") == 1 && fireDelegate != null) { //1 fireDelegate(); } } //This variable holds a reference to the last triggering GameObject public GameObject lastTriggerGo = null; //1 void OnTriggerEnter(Collider other) { //Find the tag of other.gameObject or its parent GameObjects GameObject go = Utils.FindTaggedParent(other.gameObject); //If there is a parent with a tag if (go != null) { //Make sure it's not the same triggering go As last time if (go == lastTriggerGo) //2 { return; } lastTriggerGo = go; //3 if (go.tag == "Enemy") { //If the shield was triggered by an enemy //Decrease the level of the shield by 1 shieldLevel--; //Destroy the enemy Destroy(go); //4 } else if (go.tag == "PowerUp") { // If the shield was triggered by a PowerUp AbsorbPowerUp(go); } else { print("Triggered: " + go.name); } //Line moved here } else { //Otherwise announce the original other.gameObject print("Triggered: " + other.gameObject.name); //Move this line here! } } public float shieldLevel { get { return (_shieldLevel); //1 } set { _shieldLevel = Mathf.Min(value, 4); //2 //If the shield is going to be set to less than zero if (value < 0) { //3 Destroy(this.gameObject); //Tell Main.s to restart the game after a delay Main.S.DelayedRestart(gameRestartDelay); } }
using System.Collections; using System.Collections.Generic; using UnityEngine;
//This is an enum of the various possible weapon types
//It also includes a "shield" type to allow a shield power-up
//Items marked [NI] below are Not Implemented in this book
public enum WeaponType
{
none, //The default / no weapon
blaster, //A simple blaster
spread, //Two shots simultaneously
phaser, //Shots that move in waves [NI]
missile, //Homing missles [NI]
laser, //Damage over time [NI]
shield, //Raise shield level
}
//The WeaponDefinition allows you to set the properties
// of a specific weapon in the Inspector. Main has an erray
// of WeaponDefinitions that makes this possible.
// [System.Serializable] tells Unity to try to view WeaponsDefinition
// in the Inspector pane. It doesn't work for everything, but it
// will work for simple classes like this!
[System.Serializable]
public class WeaponDefinition
{
public WeaponType type = WeaponType.none;
public string letter; //The letter to show on the power-up
public Color color = Color.white; //Color of Collar & power-up
public GameObject projectilePrefab; //Prefab for projectiles
public Color projectileColor = Color.white;
public float damageOnHit = 0; //Damage per second (Laser)
public float delayBetweenShots = 0;
public float velocity = 20; //Speed of projectiles
}
//Note: Weapon prefabs, colors, and so on. are set in the class Main.
public class Weapon : MonoBehaviour
{
static public Transform PROJECTILE_ANCHOR;
public bool ____________________;
[SerializeField]
private WeaponType _type = WeaponType.none;
public WeaponDefinition def;
public GameObject collar;
public float lastShot; //Time last shot was fired
void Awake()
{
collar = transform.Find("Collar").gameObject;
}
void Start()
{
// Call SetType() properly for the default _type SetType(_type);
SetType(_type);
if (PROJECTILE_ANCHOR == null)
{
GameObject go = new GameObject("_Projectile_Anchor");
PROJECTILE_ANCHOR = go.transform;
}
//Find the fireDelegate of the parent
GameObject parentGO = transform.parent.gameObject;
if (CompareTag("Hero"))
{
Hero.S.fireDelegate += Fire;
}
}
public WeaponType type
{
get { return ( _type ); }
set { SetType(value); }
}
public void SetType( WeaponType wt)
{
_type = wt;
if(type == WeaponType.none)
{
this.gameObject.SetActive(false);
return;
}else
{
this.gameObject.SetActive(true);
}
def = Main.GetWeaponDefinition(_type);
collar.GetComponent<Renderer>().material.color = def.color;
lastShot = 0; // YOu can always fire immediately after _type is set
}
public void Fire()
{
//If this.gameObject is inactive, return
if (!gameObject.activeInHierarchy)
{ return; }
//If it hasn't been enough time between shots, return
if (Time.time - lastShot < def.delayBetweenShots)
{
return;
}
Projectile p;
switch (type)
{
case WeaponType.blaster:
p = MakeProjectile();
p.GetComponent<Rigidbody>().velocity = Vector3.up * def.velocity;
break;
case WeaponType.spread:
p = MakeProjectile();
p.GetComponent<Rigidbody>().velocity = new Vector3( -2f, 0.9f, 0 ) * def.velocity;
p.GetComponent<Rigidbody>().velocity = new Vector3(.2f, 0.9f, 0) * def.velocity;
break;
}
}
public Projectile MakeProjectile()
{
GameObject go = Instantiate(def.projectilePrefab);
if (transform.parent.gameObject.CompareTag("Hero"))
{
go.tag = "ProjectileHero";
go.layer = LayerMask.NameToLayer("ProjectileHero");
}
else
{
go.tag = "ProjectileEnemy";
go.layer = LayerMask.NameToLayer("ProjectileEnemy");
}
go.transform.position = collar.transform.position;
go.transform.parent = PROJECTILE_ANCHOR;
Projectile p = go.GetComponent<Projectile>();
p.type = type;
lastShot = Time.time;
return( p );
}
}
Your answer

Follow this Question
Related Questions
How to delay time reset on collision? 2 Answers
Need explanation for this code 0 Answers
GUI Joystick 1 Answer
How to make a gun in unity? 1 Answer
Shooting a projectile the longer a button is held c# 1 Answer