Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
1
Question by Wilave · Nov 04, 2015 at 10:33 PM · unity 5shootingspace shooterpick up objectswitch objects

Won't fire when switching weapons?

So I am working on a game based off of the Space Shooter Tutorial game. Just implemented weapons floating in space that can be picked up and switched between using the 1, 2, and 3 keys. Currently trying to get it so I can only fire a weapon when the particular weapon has already been picked up and selected. I'm starting off with the first weapon, figured it was easier than trying to figure out all 3 at once. Here is my most recent player controller script.

 using UnityEngine;
 
 using System.Collections;
 using System.Collections.Generic;
 [System.Serializable]
 public class Boundary
 {
     public float xMin, xMax, zMin, zMax;
 }
 public class PlayerController : MonoBehaviour {
 
     public float speed;
     public Boundary boundary;
     public float tilt;
 
 
     public float secPerShot;
     private float nextFire;
     private int wepSel;
 
     public WeaponBase wepA;
     public WeaponBase wepB;
     public WeaponBase wepC;
 
     public GameObject shot;
     public Transform shotSpawn;
     private List<WeaponBase> weps;
 
     public bool wepUp1;
     public bool wepUp2;
     public bool wepUp3;
 
     void Awake ()
     {
     weps = new List<WeaponBase> ();
     
     weps.Add (wepA);
     weps.Add (wepB);
     weps.Add (wepC);
     
     foreach(WeaponBase w in weps)
         w.gameObject.SetActive(false);
     
         wepUp1 = false;
     }     
     
     void Update ()
     {    
         nextFire += Time.deltaTime;
 
         if (Input.GetKeyDown ("1") && wepUp1) {
             weps [0].gameObject.SetActive (true);
             weps [1].gameObject.SetActive (false);
             weps [2].gameObject.SetActive (false); 
                     FireSelect();           
         }
         if (Input.GetKeyDown ("2") && wepUp2) {
             weps [0].gameObject.SetActive (false);
             weps [1].gameObject.SetActive (true);
             weps [2].gameObject.SetActive (false);
         }
         if (Input.GetKeyDown ("3") && wepUp3) {
             weps [0].gameObject.SetActive (false);
             weps [1].gameObject.SetActive (false);
             weps [2].gameObject.SetActive (true);
         }
     }   
 
     void FixedUpdate () {
 
         float hor = Input.GetAxis ("Horizontal");
         float ver = Input.GetAxis ("Vertical");
         Vector3 vel = new Vector3 (hor, 0, ver);
         GetComponent<Rigidbody>().velocity = vel*speed;
 
         GetComponent<Rigidbody>().position = new Vector3 (
             Mathf.Clamp (GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
             0.0f,
             Mathf.Clamp (GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
         );
         GetComponent<Rigidbody>().rotation = Quaternion.Euler (0, 0, GetComponent<Rigidbody>().velocity.x * -tilt);
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.CompareTag("Weapon1"))
         {
             other.gameObject.SetActive(false);
             wepUp1 = true;
         }
         if (other.gameObject.CompareTag("Weapon2"))
         {
             other.gameObject.SetActive(false);
             wepUp2 = true;
         }
         if (other.gameObject.CompareTag("Weapon3"))
         {
             other.gameObject.SetActive(false);
             wepUp3 = true;
         } 
     }
 
     void FireSelect()
     {
         if (weps[0] == true && Input.GetButtonDown ("Jump"))
         {
             wepA.shoot();
         }
       }
 }
         
 

The shoot method being called in the FireSelect method is in a different weapon script that instantiates the shots. Let me know if there's anything else you think could help make this easier to figure out. I feel like im thinking the wrong way about something here. I appreciate any help than you all can give me!

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

38 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

Related Questions

Unity Multiplayer - Projectile hits owner 0 Answers

Bullet shooting in the wrong angle. 0 Answers

My script does not work each time i enter Unity unless i add it to the object again. any solutions? 0 Answers

Easy question: X axis overriding Y. Please help! :D 0 Answers

My laser bolts are not working in space shooter. If someone could help, that would be great! 1 Answer


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