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 Tijs · Dec 20, 2012 at 05:41 PM · 2dprefabspawnforceontrigger

How to spawn a prefab on a trigger?

Dear community,

Im working on a vertical 2D platformer, and i am trying to use if statements to trigger certain events in my game.

for example when the player hits a collider, i want to spawn a prefab(comet) with a constant force component added to it.

Ive tried all kinds of code, but i cant seem to make it work.

it is for a school project and would realy appreciatie any kind of help or advise.

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
0
Best Answer

Answer by greatestprez · Dec 20, 2012 at 09:22 PM

Make sure your prefab has a rigidbody attached to it, and try something like this (note that code hasn't been tested)

 void OnTriggerEnter(Collider col) {
     
              if (col.gameObject.tag == "MyTag") {
     
                      GameObject ref = Instantiate(myObj, myPosition, myRotation) as GameObject; 
                      ref.GetComponent<Rigidbody>().AddForce(new Vector3(myForceX, myForceY, myForceZ) * mySpeed)
                      
              }
 }

  
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 Tijs · Dec 21, 2012 at 11:26 AM 0
Share

This method looks good! what kind of variables must i declare to make this work?

Thanks in advance!

avatar image greatestprez · Dec 21, 2012 at 03:13 PM 0
Share

you'd need a tag on the object interacting with your trigger ("$$anonymous$$yTag" in this example), a public variable to hold your comet prefab ("myObj" in this example), a quaternion and a vector3 to Instantiate the prefab with a position and rotation ("myPosition" and "myRotation" in this example), you need a rigidbody on your comet prefab, you 3 floats to move the comet in a direction("myForceX", "myForceY", "myForceZ" ), and you would also need a speed variable to make it move at a certain speed("mySpeed")

avatar image
0

Answer by Tijs · Dec 27, 2012 at 10:25 PM

To make this work i used the Enable/Disable Component on trigger to call it in. and destroy(object,5) to make it disappear. But it seems i cant destroy a rigidbody that way.

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
avatar image
0

Answer by TheRealKuha · May 18, 2016 at 07:26 PM

I found this video explaining it pretty good, also tells you how to spawn something that falls down, a rigidbody: https://www.youtube.com/watch?v=_Xrw2EEhzI4

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

12 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

Related Questions

Children of Instantiated Prefabs are not seperate 1 Answer

Turning a menu on and off onclick in hololens 0 Answers

Move towards mouse position on button down (2D)? 1 Answer

How do I transform the position of my cube AWAY from the larger cube its attached to? 0 Answers

no velocity/force after build unity4 JS 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