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 Ochreous · May 11, 2013 at 11:52 PM · c#collisiondetection

C# GameObject is not detecting collision with Character Controller

Hi everyone, I have a script that detects when a Character Controller collides with a gameobject. I have attached my script to the gameobject but the script isn't detecting when I move the Character Controller into the gameobject. Any idea what is wrong with my script?

 using UnityEngine;
 using System.Collections;
 
 public class PlayerDetect : MonoBehaviour {
 
     void OnControllerColliderHit(ControllerColliderHit playerCollider){
      if(playerCollider.gameObject.tag == "Player"){
       Debug.Log("Hit Player");
      }
    }
 }
 
 
Comment
Add comment · Show 7
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 trs9556 · May 12, 2013 at 12:04 AM 0
Share

Is the tag of the player actually "Player"?

avatar image Ochreous · May 12, 2013 at 12:06 AM 0
Share

Yes it is.

avatar image robertbu · May 12, 2013 at 12:29 AM 0
Share

For OnCharacterColliderHit() to work, it must in a script attached to the game object with the character controller.

avatar image Ochreous · May 12, 2013 at 12:44 AM 0
Share

Ok but I want to put this script on to the gameobject that the Character Controller is walking into. Is there a way to do that and get it to work?

avatar image robertbu · May 12, 2013 at 12:53 AM 0
Share

You need to put it on the game object with the character controller. Then you can filter for the objects you want to impact the character.

Show more comments

2 Replies

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

Answer by aldonaletto · May 12, 2013 at 01:11 AM

OnControllerColliderHit events are only sent to the CharacterController script. If you want to do something in the other object's script, use SendMessage to call some specific function in it - for instance:

CharacterController script:

 void OnControllerColliderHit(ControllerColliderHit hit){
   if (hit.normal.y < 0.9){ // filter out ground collisions
     // try to call PlayerHit in the other object
     hit.gameObject.SendMessage("PlayerHit", hit, SendMessageOptions.DontRequireReceiver);
   }
 }

Other object's script:

 void PlayerHit(ControllerColliderHit hit){
   Debug.Log("Player Hit");
 }

A somewhat faster alternative is to check for the specific PlayerDetect script and, if found, directly call PlayerHit (CharacterController script):

 void OnControllerColliderHit(ControllerColliderHit hit){
   if (hit.normal.y < 0.9){ // filter out ground collisions
     // if the other object has PlayerDetect script...
     PlayerDetect otherScript = hit.gameObject.GetComponent<PlayerDetect>();
     if (otherScript){ // call its function PlayerHit
       otherScript.PlayerHit(hit);
     }
   }
 }

NOTE: This event happens all the time due to collisions with the ground. In order to quickly filter out such collisions, a simple trick is to ignore collisions where the normal angle from the ground is too steep. In this example, only normal angles < 64 degrees (sin(64) = 0.9) are processed. This trick works fine because hit.normal is a normalized vector, and in this case its Y coordinate is numerically equal to the sine of the elevation angle.

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 CgShady · May 12, 2013 at 12:59 AM

If you want to have it on the object you run into, you can use OnCollisionEnter.

http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html

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 Ochreous · May 12, 2013 at 01:15 AM 0
Share

I switched to OnCollisionEnter but it's still not detecting the player.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerDetect : $$anonymous$$onoBehaviour {
 
     void OnCollisionEnter(Collision collision) {
      if(collision.gameObject.tag == "Player"){
       Debug.Log("Hit Player");
      }
    }
 }
avatar image aldonaletto · May 12, 2013 at 01:16 AM 0
Share

OnCollisionEnter events are sent to scripts attached to both, the rigidbody and the other object, but there's a problem: such events are generated only when a rigidbody hits a collider or CharacterController, not the other way around (a character hitting a rigidbody, for instance)

avatar image CgShady · May 12, 2013 at 07:13 PM 0
Share

ah, my bad, sorry for the mislead.

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

17 People are following this question.

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

Related Questions

C# Collision Detection Help 0 Answers

Unity 3d C# Draw Circle and Detection 0 Answers

How to check if an object is colliding with another from another script ? 1 Answer

Having Problem With Performing Collison with two prefab object. 1 Answer

C# How to Detect Edges of a Collider 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