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 chris gough · May 09, 2012 at 03:46 PM · collisioncollidercontrolleroncollisionenter

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

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by aldonaletto · May 10, 2012 at 12:03 AM

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...
        }
    }
}
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 chris gough · May 10, 2012 at 03:02 PM 0
Share

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

avatar image chris gough · May 10, 2012 at 10:30 PM 0
Share

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

avatar image aldonaletto · May 11, 2012 at 03:56 AM 0
Share

$$anonymous$$y 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.

avatar image jdwieber · Jun 03, 2012 at 09:08 PM 0
Share

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

avatar image
0
Wiki

Answer by Levantez · May 09, 2012 at 05:22 PM

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

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

Comment
Add comment · Show 1 · 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 chris gough · May 09, 2012 at 11:04 PM 0
Share

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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Weird Collision Detection with a Cube and OnCollisionEnter 2 Answers

Check if collider is colliding with anything non specific? 1 Answer

Make Character Controller a Box? 2 Answers

Quickly spinning player glitches through walls 2 Answers

OnCollisionEnter2D being called upon exiting collider? 1 Answer


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