• 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
Question by Grady · Jul 07, 2011 at 07:30 AM · collisionobjectscollidetwo

How to check if an object is colliding with another?

Hey guys,

I have searched Unity Answers and tried different collision scripts, but none of them seem to work. The console has no errors, and i have added box colliders to both the objects. Is there a simple script in which i can use to check if two objects are hitting each other, and then execute some code if they are.......

Here is the script that i am currently trying to use:

 var collisionObject : GameObject;
 
 function OnCollisionEnter(collision : Collision){
     if (collision.gameObject.name == collisionObject){
         Application.LoadLevel("game_scene_01");
     }
 }

Thanks

-Grady

Comment
shaderop
Romano
elz50

People who like this

1 Show 1
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 Dominatro · Jun 21, 2013 at 10:11 PM 0
Share

where would this go? in the character controller script or the rigidbody script? where it says name what name do i put?

var collisionObject : GameObject;

function OnCollisionStay(collision : Collision){ if (collision.gameObject.name == collisionObject){ //Application.LoadLevel("game_scene_01"); Debug.Log("I'm inside!!"); } else { Debug.Log("FREEE!!!!"); } }

I've got this in a script called collisionDetect attached to my controller...and i'm not getting any print lines to the console...which tells me nothing is happening.

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by GuyTidhar · Jul 07, 2011 at 07:57 AM

I do not know if you already done that but:

One of your objects need to be a non-kinematic rigidbody (or charactercontroller) for collisions to work.

http://unity3d.com/support/documentation/ScriptReference/Collider.OnCollisionEnter.html

Comment

People who like this

0 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 Grady · Jul 07, 2011 at 08:19 AM 0
Share

ermmmm... well i can't add a rigidbody to my aircraft, because otherwise it just falls, and i tried adding a rigidbody to the terrain, and trust me that didn't go down well, the ground just starting sinking down, (like the movie 2012 or somethin')..... :/.

How would i check if my aircraft is coming into contact with the ground, or a building....?

-Grady

avatar image Grady · Jul 07, 2011 at 08:25 AM 0
Share

i just unticked the "Use gravity" checkbox on the terrain's rigidbody

and changed the script to this:

function OnCollisionEnter(collision : Collision){ if (collision.gameObject.tag == "terrain"){ Application.LoadLevel("game_scene_01"); } }

but it will only execute the code inside the if statement once the aircraft gets a bit under the terrain, which is really wierd.... and other objects seem to be in the air...... :/

avatar image GuyTidhar · Jul 07, 2011 at 08:26 AM 0
Share

Do you need gravity to effect your aircraft? If not - just disable the useGravity variable of the rigidbogy.

Have you set the rigidbody to non-kinematic?

avatar image Grady · Jul 07, 2011 at 08:28 AM 0
Share

yea the rigidbody is non-kinematic....

avatar image Grady · Jul 07, 2011 at 08:29 AM 0
Share

ok thanks, it works now, although when my aircraft hits the terrain, it makes this wierd deep sound for a second, but i guess it doesn't matter because i can disguise it with a big loud exploding boom!

thanks

-Grady

avatar image

Answer by Dominatro · Jun 21, 2013 at 10:43 PM

does the collision script go into the character controller or the rigidbody script?

Comment

People who like this

0 Show 0 · 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

Answer by tsj · Dec 15, 2015 at 10:04 PM

I am trying to create a monster truck game where I can use a monster truck to crush junkyard cars...

I am using Unity 5 and using the skycar from the standard asset, but changed it so it resembles a monster truck...

I have downloaded a junk car in the assets store with multiple damage levels,

I want it to use its level 1 mesh before hitting it and then when hit by one of the wheels of the monster truck, to gradually change to level 2, then 3, 4 and finally 5.

I have added box colliders to the junk cars mesh levels, and currently (initially) level 2,3,4 and 5 meshes have their collider and mesh renderer unticked

how can I detect if a monster truck wheel hits the car?

my main body of the monster truck has a rigidbody but the wheels have not and it will always be one of the wheels that will hit the junk car

I can't just add rigid body, no cinematic and no gravity to my wheels can I? And if I can, do I then add the check to the cars carcontroller script, a script applied to each wheel, the junk car or something completely different ??

thanks in advance for any help...

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Making the object collide while moving in the forward dir. (Vector3.forward) 1 Answer

Complication with script 2 Answers

Can I choose the right collider? 1 Answer

collision with objects, physics 1 Answer

Placing objects error 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