x


Simple - Collision + Trigger question

Hey guys i have two things to ask,

  1. I have 2 colliders, when on falls onto the other one the script works, but if i walk up to it after it has fallen and it is standing idle then the script does not work.

I need some help -_-

  1. Whats the coding ( java ) for OnTrigger() events?

Thanks

more ▼

asked Jan 26, 2010 at 08:49 AM

Trouch gravatar image

Trouch
67 27 923 40

it's java script and not java.

Jan 26, 2010 at 10:05 AM Ashkan_gc

Mazzarx, generally you shouldn't post two unrelated questions in one here on UnityAnswers. Post them one by one as separate questions instead. Thanks!

Jan 26, 2010 at 11:31 AM runevision ♦♦
(comments are locked)
10|3000 characters needed characters left

5 answers: sort voted first

OnControllerColliderHit() is only called on the GameObject containing the CharacterController Component, not on the colliding rigidbody, so you'd have to do it the other way round, detecting if your character hit something and then determining if it was a cube/crate whatsoever.

Oh and the parameter for OnControllerColliderHit is of type ControllerColliderHit. ;)

more ▼

answered Jan 26, 2010 at 01:30 PM

StephanK gravatar image

StephanK
6.6k 117 106 165

Thanks!, i add the code to the Character Controller but i know that isnt enough, what exactly do i need to do more? im not exactly sure about " ControllerColliderHit "

Jan 26, 2010 at 02:28 PM Trouch

And yeah, i'am really new to this

Jan 26, 2010 at 02:28 PM Trouch

not sure what you want to do, so it's hard to tell. The function in your script that is supposed to handle your collision events is written as "function OnControllerColliderHit(other:ControllerColliderHit)" ControllerColliederHit contains the information you need to determine what you hit. You can look up in the scripting manual what information is actually supplied.

Jan 26, 2010 at 02:34 PM StephanK
(comments are locked)
10|3000 characters needed characters left

to use triggers you should mark your colliders as triggers. check the "is trigger" box in collider's inspector. then there are three events that you can use. OnTriggerEnter will be called when a collision starts and OnTriggerExit will be called when the collision ends and two objects don't overlap. OnTriggerStay will be called once in each frame between OnTriggerEnter and OnTriggerExit. stay means two colliders are in a collision. after your object fall in to the other one, it should go back and exit from other's collider region and then come back in to call OnTriggerEnter again.

function OnTriggerEnter (other:Collider)
{
    //collision code
}

the function takes one argument that tells you who is colliding with you.

more ▼

answered Jan 26, 2010 at 10:18 AM

Ashkan_gc gravatar image

Ashkan_gc
10.1k 77 110 165

Ah ok thanks for explaining, but how do i assign a key for the trigger, e.g. ' Once " E " is pressed the collision code runs '

Jan 26, 2010 at 11:50 AM Trouch
(comments are locked)
10|3000 characters needed characters left

function OnControllerColliderHit(collision:Collision) { if(collision.gameObject.tag == "Player") {

     Debug.Log("yay");

 }

}

This code is attached to a cube that has a collider, When i use the standard First Person Controller to touch the box ( walk over to it ), Nothing happens

I know this code works only if its a moving object

And yes my First Person Controller has the tag " Player "

Thanks for help so far, and i'am sorry for posting two questions in one.

more ▼

answered Jan 26, 2010 at 11:46 AM

Trouch gravatar image

Trouch
67 27 923 40

(comments are locked)
10|3000 characters needed characters left

It's a bit hard to tell you what you're doing wrong if you don't say what you are actually doing... (code).

What you should look at is if all your objects have some sort of Collider attached to it, and that at least one of them is a dynamic (not kinematic) rigidbody. Otherwise Collisions won't work. I suspect that you are using a CharacterController to move your character, so OnCollisionEnter() won't be called. Try using the OnControllerColliderHit() for Collisions involving your character.

Triggers are different from Collisions. To receive trigger events you have to mark your Collider as Trigger. Then use the OnTriggerEnter/Stay/Exit methods of the Collider.

more ▼

answered Jan 26, 2010 at 09:20 AM

StephanK gravatar image

StephanK
6.6k 117 106 165

(comments are locked)
10|3000 characters needed characters left

"how do i assign a key for the trigger"

You'll need to manually check whether the key is pressed or not in your OnTriggerEnter code using Input.GetKey() or a similar function.

more ▼

answered Jan 26, 2010 at 05:16 PM

Duke gravatar image

Duke
463 5 7 16

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x9516
x4379

asked: Jan 26, 2010 at 08:49 AM

Seen: 27167 times

Last Updated: Jan 26, 2010 at 08:49 AM