Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 CJGaming · Dec 29, 2015 at 05:10 PM · rigidbodyinstantiatefpsshootingcolliding

Set up time between each instantiated object?

I have a script to shoot bullet's out of a gun, but the bullet's are being shot in such a short time that they collide with thenselves everytime the gun moves, how do i set the time (0.1 seconds) between each bullet shot so they don't collide?

Here's the script (not complete, only usefull parts):

 function Update () {
     if(Input.GetButton("Fire1") && bulletsInCurrentMag > 0) {        
         shoot();
     }
 }
 function shoot () {
     var pel = Instantiate(projectile, muzzle.position, muzzle.rotation);
     pel.GetComponent.<Rigidbody>().AddForce(transform.forward * 0);
     bulletsInCurrentMag -= 1;
     isFiring = true;
 }

PS: AddForce is set to 0 just so i can track the bullets. It still doesn't work with 1, 2 or 1000.

Anyone can help 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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by g__l · Dec 29, 2015 at 06:59 PM

The problem is that your method is getting called from Update() so that is every frame which is a very short time. So instead i would recommend using InvokeRepeating() which calls a selected method after so many seconds and repeats it every so many seconds. It takes in three parameters

 InvokeRepeating(string methodName, float time, float repeatRate); 

So put this code in your start function and choose the time since start to call the method and the repeat rate to call that method. Hope that helps...

Note - I code in C# sharp so when i say method i guess you mean function (function shoot ()). Any problems look at the documentation or video.

Comment
Add comment · Show 3 · 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 CJGaming · Dec 30, 2015 at 12:18 AM 0
Share

Thanks a lot man, will try it out!

avatar image CJGaming · Dec 30, 2015 at 12:32 AM 0
Share

Didn't work man, apearently in JS, it's only Invoke("function", float time); and the problem is still the same, just as it was yield WaitForSeconds

@g__l

avatar image g__l · Dec 30, 2015 at 10:52 AM 0
Share

Remember you are using InvokeRepeating not Invoke. The second half of the video or documentation will help. Say if you have any more problems.

avatar image
0

Answer by bowloflol · Dec 30, 2015 at 01:05 AM

Try this, change attack speed to whatever you think is best.

 var attackSpeed         : float = .5;
 var attackSpeedCurrent  : float = 0;
 
 function Update () {
     if(attackSpeed > 0) {
         attackSpeedCurrent -= Time.deltaTime;
     }
 
     if(Input.GetButton("Fire1") && bulletsInCurrentMag > 0 && attackSpeedCurrent <=0) {        
         shoot();
     }
 }
 function shoot () {
     var pel = Instantiate(projectile, muzzle.position, muzzle.rotation);
     pel.GetComponent.<Rigidbody>().AddForce(transform.forward * 0);
     bulletsInCurrentMag -= 1;
     isFiring = true;
     attackSpeedCurrent = 0
 }

Comment
Add comment · 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

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

47 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

Related Questions

Script removing rigidbody component, NOT game object 1 Answer

Cant Get a Bullet to Shoot (C#) 2 Answers

FPS Raycasting vs Instantiate 0 Answers

Damage drop-off over distance on a physical projectile 1 Answer

Shoot projectile towards the end of a raycast? 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