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 Martin 3 · Oct 22, 2010 at 08:53 AM · collisiontagoncollisionenter

Ammo crate collision

Hi. I want to make a very easy script, that adds bullets to a character when colliding with a ammo crate.

function OnCollisionEnter(crate : Collision) 
{ 
    if (crate.gameObject.tag == "Ammo") 
    { 
       print("Collision");
    } 
}

This script doesn't work, and I dont know why. The ammo crate have a box collider, and the Character have a Character controller and I tried to add box collider to it, but the script still didn't work. What is the problem here? Or is there a easier way to do this?

Comment
Add comment · 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 FLASHDENMARK · Mar 27, 2011 at 07:51 AM 0
Share

When dealing with a characterController you SHOULD use OnControllerColliderHit.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Alec-Slayden · Mar 27, 2011 at 03:10 AM

Your problem stems from the way colliders and the character controller work. The character controller is not a physical object. It's designed to react to walls and the floor as though it was, but if it really were you'd have less control over how it moves, because you'd have to obey the laws of physics in the simulation.

So its collider is more of a constant ray-cast, rather than a true collision.

Not all colliders will detect each other. Here's a chart (bottom of the page) that shows what collisions are required to detect OnCollisionEnter, and OnTriggerEnter. This can be found at the bottom of each collider page in the reference docs.

Solution 1: Your character controller will properly cause OnTriggerEnter events. You can put box collider with the "is Trigger" checked, and a script on this involving OnTriggerEnter to grant ammunition. You can have it check if the tag is player, instead of the other way around.

Since it's a trigger, it won't stop the player from moving through it, so you'll want it to be separate from the ammo crate's box collider if it's to be a solid object.

Solution 2: If you want the script to be on the player (might be a good idea if there's other stuff to grab besides ammo), you should use OnControllerColliderHit in the character controler script.

In this function you can check the hit info of the ammo crate's game object / tag, and apply the code you want.

Comment
Add comment · 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
0

Answer by zannghast · Oct 22, 2010 at 08:58 AM

Did you check if one of the object has a non-kinematic rigidbody attached to it?

Collider.OnCollisionEnter on the Unity Script Reference section.

Comment
Add comment · Show 3 · 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 Martin 3 · Oct 22, 2010 at 09:14 AM 0
Share

I have tried that on my Character, but it didn't help

avatar image zannghast · Oct 22, 2010 at 10:03 AM 0
Share

I see. Forgive me for asking this, but, are you sure the object colliding with the collider is tagged as "Ammo"?

avatar image Martin 3 · Oct 22, 2010 at 10:56 AM 0
Share

Yes. I think there is something wrong with Colliders. I made a new scene with Unitys First person controller and A box. I added my script and Rigidbody to the controller, and started the game. When colliding, nothing happened.

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

No one has followed this question yet.

Related Questions

Detect a gameObject's tag via OnCollisionEnter() 5 Answers

Colliding with the tag of a Child Object 2 Answers

On Trigger Enter, Collide with object, specific collision 1 Answer

Collide detection with tag [JS] 0 Answers

Simple Collision Detection not working. [Solved] 3 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