Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by GreenGuy92 · Oct 27, 2013 at 01:26 PM · gameobjectshootingbulletshooter

All Three Game Objects Fire At Once

Hey I have three game objects/Bullet Factories that fire all at the same time when i press spacebar instead of them fire individually when selected any ideas ? here is my script

 #pragma strict
 var bullet : Rigidbody;
 
 
 function Start () 
 {
 
 }
 
 function Update () 
 {
 if(Input.GetKeyDown("space"))
 {
 //everything inside brackets runs
 
     var latestBullet = Instantiate(bullet, transform.position, transform.rotation); 
     latestBullet.AddRelativeForce (0, 0, 500);
 }
 
 
     
 }
Comment
Add comment · Show 3
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
avatar image tw1st3d · Oct 27, 2013 at 01:30 PM 1
Share

What are you expecting to happen? There's no code inside of this that says "only fire one" or "only fire one based on how many times I've already fired". Do you want one to fire at a time, or do you want them to fire on the same button click, but one after the other?

avatar image GreenGuy92 · Oct 27, 2013 at 01:39 PM 0
Share

Well I have three game objects attached to three cameras and the idea is when i press space bar the game object fires pending on which ever camera it is attached to. It doesnt have a bullet count or anything but i would want it to only fire once for every time space bar is pressed down

avatar image tw1st3d · Oct 27, 2013 at 01:39 PM 0
Share

Converted your answer to comment

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Oct 27, 2013 at 02:38 PM

You must enable the shooting code only when the object is selected. Supposing that this script has a public boolean variable selected that is true when the object is selected, the code can be like this:

 #pragma strict
 
 public var bullet : Rigidbody;
 public var selected : boolean = false; // set this to true when object selected
  
 function Update (){
   if (selected && Input.GetKeyDown("space")){
     var latestBullet = Instantiate(bullet, transform.position, transform.rotation); 
     latestBullet.AddRelativeForce (0, 0, 500);
   }
 }
Comment
Add comment · Show 2 · Share
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
avatar image GreenGuy92 · Oct 27, 2013 at 03:17 PM 0
Share

Just tried that script and it does work. Im bad a getting my point across sorry I have a feature on another script where if a right click is pressed then the player can switch camera but can only see through one camera at a time. So I was meaning if the player switches to camera 2 with another first person controller attached to it can it automatically turn camera 1 and camera 3s shooter off unless a right click is made which will switch to camera 3 ?

avatar image aldonaletto · Oct 28, 2013 at 06:46 PM 0
Share

It depends on how you're selecting the cameras. If you're enabling the selected camera while disabling the others, you could set the variable selected like this:

 function Update(){
   // selected is true when the parent camera is enabled:
   selected = transform.parent.camera.enabled;
   if (selected && Input.Get$$anonymous$$eyDown("space")){
     ...

Another possibility would be to activate the selected camera and deactivate the others in the camera switching script with SetActive() - this would not require any additional code to stop the shooters, since a deactivated object also deactivates all its children (what includes the weapon). I don't know how your camera switching script is, but that's a suggestion:

 public var cameras: Camera[]; // drag the cameras here
 private var curCam = 0;
 
 function Update(){
   if (Input.Get$$anonymous$$ouseButtonDown(1)){ // if right button pressed...
     curCam = (curCam+1)%3; // cycle to the next camera
     // activate only cameras[curCam], deactivate the others
     for (var i=0; i<cameras.Length; i++){
       cameras[i].gameObject.SetActive(i == curCam);
     }
   }
 }

    

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

17 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

Related Questions

ContactPoint with OnParticleCollision? 0 Answers

Rapid fire Help 1 Answer

need help with instantiating for making a bullet shoot 1 Answer

How to Sync bullet spawn with shoot animation 0 Answers

Enemy Damage problem 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