• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by cobispan · Mar 18 at 09:59 AM · shooting

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 );
 
 }
 
 
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

134 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Shooting Cooldown Bar 2 Answers

Bullet instance giving useless error 0 Answers

Problem with shooting an object in 2D 1 Answer

Unity 2D On Player Flip change shooting direction? 1 Answer

Does RayCast crosses mesh colliders? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges