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 Sting_Auer · May 25, 2012 at 05:15 PM · collisioncollidedetect

What is the code for detecting a collision?

I have the problem that my character keeps clipping through walls. I've come up with the solution that, if I lower the speed of the character upon collision with the wall, it won't clip through.

I've looked through the Unity scripting reference, but I'm having a lot of trouble finding which code I'm supposed to use. What is the code for detecting a collision?

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
1

Answer by oliver-jones · May 25, 2012 at 05:31 PM

OnCollisionEnter?

Here is the documentation:

Link Edited : OnCollisionEnter

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 Sting_Auer · May 25, 2012 at 05:43 PM 0
Share

Is there a way for me to detect a collision on a specific collider?

Your link isn't correct, by the way, but I did do a google search for it, and I'm having trouble making sense of that. Could somebody please show me the individual parts of the collision codes in that page and what they mean/how they are used?

avatar image Lo0NuhtiK · May 25, 2012 at 06:22 PM 0
Share

"Is there a way for me to detect a collision on a specific collider?"

void OnCollisionEnter(){ //no (parameters) //do stuff to whatever, without needing any info from the object hitting this object

    //can I detect a specific collider here? No. Why? Cuz we didn't get any info from what hit us.
 }
 //------------------------------------------------------------
 void OnCollisionEnter(Collision other){ //with (parameters)
    //do stuff and get info if we want
    // specific collider example :
    if(other.collider.CompareTag("Player")){ //if the thing that hit us is tagged as Player, then...
        //do stuff
    }

    //nothing else? ok, this thing only does stuff when "Player" hits us
 }

(Collision yourVariableName) gets info from the colliding object.
From that you can access the other object as if this script were on it.
e.g. :


    void OnCollisionEnter(Collision whatever){
        if(whatever.collider.CompareTag("CubeZilla")){
            //access a script on CubeZilla
            SomeScriptOnCubeZilla otherScript = whatever.collider.GetComponent<SomeScriptOnCubeZilla>() ;
            //change a variable value in that script from here
            otherScript.somePublicBooleanVariable = false ;
            //call a function on that script from here
            otherScript.SomePublicFunction() ;
        }
     }

Contact Points ...
foreach(ContactPoint in yourVariableName.contacts){
Contact points are exactly what they're called. Points on the colliders at which contact between the two colliders was made upon collision.
Everything on those script reference pages in Blue Font is a link to more information. Click on them, and read everything you come across. Hope this helps people a bit (and hopefully the code chunks format correctly so I don't have to edit this post..)

avatar image oliver-jones · May 25, 2012 at 06:27 PM 0
Share

If you want to check if a specific object/collider has hit, you can do:

function OnCollisionEnter(hit : Collision){

if(hit.collider.gameObject.tag == "YourTag"){

} }

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Camera Colliding with Terrain 1 Answer

Gameover On Collision 1 Answer

How to let the player collide with animated objects? 0 Answers

How do i understand which gameObject is player touching ? 1 Answer

Mesh Collider doesn't work without reset 2 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