• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Rotation issue with remote players projectiles 0 Answers

How to instantiate object at particular location 1 Answer

why is my object's rotation affecting it's position? 1 Answer

transform position y changes when character rotates 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges