• 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
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 w$$anonymous$$te 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 (w$$anonymous$$ch 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 $$anonymous$$tSomet$$anonymous$$ng = 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 $$anonymous$$t = Physics2D.Raycast(position, (mousePosition - position).normalized, maxWallDistance);
             if ($$anonymous$$t.collider != null)
             {
                 if ($$anonymous$$t.collider.gameObject.CompareTag("isEnvironment"))
                 {
                     $$anonymous$$tSomet$$anonymous$$ng = true;
                 }
             }
         }
         Vector2 positionLast = new Vector2(transform.localPosition.x + 0.25f, transform.position.y - 0.24f);
         RaycastHit2D $$anonymous$$tLast = Physics2D.Raycast(positionLast, (mousePosition - positionLast).normalized, maxWallDistance);
         if ($$anonymous$$tLast.collider != null)
         {
             if ($$anonymous$$tLast.collider.gameObject.CompareTag("isEnvironment"))
             {
                 $$anonymous$$tSomet$$anonymous$$ng = true;
             }
         }
         if ($$anonymous$$tSomet$$anonymous$$ng)
         {
             return false;
         }
         return true;
     }



illustration.png (50.9 kB)
Comment

People who like this

0 Show 0
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Following another object's position/rotation like parent/child relationship? 4 Answers

Using the Rule Set function from the PiXYZ Plugin to try to reset Position or Translation with the 'Set > Transform' node without resetting the Rotation field either 0 Answers

How to predict position of object before transform.translate will apply? 1 Answer

Rotation issue with remote players projectiles 0 Answers

why is my object's rotation affecting it's position? 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