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 AndreaF · Jun 30, 2015 at 04:31 PM · unity5collision detection

Cannot get Collider event of different object

I have a scrip attached to GUI canvas in this script I perform various operation related to game status.

Now I want to detect the hit of a bullet MyBullet to a target MyTarget I have tried o add in this script the follow code

 function OnCollisionEnter(collision: Collision) {
    for (var contact: ContactPoint in collision.contacts) {
        if(collision.gameObject.name=="MyTarget" || collision.gameObject.tag=="MyTarget" )
          {
          Debug.Log("Ball hit Box");
          points++;
          speed=bullet.velocity; 
         }
      }
  }


And nothing happens

I have altso tried to create in Start function

 var target=GameObject.FindGameObjectWithTag("MyTarget");
 targetCollider=target.GetComponent.<CapsuleCollider>();

adding this check in Update function

 if(boatCollider.isTrigger){
     Debug.Log("Ball hit Box");
     points++;
     speed=bullet.velocity; 
 }

With no result. How could I fix this?

Please note that this script is not attached to MyTarget object is attached to my GUI canvas object

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
Best Answer

Answer by Hellium · Jun 30, 2015 at 06:39 PM

Making some research before asking a question is always a good idea :

Check OnCollisionEnter on other GameObject

http://answers.unity3d.com/questions/519708/check-oncollisionenter-on-other-gameobject.html

Note that the FAQ mentions it :

Please look around to see if your question has already been asked (and maybe even answered!) before you ask.

Comment
Add comment · Show 4 · 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 AndreaF · Jun 30, 2015 at 07:18 PM 0
Share

The answer is for C# I'm on JavaScript

avatar image Hellium · Jun 30, 2015 at 07:21 PM 0
Share

Then, you know what you have to do !

http://docs.unity3d.com/ScriptReference/index.html

avatar image AndreaF · Jun 30, 2015 at 07:57 PM 0
Share

Could you help me to understand what is wrong with my code? If I have asked the question is because I haven't found the solution searching in official documentation

avatar image Hellium · Jun 30, 2015 at 08:09 PM 0
Share

I think the documentation is quite clear about this :

OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider

Thus, if you attach the code above on your GUI canvas, OnCollisionEnter will be called only if something has hit its collider.

If you want to detect if your object has hit something you must attach a script on your bullets with the OnCollisionEnter defined. There is no workaround.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Intersection of two game object 0 Answers

Cable-building simulations. 0 Answers

Collider with rigidbody does not register collision with terrain 2 Answers

where have i went wrong in my script (unity 5) 0 Answers

How do i get local Hit position of Raycast on a collider 1 Answer


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