Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This question was closed Dec 20, 2014 at 08:24 AM by AlucardJay for the following reason:

other : question is too broad / unspecific

avatar image
4
Question by PigInATuxedo · Dec 20, 2014 at 01:25 AM · collisionobjectgametouch

How do I check if an object is touching another object?

How do I check if the player is touching any object?

What I am trying to do is make it so I can jump if I am touching anything.

Comment
Add comment · Show 1
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 AlucardJay · Dec 20, 2014 at 08:22 AM 0
Share

Start in the Unity Learn section : http://unity3d.com/learn/tutorials/modules

1 Reply

  • Sort: 
avatar image
10

Answer by JackMini36 · Dec 20, 2014 at 02:12 AM

Use a collider on both objects. Also tag the objects, for example one could be "floor" and the other "player". Then use the OnCollisionEnter function to act accordingly if certain objects collide.

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

for example if you want to be able to jump if your players touch on anything:

 void OnCollisionEnter (Collider other) 
     {
         canJump = true;
     }
 
 void OnCollisionExit (Collider other) 
     {
         canJump = false;
     }

then in your player script, enable the player to jump only if canJump is true.

Comment
Add comment · Show 7 · 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 PigInATuxedo · Dec 20, 2014 at 05:13 AM 0
Share

Does this work in c#?

avatar image getyour411 · Dec 20, 2014 at 05:27 AM 0
Share

Yep it does.

avatar image warhammercasey · Oct 15, 2015 at 11:56 PM 1
Share

I just get an error saying This message parameter has to be of type: collision.

avatar image luckynamal · Dec 07, 2015 at 03:11 PM 1
Share

i am modifing above answer a little bit. void OnCollisionEnter (Collision other) { canJump = true; }

void OnCollisionExit (Collision other) { canJump = false; }

avatar image Giantbean · Aug 17, 2020 at 03:22 PM 2
Share

To clarify this for future viewers: The script above mixes syntax from two different functions. One for Collision and one for colliders. Use either void OnTriggerEnter(Collider other) or void OnCollisionEnter(Collision collision). Either one could be used in this scenario and work just fine.

On Trigger happens when two colliders make contact or "collide", one has a rigidbody and is set as a trigger. Its easy enough to use for telling when two objects are touching one another and then making an event accessible only if they are touching as seen above.

On collision happens when two objects have a "collision" and at least one has a non-kinematic rigidbody making it good to use if you need physics to know the contact point or to only register the collision if a velocity threshold has been met for changing the result such as how loud a sound is based on the velocity of the impact.

Either will work but for a simple jump detection OnTriggerEnter may require an extra trigger collider if you also need a physics collider but still uses a fraction less resources when calculating the results as it is not reading a collision object. The difference is near meaningless at a small scale but may add up on a larger project. So generally speaking, you can use a trigger when you have an object which doesn't interact "physically" and a collision when they do.

Show more comments

Follow this Question

Answers Answers and Comments

34 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 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

how to morph character on collision enter with gameobject 1 Answer

Slicing Objects 1 Answer

End game then an Object is close to another object? 0 Answers

Basic Collision, Object + First Person 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