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 Attapi0 · Dec 07, 2013 at 04:30 PM · collisioncolliders

Can't seem to get collision to work

So I have a rocket prefab that instantiates every time you press the spacebar. It's supposed to be destroyed when it goes out of a camera's view, or hits the terrain. I can't get it to work. Take a look at my code.

 #pragma strict
 
 var terraintarget : GameObject;
 private var invis : boolean = false;
 private var terraincollide : boolean = false;
 
 
 function OnBecameInvisible() {
     invis = true;
 }
 
 function Update(){
 
 
 
     if(invis === true){
         Destroy(gameObject);
     }
     if(terraincollide === true){
         Destroy(gameObject);
     }
     
 
 }
 
     function OnCollisionEnter (collision : Collision) {
         
         if(collision.collider.name === "Ground"){
             Destroy(gameObject);
         }
     
     }

I tried changing it to print to the console the name of the colliders it hits, but that won't work either.

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

Answer by Narv · Dec 07, 2013 at 05:20 PM

some sanity checks first:

  • does your rocket have a collision on it? box collider, sphere, etc?

  • you are using a variable called terraincollide but never set the value to true but then you are checking if it's true..

  • you have a public variable for targetterrain. I assume you are setting that in the inspector but never reference it in the code.

  • the terrain you are hitting, is it a unity terrain or mesh? does it have a terrain collider or other collider of shorts?

  • have you tried using == instead of === in case unity is doing something funky with datatypes?

Comment
Add comment · Show 4 · 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 Attapi0 · Dec 07, 2013 at 05:45 PM 0
Share

The rocket does have collision on it. Both variable issues are just from me forgetting about them. Neither of them are supposed to be used right now at least. The terrain is a Unity Terrain with a Terrain Collider on it.

I tried what you said, and nothing changed.

avatar image Narv · Dec 07, 2013 at 05:51 PM 0
Share

which case happens most often? invisible or collision? I found this note on the method for OnBecameInvisible:

Note that object is considered visible when it needs to be rendered in the scene. It might not be actually visible by any camera, but still need to be rendered for shadows for example. Also, when running in the editor, the scene view cameras will also cause this function to be called.

as for the collision, you said you tried printing the name of the collider and it didn't work... wrong collider name or not printing anything at all? like a Debug.log("$$anonymous$$eh");

the other thing is do you have a rigidbody on the rocket? (required for collision) or is anything checked as "use as trigger" or whatever the ter$$anonymous$$ology is? I don't have unity open infront of me at the moment.

avatar image Attapi0 · Dec 07, 2013 at 05:53 PM 0
Share

THAN$$anonymous$$ YOU SO $$anonymous$$UCH, OH $$anonymous$$Y GOD! I.. I didn't know you needed a rigidbody for it to work...

avatar image Narv · Dec 07, 2013 at 05:55 PM 0
Share

haha no problem :) guess I should have put that at the beginning list :p yeah collision in unity in a scripting sense is for physics.. where as a non-physics based collision uses triggers.

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

17 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

Related Questions

Why does my player fly through my barrier? 2 Answers

Colliding fast moving object with a slow moving object 1 Answer

How to make two objects not go through each other (without physics) 3 Answers

Can two different composite colliders collide? 0 Answers

How to detect if target is inside an "arc" area? 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