- Home /
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
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.
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
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 co$$anonymous$$g into contact with the ground, or a building....?
-Grady
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...... :/
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?
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
Answer by Dominatro · Jun 21, 2013 at 10:43 PM
does the collision script go into the character controller or the rigidbody script?
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...
Your answer
Follow this Question
Related Questions
Complication with script 2 Answers
Can I choose the right collider? 1 Answer
Making the object collide while moving in the forward dir. (Vector3.forward) 1 Answer
Collision problem 0 Answers
I need help with my enemy collision. 1 Answer