Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by TacoMakerMan · Jul 24, 2017 at 10:56 AM · rotationcollider2draycasthit2dphysics.raycast

Raycast2D doesn't work correctly on rotated object

So I have a simple script, it casts a ray from a point and upwards. I have a Debug.DrawRay that draws a ray from the start point and up, unless the ray hits something, in which case it will stop where the ray hit.

However, when the object (with a 2D capsule collider) that is obstructing the 2D raycast is rotated to a certain angle, the hit is not detected anywhere but the object's ends.

In case you don't understand what I mean, here is a demonstration

Here is my script:

     LineRenderer lr;
     Vector2 startPoint;
     Vector2 endPoint;
 
     void Awake () 
     {
         lr = GetComponent<LineRenderer> ();
         startPoint = lr.GetPosition (0);
         endPoint = lr.GetPosition (1);
     }
 
     void Update () 
     {
         RaycastHit2D hit = Physics2D.Raycast (startPoint, endPoint - startPoint);
         if (hit)
         {
             Debug.DrawRay (startPoint, hit.point - startPoint, Color.white);
         }
         else
         {
             Debug.DrawRay (startPoint, endPoint - startPoint, Color.white);
         }
     }

The start and end points are from a line renderer that I will use to actually show the ray in the game, and at the moment I am using it to set the start point and direction of the ray with two points.

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 bibbis · Aug 05, 2017 at 06:27 AM

I'm having the exact same issue but with Physics2D.Linecast!

I'm trying to learn how to use line casting between the enemies and the player to see if they are within shooting range.

I couldn't understand why the line was only able to hit the player when the player was in certain angles around the enemy.

Now I can see that it has to do with the rotation of the player - the rotation of the object being hit - just like in your example!

Video: http://recordit.co/YkrQDGmniK

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 TacoMakerMan · Aug 05, 2017 at 07:14 AM 0
Share

Yeah, I figured out that certain types of colliders work, while others don't. For instance, a capsule collider has all sorts of issues, but a polygon collider and a box collider seem to work fine. Also I think it's fixed the the 2017 Unity beta update thing, but I could be wrong. Anyway if it's not yet fixed I hope they fix it soon.

avatar image bibbis TacoMakerMan · Aug 05, 2017 at 09:59 AM 0
Share

You're right! $$anonymous$$y player uses a capsule collider to bumb into walls etc. So what I did to fix this was to add a smaller box collider inside the capsule. Now the player can be hit from all directions regardless of rotation!

avatar image TacoMakerMan bibbis · Aug 05, 2017 at 01:28 PM 0
Share

Awesome man, I'm glad you got it working :)

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

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

RayCastAll seemingly ignoring a particular collider 0 Answers

I'm trying to use Physics.Raycast to rotate the player to face the mouse cursor but it's not working like it should 0 Answers

How can I make the RyacastHit2D hit an item from far, without my player standing beside it (while blocking my item by its own collider)? 0 Answers

Why raycast2d not work? 0 Answers

Problems using RaycastHit2D.collider, detection only on the rays origin 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