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 Vioswift · Apr 10, 2014 at 06:01 AM · collisioncollide

Unity 3D C# - Collosion Code Not working?

This is my code

 using UnityEngine;
 using System.Collections;
 
 public class ActivateKill : MonoBehaviour {
 
     void OnCollisionEnter(Collision collision)
     {
         if(collision.gameObject.name == "Killer")
         {
             //Destroy(col.gameObject);
             Debug.Log("COLLISION!");
         }
     }
 
 }

Not sure what the problem is. The script is applied to the first person controller with a capsule collider with a capsule mesh, the capsule then collides with the cube, it is meant to show in the log that It has collided. but when it collides nothing happens nut sure, any suggestions?

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

3 Replies

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

Answer by HarshadK · Apr 10, 2014 at 06:19 AM

Ok! Here are the solutions.

There are two ways to achieve this:

1) OnControllerColliderHit()

2) OnTriggerEnter()

Method #1:

The function OnControllerColliderHit() is available for the controller.

Code with that is:

 void OnControllerColliderHit (ControllerColliderHit collision) {
         if(collision.gameObject.name == "Killer"){
          Debug.Log("COLLISION!");
         }
 }

But this is fired not just on controller enters the collision but also on stay.

Method #2:

Make your collider for controller object as a trigger.

Code is:

    void OnTriggerEnter(Collider collider)
     {
     
        if(collider.gameObject.name == "Killer")
        {
          //Destroy(collision.gameObject);
          Debug.Log("COLLISION!");
        }
     }

This will be fired once only when the collision is entered and not on stay.

As you are destroying the game object on collision, I suggest method #2 with trigger. Also OnControllerColliderHit() will be called continuously if you are walking on a floor which is just not required in this case.

Cheers!

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 Vioswift · Apr 10, 2014 at 09:07 AM 0
Share

no it didn't solve it for some reason, I put a rigedbody on the onto the FPC, then I froze the x,y,z so then the FPC doesn't fly in the air, and the cube is named "$$anonymous$$iller", i'm so confused, is it because its in C#?

avatar image HarshadK · Apr 10, 2014 at 01:37 PM 0
Share

I've updated my answer. Check it out.

avatar image Vioswift · Apr 11, 2014 at 07:52 AM 0
Share

$$anonymous$$ethod 1 works, thank you very much, now I know I have to use the colliderhit method

avatar image
0

Answer by HassanBajwa · Apr 10, 2014 at 09:37 AM

Add RigidBody to your first person controller and don't forget to Freeze its position to X,Y,Z.

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 Vioswift · Apr 10, 2014 at 10:55 AM 0
Share

I did that, same result, bit confused why its not working, does unity have some sort of cache I need to clear? I don't see any reason why this is not working, I put a ridgedbody on the cube and the FPC froze both X,Y,Z, script is put of the FPC, which has a capsole collider

avatar image HassanBajwa · Apr 15, 2014 at 10:12 AM 0
Share

Then there might be problem with "$$anonymous$$iller" object. You may try it with tag give a tag to object. and try collision.gameobject.tag = "";

avatar image
0

Answer by NDJoshi · Apr 10, 2014 at 09:22 AM

No, Your problem has no relation with any programming language (C#). I think you need to check your "killer" object's property. Is your object having rigidbody or not? And if your is kinematic is checked or not? Go through this, and i think it will solve your problem. Just let me know whether your problem is solved or not.

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

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

Related Questions

How can i detect if 2 object hit eachother from a script which is not attached to either one of the objects? 0 Answers

Camera raycast? 2 Answers

Can I choose the right collider? 1 Answer

How can I detect which SIDE of a box I collided with? 3 Answers

How to check if an object is colliding with another? 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