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 hackgamn · Jan 02, 2014 at 06:23 PM · c#oncontrollercolliderhit

Cannot detect collision between objects

Hi, I am making my first game and I do not really understand the collisions. I was trying to detect a collision between my character(charactercontroller) and the enemy's sword(rigidbody, mesh collider), using oncontrollercolliderhit, but it did not work. Could anyone help me?

 The character's script 
 using UnityEngine;
 using System.Collections;
 
 public class PlayerStats : MonoBehaviour {
     
     //public int LVL;
     //public GameObject Target;
     public int MaxHealth; // Максимальное количество здоровья
     public int CurHealth; // Текущее количество здоровья
     public int MaxMana; // Максимально количество маны
     public int CurMana; // Текущее количество маны
     
     public bool AttackStats = false;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if(MaxHealth < CurHealth) CurHealth = MaxHealth;
         if(MaxMana < CurMana) CurMana = MaxMana;
         if(CurHealth < 0) CurHealth = 0;
     }
      void OnControllerColliderHit(ControllerColliderHit playerCollider){
      if(playerCollider.gameObject.tag == "sword"){
       Debug.Log("Hit Player");
      }
    }
     
     }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by BigRoy · Jan 02, 2014 at 09:33 PM

The OnControllerColliderHit() method is only called in combination with the Character Controller behaviour component.

Are you sure the object you're using contains the CharacterController behaviour component, because the OnControllerColliderHit method is called from that component (not from RigidBody component if I'm correct).

Also see the following links:

http://forum.unity3d.com/threads/29919-OnCollisionEnter-vs-OnControllerColliderHit

http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnControllerColliderHit.html

Put a Debug.Log("Hit Controller"); directly in the method to see if it's finding any hit in OnControllerColliderHit() method in general.

The OnControllerColliderHit() only gets called if the controller is moving at the same time

Quote from here:

  • "If you read the docs, that's how it should work: "OnControllerColliderHit is called when the controller hits a collider while performing a Move." (emphasis is mine.) If means it only checks during CC.Move or CC.simpleMove.
    To check for other stuff hitting you when it moves, just use a regular OnCollisionEnter."
    - Owen Reynolds

You might want to use the OnCollisionEnter() method instead if you always want to find any collision.

Otherwise you could try putting it in the OnCollisionEnter() method. Like so:

 void OnCollisionEnter(){
     if(playerCollider.gameObject.tag == "sword"){
         Debug.Log("Hit Player");
     }
 }

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

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

19 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 avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Flip over an object (smooth transition) 3 Answers

OnControllerColliderHit counting twice towards score 0 Answers

C# OnControllerColliderHit Detection Problems 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