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
2
Question by Steven-1 · May 25, 2012 at 01:16 PM · collisioncharacter controller

how to correctly check for a collision with a character controller

the player is a character controller, the enemy is a rigidbody.

in a script attached to the enemy I use OnCollisionEnter() to react when it hits the player. however, it barely ever notices a hit. it seems it often notices it when I use addforce on the enemy, but never when I just walk into it.

So I'm asking if anyone knows a way to correctly check for a hit in the enemy script, not in the player script, searching this site, that was most often the remedy. but I just can't beleive that there wouldn't be a way to correctly check it outside of the player. I mean otherwise the character controller is just completely useless.

Comment
Add comment · Show 4
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 timo0060 · May 25, 2012 at 01:30 PM 0
Share

Try making an object inside your enemy and make it a trigger. $$anonymous$$ake that a child of your enemy. Then just give it a tag name and use the OnTriggerEnter() function ins$$anonymous$$d of OnCollisionEnter(). Usually the triggers work better for me then using collision.

avatar image Steven-1 · May 26, 2012 at 04:54 PM 0
Share

works even worse :s god the character controller sucks, I guess I should just use a rigidbody for the player

avatar image You! · May 26, 2012 at 05:09 PM 0
Share

...Have you tried adding a different collider to the enemy in addition to the rigidbody? That should help, and you will still be able to use "OnCollisionEnter()".

avatar image Steven-1 · May 30, 2012 at 03:18 PM 0
Share

it already consisted of mutiple colliders as children of the rigidbody

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by aldonaletto · May 26, 2012 at 05:48 PM

The collision system has some weird features: OnCollision events are generated only when the rigidbody hits the character, not the other way around (to be more precise, sometimes they may be generated, but only when the rigidbody is also moving).
To detect character->rigidbody collisions, use the OnControllerColliderHit event. It's sent only to the character script, but you can create your own OnCharacterRigidbodyHit event in the rigidbody script and call it via GetComponent:

  • Rigidbody Script (let's suppose it's called RigidbodyScript.js):

function OnCharacterRigidbodyHit(point: Vector3, normal: Vector3, other: Transform){
  // this function will be called when the character hits the rigidbody;
  // in this example it will receive the rigidbody's hit point and normal,
  // and the transform of the character that hit the rigidbody
}
  • Character Script:

function OnControllerColliderHit(hit: ControllerColliderHit){
  if (hit.rigidbody){ // if a rigidbody was hit, try to get its RigidbodyScript:
    var rScript: RigidbodyScript = hit.rigidbody.GetComponent(RigidbodyScript);
    // if such script was found, call its OnCharacterRigidbodyHit event:
    if (rScript) rScript.OnCharacterRigidbodyHit(hit.point, hit.normal, transform);
  }
}
You can modify the fake OnCharacterRigidbodyHit to have any other arguments, case the ones suggested above aren't what you need.

NOTE: Many guys criticize the CharacterController, but I think it's yet the best solution for characters. From my experience, any kind of rigidbody based character I've tried to implement gave me way more headaches than the old-and-not-so-good CC...

Comment
Add comment · Show 2 · 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 Steven-1 · May 30, 2012 at 03:16 PM 0
Share

thanks, but I had already tried that, the problem is that it somehow triggers to much (like 10-20 times per actual hit). And it causes a small framedrop. And when I comment out the original OnCollision() it still doesn't always work, and when I leave them both (so nothing commented out), there are cases when it triggers both (so double effect). hm, I think I'll just do the CD manually.

avatar image aldonaletto · May 30, 2012 at 05:15 PM 0
Share

Are you using this exact code for OnControllerColliderHit? This event is generated every physics cycle due to the collisions between the character and the ground, thus we must ignore the undesired events - that's why the code checks if a rigidbody was hit before doing anything. Anyway, if you want a rigidbody character, try this: http://www.unifycommunity.com/wiki/index.php?title=RigidbodyFPSWalker

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

7 People are following this question.

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

Related Questions

How do I use a power up to change the character I'm controlling? 3 Answers

Determinism of collision judgment of Character Controller 0 Answers

How to slow character controller down when colliding with a wall? 2 Answers

Basics of Collision help. Working with character controllers. 1 Answer

Gun collision problem 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