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 Memo · Sep 15, 2014 at 11:11 AM · falling-through-floorfalling-through-terrainrocks

Help with falling rocks

I'm making a game and a part of a game when a player reach to a place the rocks start falling , And i wrote a script to do this job , But the problem is the rocks are keep falling through the Terrain , And the number of the rocks is unlimited , How can i fix that problem ?

here is the script that i used :

 pragma strict
 var prefabs : Transform[];
 
 function OnTriggerEnter() { 
   
     Instantiate (prefabs[0]); 
 }

Please 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

3 Replies

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

Answer by tanoshimi · Sep 15, 2014 at 07:33 PM

Your question is missing a few details, but here's my guess of what's going on:

  • You've got the script above attached to some static object in the scene, with a collider (with "Is Trigger" set) that represents the area in which you'd like the rocks to appear.

  • When the player first enters the collider, the OnTriggerEnter() function is fired, which causes a rock (specifically, the first object in the prefabs[] array) to be instantiated.

  • Since you're not specified a position parameter in your Instantiate, the rock is being created at (0,0,0). However, I suspect this location lies within the trigger area.

  • So, the newly instantiated rock causes the OnTriggerEnter() to fire again, spawning a new rock, which causes OnTriggerEnter to fire again, ad infinitum.

To solve this problem, you need to be more specific to specify that OnTriggerEnter should only spawn a rock when the player enters the trigger, not just any object. i.e.:

 function OnTriggerEnter(col : Collider)
 {
     if (col.gameObject.tag == "Player")
     {
         Instantiate (prefabs[0]);
     }
 }

(There's a whole extra question about whether you want to, presumeably, spawn all the objects in the prefabs array rather than just the first, but we'll address that separately.

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 Memo · Sep 16, 2014 at 10:41 AM 0
Share

Thank you very much , It is working perfectly now!

avatar image
1

Answer by RedDevil · Sep 15, 2014 at 11:13 AM

Add a collider to the terrain and add a collider to the rocks and also add a rigidbody to the rocks so there wont be any problems with the rocks falling.

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 Memo · Sep 15, 2014 at 04:23 PM 0
Share

The rocks have a collider and a rigidbody , And my player can stand on the terrain but the rocks can't , But i want to know more about how can i put a number of the rocks that it is going to fall , Becuse the rocks are falling with unlimited number. Can you help me about the number of rocks thing please ?

Thank you!

avatar image
0

Answer by KingMatthew · Sep 15, 2014 at 06:01 PM

To stop the rocks from falling, have a script deactivate the instantiating script when the player leaves the area. An example script here:

 function OnTriggerEnter(col : Collider)
 {
  
     if (col.gameObject.tag == "Player")
     {
    GetComponent("SCRIPTNAMEHERE").enabled = false;
    
    
    }
 }
 

Add a collider component to the same gameobject that your instantation script is attached to. place it at the point where, when your player runs into it, you want to stop the rocks. Make sure the collider is a trigger, and make sure the player has the tag of "Player".
Then, just change the "SCRIPTNAMEHERE" to whatever the name of your instantation script is.

I hope this helps. This might not be the best way of doing it, but it should work.

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 Memo · Sep 16, 2014 at 10:48 AM 0
Share

I have add a collider to the gameobject , And the collider is a trigger , And my player also has the tag of "Player" , And i have change it the "SCRIPTNA$$anonymous$$EHERE" to the name of my script , But it is giving me an error :(

Is there any other way to do so ??

Thank you for your help!

avatar image KingMatthew · Sep 16, 2014 at 06:47 PM 0
Share

Tanoshimi has a much better answer than me. I tried though.

Can you still tell me what the error was? I want to fix it for future references. Thanks

avatar image Memo · Sep 16, 2014 at 11:54 PM 0
Share

Of course the error says like this : Assets/RocksDropper.js(9,39): BCE0019: 'enabled' is not a member of 'UnityEngine.Component'.

If there anyway that i can fix the problem please tell me

Thanks!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

THIN objects can fall through the ground? (AMAZING SOLUTION) 2 Answers

2D Edge Collider Thickness? 1 Answer

How am I supposed to make sure that the player can fall through the ground? 3 Answers

Rigidbody object falling through floor. 8 Answers

Is it possible to make a Mesh Collider thicker? 2 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