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 /
avatar image
0
Question by SirSmileX · Jul 09, 2018 at 10:38 AM · rotationtransformpositionraycastingrelative position

Getting a Vector2 position depending on the rotation of an object

Hey Guys,


so I have a 2D game. I want my character to test, whether an object is too close to it by sending Raycasts to the mouse Position (the direction he will be going). If so then it should not move. My character is a cube with a small cube ontop to illustrate the direction


alt text


Now my character can also hang on these white walls, i.g. change rotation. In my code I want to calculate the position from where the rays should fire (I have rays colliding with own collider disabled). So I need to calculate these positions (blue dots) relative to the objects rotation. How do I do that? My approach is to first calculate the spacing. I do that by dividing the "width" of the sprite (which I hardcoded as 0.5f) by the number of rays - 1. I want the last ray to always be on the end so I do not include it. Then I calculate the position as seen in the for-loop. And I do not know how to rewrite it like so, that the position defined by the hard coded values are not in world space, but local space. How do I do that? transform.TransformPoint does not seem to work as well as multiplying by transform.up.


Maybe someone has a suggestions. Here is the code:


     private bool ValidateMove()
     {
         Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
         bool hitSomething = false;
 
         float spacing = 0.5f / rayCount;
         for (int i = 0; i < rayCount - 1; i++)
         {
             Vector2 position = new Vector2(transform.position.x - 0.25f + spacing * i, transform.position.y - 0.24f);
             RaycastHit2D hit = Physics2D.Raycast(position, (mousePosition - position).normalized, maxWallDistance);
             if (hit.collider != null)
             {
                 if (hit.collider.gameObject.CompareTag("isEnvironment"))
                 {
                     hitSomething = true;
                 }
             }
         }
         Vector2 positionLast = new Vector2(transform.localPosition.x + 0.25f, transform.position.y - 0.24f);
         RaycastHit2D hitLast = Physics2D.Raycast(positionLast, (mousePosition - positionLast).normalized, maxWallDistance);
         if (hitLast.collider != null)
         {
             if (hitLast.collider.gameObject.CompareTag("isEnvironment"))
             {
                 hitSomething = true;
             }
         }
         if (hitSomething)
         {
             return false;
         }
         return true;
     }



illustration.png (50.9 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

144 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

Related Questions

NullReferenceException when using raycast 1 Answer

transform.right not working correctly? 0 Answers

How to make Camera position Independent of its Rotation? 1 Answer

setting transform physics2d issue 0 Answers

Align objects based on child objects 0 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