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 pwego · Sep 05, 2015 at 12:26 AM · instantiatecolliderprefabtriggerone

My code is instantiating many prefabs, i only want one.

I have the player press E to stab with their sword. Which instantiates a trigger collider around the sword they cant see. If a monster collides with it they take damage and the whole bit. The problem is, the way I coded it spawns maybe 10 prefabs a second resulting in the monster taking a lot of damage really fast. I messed around and coded to destroy the prefabs all but just as fast as they come in but it still bugs a good bit. What should i do to only instantiate one?

  • Code attached to sword only deals with knockback.

  • Animation code attached to player says "attacking" bool to true when player presses E, and instantiates the prefab.

    void FixedUpdate () {

           if (Input.GetKey (KeyCode.E)) {
                 attacking = true;
                 attack();
             }else attacking = false;
     
     
             if (Input.GetKeyUp (KeyCode.E)){
                 attacking = false;
             }
    
    

-Here is the instantiate code ** (also in the animation script)

 void attack(){

     if (OneHandEquipped){
         instantiatedObj = (GameObject) PhotonNetwork.Instantiate(wepTriggerPrefabName, wepPoint.position, wepPoint.rotation, 0);}
     Destroy(instantiatedObj, time);
 }

 





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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by VMohan · Sep 05, 2015 at 03:20 AM

Instead of Input.GetKey use Input.GetKeyDown. Input.GetKey is called any time the button is pressed (which means you will call attack ~60 times if you hold the key for just one second), whereas GetKeyDown is called just once.

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 pwego · Sep 05, 2015 at 04:21 AM 0
Share

Ah yes thankyou, I remember I started with this and then changed to just Get$$anonymous$$ey and now I see why. The animation cancels and the prefab gets destroyed too quick. I put it in Update ins$$anonymous$$d of FixedUpdate and its still way too quick.

Anyway to let the animation play out? I want to be able to move and stab and at the end of the animation the bool will switch off ins$$anonymous$$d of just right away. Do you know how to achieve this?

avatar image VMohan · Sep 05, 2015 at 06:27 AM 0
Share

I would use a coroutine, where you wait a set amount of time (while the animation plays) and then perform all the actions you want such as switching the bool and destroying the gameobject.

avatar image pwego · Sep 05, 2015 at 01:18 PM 0
Share

If you want another best answer im about to post that question with the current code i have.

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

31 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

Related Questions

Destroy instatiate object on trigger enter / collision,destroy instantiate prefab on trigger enter 0 Answers

is the Character Controller a Collider? 2 Answers

OnTriggerStay Doesn't work if not moving 2 Answers

Can't Instantiate an object as child of another 1 Answer

Cannot destroy the SphereCollider on Instantiated Objects 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