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 alexizz · Mar 28, 2014 at 06:09 PM · gameobjectinstantiate

problem with instantiate

Hey guys, i'm making a 2D game but i'have problem with my enemy shoot, when he instantiate a bullet, he instantiate many bullet but i would like one by one, this is the code of the enemy :

function OnTriggerStay(col : Collider) { if(col.gameObject.tag == "Player") { Instantiate(lazerBullet, shooter.transform.position, lazerBullet.transform.rotation); } }

and the code of the bullet :

 //var perso : Transform;
 var shootSpeed : int;
 
 function Start ()
 {
     
 }
 
 function Update ()
 {
     var shoot = Time.deltaTime * shootSpeed;
     transform.Translate(0,-shoot,0);
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Leuthil · Mar 28, 2014 at 06:33 PM

Perhaps try this?

 var shootSpeed : int;
 // Set in Editor to be how many shots are fired per second
 var shotsPerSecond : float;
 private var shotTimer : float;
 private var shotTimerElapsed : float;
 
 function Start ()
 {
     shotTimer = 1 / shotsPerSecond;
     shotTimerElapsed = 0;
 }
  
 function Update ()
 {
     shotTimerElapsed = shotTimerElapsed + Time.deltaTime;
 }
 
 function OnTriggerStay(col : Collider)
 {
     if (col.gameObject.tag == "Player") { 
         if (shotTimerElapsed >= shotTimer)
         {
             shotTimerElapsed = 0;
             Instantiate(lazerBullet, shooter.transform.position, lazerBullet.transform.rotation);
         }
     }
 }
 

You don't really want to be increasing a timer in a collision or trigger event since it can happen multiple times in the same frame depending on how many things are colliding in that frame.

Comment
Add comment · Show 1 · 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 JasonBricco · Mar 28, 2014 at 06:40 PM 0
Share

Good point, didn't think about the "multiple times in the same frame" part. Not used to doing it that way :)

avatar image
0

Answer by JasonBricco · Mar 28, 2014 at 06:15 PM

OnTriggerStay will repeatedly activate as long as the collider is touching. You could add in a delay, such as:

 var delay = 0.0;
 var delayAmount = 1.0;
 
 function OnTriggerStay(col : Collider)
 {
     if (col.gameObject.tag == "Player" && Time.time > delay)
     {
         delay = Time.time + delayAmount;
         Instantiate(lazerBullet, shooter.transform.position, lazerBullet.transform.rotation);
     }
 }

That way, you'll force it to wait (in this case) a second before firing again.

Comment
Add comment · Show 5 · 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 alexizz · Mar 28, 2014 at 06:19 PM 0
Share

thak's JasonBricco, but it works just for the delay but it always instantiate a lot of bullet in the same time :(

avatar image JasonBricco · Mar 28, 2014 at 06:24 PM 0
Share

That's odd... because it would only ever run the Instantiate function if the current time is greater than the delay time (and it should only run it once, because it immediately modifies the delay time to add some extra time to it).

Are you saying one instantiate call makes multiple bullets, or that instantiate is being run multiple times when it shouldn't be?

avatar image alexizz · Mar 28, 2014 at 06:31 PM 0
Share

when the instantiate function is called, it's instantiate multiplayer of bullet, and i tryed to change the instantiated object and it's doing the same sh... lol

avatar image JasonBricco · Mar 28, 2014 at 06:36 PM 0
Share

So even if you put this Instantiate() call in a start or awake function it would make multiple of the object? Sorry, I'm just trying to understand exactly what's wrong. Seems like an odd issue to me.

avatar image alexizz · Mar 28, 2014 at 07:08 PM 0
Share

maybe it was instantiate since de character enter in the trigger zone and as there were no shoot per second, maybe that was the problem

avatar image
0

Answer by alexizz · Mar 28, 2014 at 06:44 PM

it WORK's, thank you so much dude and sorry if my english is not well, i'm French lol

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

21 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

Related Questions

Instantiate at Location Problem 2 Answers

[Solved]Instantiating prefab from a script and destroy it from another one 2 Answers

Get Component on Instantiated Object 1 Answer

Instantiate problem 1 Answer

Instantiate A Prefab 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