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 ryno9788 · May 28, 2015 at 05:28 AM · collisionrigidbodydirectioncollision-detection

Checking collision direction.

I'm writing this new controller that uses a rigid body as the main character. What I would like to implement into it, is a way to tell if a character is touching a wall and what side of the character side of the character the wall is touching.(meaning right, left, back and front for animators sake) I know character controllers have a method detect collision from different sides but do rigid bodies have one as well? Or will I have to create away to do it with raycast? If so I think I should say I have never worked well with arrays and if you can could you bring up an example. The main problem with this is I really don't know what direction (hahaha puns) I should go with this.

and if you do show any code can it please show it in c#? if not it'll be fine

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 virgiliu · May 28, 2015 at 06:00 AM

Create 2 child objects on the character, put them one at the left, one at the right.

Set up a check radius in the controller.

Then on collision enter do a Physics2D.OverlapCircle() and see which one of the points is hitting. http://docs.unity3d.com/ScriptReference/Physics2D.OverlapCircle.html

Play with the radius so that it covers most of the body.

Something like this (didn't test but it should give you a good starting point):

 public GameObject leftCheck;
 public GameObject rightCheck;
 
 public float directionCheckRadius = 0.4f; // Play with this until you find the right radius
 
 public LayerMask wallLayer; // Set it to whatever you want to check collision against, optional but helps with speed to not check against every layer
 
 
 void OnCollisionEnter2D(Collision2D other)
 {
   bool rightHit = Physics2D.OverlapCircle(leftCheck.position, directionCheckRadius, wallLayer);
   bool leftHit = Physics2D.OverlapCircle(leftCheck.position, directionCheckRadius, wallLayer);
 
   if(rightHit) Debug.Log("Hit on right");
   if(leftHit) Debug.Log("Hit on right");
 }


...Or you could do raycasting from each side by setting up a point at the top, middle and bottom of the character for each side so you can detect even if the character is jumping and collision is happening with only their head or feet.

This might also help https://youtu.be/99HFfubBjYg?t=15m27s

Comment
Add comment · Show 1 · 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 ryno9788 · May 28, 2015 at 06:55 PM 0
Share

does my leftcheck and rightcheck have to be game objects I could i make them transforms cause its giving me an error with them as gameobjects but when I swapped them with transforms to get their positions nothing happened when walked up to the wall.and how would I use this with a 3 dimensional object. I'm using the thirdperson controller

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

20 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

Related Questions

Collision-detection ALWAYS happens 1 Answer

collision detection on child rigidbodies 1 Answer

Getting Turret to Fire on the Correct Top-Down Plane 0 Answers

rigid bodies passing through one another. 1 Answer

How can I apply a force to an object in the opposite direction of the object it's colliding with? 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