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 superluigi · Jul 29, 2013 at 11:56 PM · charactercontrollercollisionflags

How to detect on which side my character controller was hit?

I've just barely started messing with collision flags but they only detect a side hit and don't specify which side. I'm experimenting with 2D so I could have if my character is facing this direction and is hit on this side to get my desired result. Collision flags sometimes returns an integer not sure if I can use that to my advantage. Thank you in advance for any response.

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 robertbu · Jul 30, 2013 at 12:15 AM

One easy way is to compare the angle between the character controller and the center of the collier object.

 function OnControllerColliderHit (hit : ControllerColliderHit) {
     var v3 = hit.transform.position - transform.position;
     var angle = Vector3.Angle(v3, transform.forward);
     
     if (angle > 45.0 && angle < 135.0)
         Debug.Log("Side hit");
 }

This is not a perfect solution. The calculation is done from the middle of the character controller to the pivot. But unless the collider is on a capsule or a sphere, the hit is likely not at this exact position. You don't get contact points with a ControllerColliderHit. If this turns out to be a problem, you might try using Collider.ClosestPointOnBounds() as the point to use in generating the vector for the angle calculation. The second issue is that the angle is in 3D space. That is, if the object collided with is much shorter or taller, it can skew the angle upwards. That can be fixed by bringing the point used in the calculation to the 'y' level of the character controller. The calculation could be:

 var v3 = hit.transform.position;
 v3.y = transform.position.y;
 v3 -= transform.position;
 var angle = Vector3.Angle(v3, transform.forward);
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 superluigi · Jul 30, 2013 at 05:00 AM 0
Share

hey robert what about something like

  function OnTriggerenter(other : Collider)
     {
        if(other.tag=="Enemy" && other.position.x > position.x)
        {
           do this;
        }
     }


You know a lot more than I do. Do you think that might work or if not then something like it?

avatar image robertbu · Jul 30, 2013 at 05:50 AM 0
Share

@superluigi - No I don't think it will work as is. For example say your character was hit just barely right of center in the front. Your code would call that a side hit. If you are using two capsule colliders, you might get something like this to work:

 if (other.tag == "Enemy") {
     var delta = $$anonymous$$athf.Abs(other.position.x - position.x);
     if (delta > someValue) {
         Debug.Log("Side hit");
     }
 }

Where 'someValue' would likely be around 1/2 of the character width.

avatar image superluigi · Jul 30, 2013 at 07:06 AM 0
Share

ok thanx. Helpful as always.

avatar image robertbu · Jul 30, 2013 at 05:42 PM 0
Share

IF your question is answered, please click on the checkmark next to the answer. Thanks.

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

15 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

character controller asset tells me the name CharacterMotor does not exist and i cannot test play me level 0 Answers

characterController rotate around an object help! 1 Answer

CharacterController prefab script 0 Answers

Input.GetAxis won't start working properly. 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