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 /
This question was closed Jun 29, 2018 at 02:27 PM by dhon2407 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by dhon2407 · Jun 28, 2018 at 03:11 PM · scripting problembeginner

2d Raycasting on rotating rectangle.

I was doing my collisions via Raycasting from a collider horizontally and vertically base on collider bounds, problem is when rotating the collider, I want the rays to be perpendicular on the collider rectangle itself not on its bounds. Any idea how to do it properly?

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

  • Sort: 
avatar image
0

Answer by dhon2407 · Jun 29, 2018 at 01:52 AM

More details:

I was following tutorials from youtube by Sebastian Lague on collisions via Raycasting. I just want to add some rotation to the box collider (like swinging weapon). I'm missing some math formula and having hard time figuring it out(not good at Trigo). Anyways here are some excerpts from code. I've added also some picture for visual reference. I know this will be easy for someone but can find the specific formula. :) alt text

    public void UpdateRaycastOrigins()
     {
         Bounds bounds = collider2d.bounds;
         bounds.Expand(skinWidth * -2);
 
         raycastOrigins.bottomleft = new Vector2(bounds.min.x, bounds.min.y);
         raycastOrigins.bottomRight = new Vector2(bounds.max.x, bounds.min.y);
         raycastOrigins.topleft = new Vector2(bounds.min.x, bounds.max.y);
         raycastOrigins.topright = new Vector2(bounds.max.x, bounds.max.y);
     }
 
     void CalculateRaySpacing()
     {
         Bounds bounds = collider2d.bounds;
         bounds.Expand(skinWidth * -2);
 
         float boundsWidth = bounds.size.x;
         float boundsHeight = bounds.size.y;
 
         horizontalRayCount = Mathf.RoundToInt(boundsHeight / dstBetweenRays);
         verticalRayCount = Mathf.RoundToInt(boundsWidth / dstBetweenRays);
 
         horizontalRaySpacing = bounds.size.y / (horizontalRayCount - 1);
         verticalRaySpacing = bounds.size.x / (verticalRayCount - 1);
 
     }

 



raycast-01.jpg (104.1 kB)
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 dhon2407 · Jun 29, 2018 at 02:26 PM 0
Share

Answer to this can be found here.

This lines of code will to the trick:

 BoxCollider2D collider = (BoxCollider2D) this.gameObject.GetComponent<Collider2D>();            
          
          float top = collider.offset.y + (collider.size.y / 2f);
          float btm = collider.offset.y - (collider.size.y / 2f);
          float left = collider.offset.x - (collider.size.x / 2f);
          float right = collider.offset.x + (collider.size.x /2f);
          
          Vector3 topLeft = transform.TransformPoint (new Vector3( left, top, 0f));
          Vector3 topRight = transform.TransformPoint (new Vector3( right, top, 0f));
          Vector3 btmLeft = transform.TransformPoint (new Vector3( left, btm, 0f));
          Vector3 btmRight = transform.TransformPoint (new Vector3( right, btm, 0f));


Follow this Question

Answers Answers and Comments

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

I want to have a cannon that infinitely fires after a certain number of time but my script crashes unity 1 Answer

How do you make a method that gets called when a button gets highlighted using the arrow keys? 0 Answers

How to persist a List? 2 Answers

How do I make my joint slerp from 1 rotation to another? 1 Answer

X is a variable but a type was expected 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