x


Character controller collision

i am attempting to make it so that when you collide with an object you collect it so i have this script on the object:

function OnCollisionEnter(collision : Collider){
       if(collision.gameObject.tag == "Player"){
       print(gameObject.name);
       var player = GameObject.Find("First Person Controller");
       var stacking = GameObject.Find(gameObject.name+"_model").GetComponent(stacking);

    if (stacking.stackAmount >=1){
    stacking.stackAmount++;
    Destroy(gameObject);
    }else if (Inventory.itemNum < 8){
    player.GetComponent(Inventory).AddItemToInventory(gameObject);
    stacking.stackAmount++;

} } }

the issue is since im using the character controller it isnt registering the collision and i cant use OnControllerColliderHit as it causes me alot of issues. thanks in advance

more ▼

asked May 09 '12 at 03:46 PM

chris gough gravatar image

chris gough
97 5 7 9

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

2 answers: sort voted first

Usually you should make the item a trigger and use OnTriggerEnter in the player or item script (the event is sent to both). But when the item has a rigidbody with Use Gravity checked, it falls through the floor. A solution is to place the trigger in a child of the main object: child an empty object to the item, add a collider (usually a sphere) and set Is Trigger. Modify the OnCollisionEnter to OnTriggerEnter like below and attach the script to the trigger, not to the item:

function OnTriggerEnter(other: Collider){
    if (other.tag == "Player"){
        print(parent.name); // the parent is the item
        var player = other.gameObject; // the player is the "other" object
        // use the parent's name to find the script stacking
        var stacking = GameObject.Find(parent.name+"_model").GetComponent(stacking);
        if (stacking.stackAmount >=1){
            stacking.stackAmount++;
            Destroy(gameObject);
        } else if (Inventory.itemNum < 8){
            player.GetComponent(Inventory).AddItemToInventory(gameObject);
            stacking.stackAmount++;
            // I suspect you should destroy the object in this case too...
        }
    }
}
more ▼

answered May 10 '12 at 12:03 AM

aldonaletto gravatar image

aldonaletto
41.2k 16 42 195

ill try this out now and see it works, thanks for the help

May 10 '12 at 03:02 PM chris gough

yea this works just need to make it destroy the parent object now and I am golden so thanks =D

May 10 '12 at 10:30 PM chris gough

My bad! It should be

  Destroy(transform.parent.gameObject);

By the way, other references like parent.name should also be altered to trasnsform.parent.name etc.

May 11 '12 at 03:56 AM aldonaletto

Aldo, thanks for the suggestion, it works like a charm!

Jun 03 '12 at 09:08 PM jdwieber
(comments are locked)
10|3000 characters needed characters left

If it's collecting an item, try using TriggerEnter instead.

Make sure you check is trigger on the item's collision component.

more ▼

answered May 09 '12 at 05:22 PM

Levantez gravatar image

Levantez
38 9 18 22

i tried a trigger and that didn't work either, also a trigger makes it fall through the floor.

May 09 '12 at 11:04 PM chris gough
(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:

x2482
x1682
x519
x227

asked: May 09 '12 at 03:46 PM

Seen: 2839 times

Last Updated: Jun 03 '12 at 09:08 PM