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
1
Question by C4sh · May 24, 2012 at 10:28 AM · collisioncollidercharactercontroller

Collision CharacterController vs CharacterController

Hi, I have my player character defined with a CharacterController, and I move my enemies the same way (with other CharacterControllers). The thing is, I was thinking about using OnControllerColliderHit for when my character is hit by enemies, so that he goes back hurt.

The problem is that OnControllerColliderHit seems to trigger on just when it hits a Collider, and as my enemies don't have a collider, but a CharacterController to move them, it's not working!

Is there a way around this? I don't want to change my enemies from being CharacterControllers, because both enemies and main character inherit from the same class (which has a CharacterController of course).

Thanks a lot!!

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 C4sh · May 24, 2012 at 02:30 PM 0
Share

I just realized that CharacterController.collisionFlags is working correctly, but I need more information, because I need the normal of the collision point to push my main character back, not just if the collision is from the side, above or below. Any ideas?

It's veeery wierd that both OnControllerColliderHit and OnCollisionEnter aren't working for CharacterController vs CharacterController when the very CharacterController inherits from Collider, which is $$anonymous$$ADE FOR THAT!! oO

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Zeldarulah · May 24, 2012 at 02:58 PM

Why not set enemies to have a certain tag like "Enemy", and do an OnTriggerEnter(Collider col) for your character, wherein you use:

 if (col.gameObject.tag == "Enemy") {
    //whatever you wanted to do here
 }
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 C4sh · May 24, 2012 at 05:00 PM 0
Share

Because I need more information (I want to push the main character in the direction it has been hit) so collider doesn't give me that. Collision would, or ControllerColliderHit, but they don't seem to be wokring...

avatar image Zeldarulah · May 24, 2012 at 05:25 PM 0
Share

Ah. Well, I'm new to Unity, but, if your enemies are 'targetting' your player, perhaps you could create an accessor method in enemy to that, and then use a GetComponent to use their accessor, and use that vector to push your player back.

avatar image
1

Answer by aldonaletto · May 24, 2012 at 05:49 PM

You're right: that's a weird thing to not have a character-character collision event! But maybe @zeldarulah's suggestion can do what you want: add a trigger volume to the character and a kinematic rigidbody (moving triggers need it to work); when the collision happens, calculate a "fake normal" based on the position of both characters - like this:

function OnTriggerEnter(other: Collider){
  var normal = (transform.position - other.transform.position).normalized;
  // this fake normal shows the direction from the other to this object, 
  // somewhat like hit.normal would do (if it worked, of course)
}
Notice that you must add the collider to the character (Unity asks if you want to add or replace the current collider).
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 C4sh · May 25, 2012 at 03:25 PM 0
Share

Thanks a lot! I surrendered yesterday and started doing another thing because nothing was working, but I'll try tonight or tomorrow and tell you how it went. From what I've seen in another thread, this volume should be a child of the object, like a sphere or something...

avatar image aldonaletto · May 26, 2012 at 01:18 AM 0
Share

In this case it will be easier to add a trigger ins$$anonymous$$d of childing a trigger object to the character: select the prefab and click menu Component/Physics/Capsule Collider - Unity will ask if you want to replace or add the collider: click Add, and the capsule collider will be added to the character without removing the CharacterController. Set the capsule collider Is Trigger field to make it a trigger, and adjust its radius in the Inspector to make it larger than the CharacterController. Add the OnTriggerEnter code to some script attached to the character, and voilà: it's ready.
If only the enemy applies damage to the player when hitting it, add the OnTriggerEnter event and the trigger only to the enemy prefab.

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

OnCollisionEnter Push Object Problem 0 Answers

Can static objects detect collisions 2 Answers

OnTriggerEnter() and OnCollisionEnter(), OnControllerColliderHit() not working with character controller 2 Answers

Making Colliders/Triggers or Rigidbodies move a Character Controller 5 Answers

Character Controller giving false collisions 0 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