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
0
Question by JakoDev · Jan 31, 2017 at 11:02 PM · shootingdelayupdate functionshot

4 Guns, but only 3 are shooting at the same time?

Hello friends, i'm working on a 3D-spaceship "ish" game. My spaceship has 4 guns, that i want to fire at the same time. Also, im checking if the guns have already been shot off. The code:

 private void Update()
     {
 
         countDelay -= Time.deltaTime;
 
 
         if (Input.GetAxis("Fire1") > 0 && countDelay <= 0) 
         {
                 checkWeapons();
                 if (leftAlive)
                 {
                     shoot(left);
                 }
                 if (rightAlive)
                 {
                     shoot(right);
                 }
                 if (rightRightAlive)
                 {
                     shoot(rightRight);
                 }
                 if (leftLeftAlive)
                 {
                     shoot(leftLeft);
                 }
             
             countDelay = shotDelay;
 
         }
     }

As you can see, I'm checking if each of the 4 gun parts are still on the ship (left, leftLeft, right, rightRight). Then I'm calling my shoot fuction on every gun which is still alive:

 void shoot(Transform shotPos)
     {
         GameObject projectile = Instantiate(projectilePrefab);
         projectilePrefab.transform.position = shotPos.transform.position;
         projectilePrefab.transform.rotation = transform.rotation;
         Rigidbody rb = projectile.GetComponent<Rigidbody>();
         rb.velocity = transform.forward * projectileSpeed;
     }

Just for info, the method which checks if the parts are on the ship goes as the following:

 void checkWeapons()
     {
 
         if (weapons[0].transform.parent == player.transform){
             leftAlive = true;
         } else {
             leftAlive = false;
         }
 
         if(weapons[1].transform.parent == player.transform){
             leftLeftAlive = true;
         } else {
             leftLeftAlive = false;
         }
 
         if(weapons[2].transform.parent == player.transform){
             rightAlive = true;
         } else {
             rightAlive = false;
         }
 
         if(weapons[3].transform.parent == player.transform){
             rightRightAlive = true;
         } else {
             rightRightAlive = false;
         }
     }

My problem: when shooting, the gun which i check thelast in Update() always fires with a delay. But when displaying the time in each of the if statements in Update() with "print(time.Time);" they all go at the same time... I apprechiate every idea :)

Greetings Jako

Comment
Add comment · Show 4
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 Scoutas · Jan 31, 2017 at 11:33 PM 0
Share

Personally, I don't see a problem in the code (and as you said, time.Time shows that they execute at the same time). It'd be lovely to see the setup of the guns on the ship, because I feel the problem might be there.

avatar image JakoDev · Feb 01, 2017 at 12:39 PM 0
Share

Hey Scoutas,

here's a picture of my current setup: alt text

The weapon which is delayed is the leftLeft one, I selected it in the Scene view. Everything seems normal but i can't find out whats wrong.

avatar image ampck · Feb 01, 2017 at 06:39 PM 0
Share

Try putting shoot(leftLeft); above shoot(rightRight);. It should NOT work in theory, but I did that before with Canvases for canvas.enabled = true/false, and it fixed my code for some reason.

avatar image JakoDev ampck · Feb 01, 2017 at 11:51 PM 0
Share

Hey ampck, I just tried your suggestion but it just doesn't matter in what order I put them, the last if statement is always delayed :(

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

93 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

Related Questions

HELP! How to make Update function start after delay? C# 2 Answers

How can i make the right choice of implementation of my shooting fire script ? 1 Answer

making a boomerang effect in a 2D enviorment 1 Answer

Help me to make a delay when shooting 0 Answers

Help with Editing Code 0 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