• 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
1
Question by SquigglyFrog · Jun 20, 2014 at 04:33 PM · 2draycastanglesteeringobstacle avoidance

2d Raycasting, trouble drawing rays at the correct angles

I hate asking for help, but I've been at this for a couple hours last night and today and still running into troubles.. Im trying to work on 2d steering and obstacle avoidance using raycasts. The trouble is the angles of my rays and drawing them. I'm trying to get 3 rays, a center, and a left and right each offset by X degrees (say 20 degrees).. which seems to work fine IF the transform is NOT rotated. As soon as a rotation is applied, my left and right rays always seem to go askew..

This seems like it should be simple, but for some reason I am just struggling here.. and transform.forward always points back behind the scene hence the use of transform.right.. I don't think that should matter, but.. What am I missing (aka doing wrong) & what can I do to CORRECTLY aim and draw my rays in the desired direction even when the transform is rotated?

alt text

Heres the entire code in its CURRENT form.. which is revision 9,120,100,203.. or it feels like it.. I appreciate any help!

     void Update () 
     {
 
         bool centerHit = false;
         bool leftHit = false;
         bool rightHit = false;
         Vector3 lDir = new Vector3(Mathf.Cos(0.35f), Mathf.Sin(0.35f), 0);
         Vector3 rDir = new Vector3(Mathf.Cos(-0.35f), Mathf.Sin(-0.35f), 0);
 
 
         RaycastHit2D hit = Physics2D.Raycast(transform.localPosition, transform.right, 3f, 1 << LayerMask.NameToLayer("waterObstacles"));
         RaycastHit2D lHit = Physics2D.Raycast(transform.localPosition, transform.right + lDir, 3f, 1 << LayerMask.NameToLayer("waterObstacles"));
         RaycastHit2D rHit = Physics2D.Raycast(transform.localPosition, transform.right + rDir, 3f, 1 << LayerMask.NameToLayer("waterObstacles"));
 
 
 
         if (hit)
         {
             if (hit.transform.tag == "rockObstacle")
             {
                 centerHit = true;
                 Debug.Log("Center - " + hit.collider);
             }
         }
 
         if (lHit)
         {
             if (lHit.transform.tag == "rockObstacle")
             {
                 leftHit = true;
                 Debug.Log("Left - " + lHit.collider);
             }
         }
 
         if (rHit)
         {
             if (rHit.transform.tag == "rockObstacle")
             {
                 rightHit = true;
                 Debug.Log("Right - " + rHit.collider);
             }
         }
 
 
         if (!centerHit)
             Debug.DrawRay(transform.localPosition, transform.right * 3f, Color.yellow);
         else
             Debug.DrawRay(transform.localPosition, transform.right * 3f, Color.red);
 
         if (!leftHit)
             Debug.DrawRay(transform.localPosition, transform.right + lDir * 3f, Color.green);
         else
             Debug.DrawRay(transform.localPosition, transform.right + lDir * 3f, Color.red);
 
         if (!rightHit)
             Debug.DrawRay(transform.localPosition, transform.right + rDir * 3f, Color.blue);
         else
             Debug.DrawRay(transform.localPosition, transform.right + rDir * 3f, Color.red);
 
     }


Sincerely,

currently lost and confuzzed...

doh.jpg (48.5 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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by robertbu · Jun 20, 2014 at 04:37 PM

You can calculate your left and right directions by:

 Vector3 lDir = Quaternion.AngleAxis( 20.0f, Vector3.forward) * transform.right;
 Vector3 rDir = Quaternion.AngleAxis(-20.0f, Vector3.forward) * transform.right;

Then you use these directly (not added) in your Raycast():

 RaycastHit2D lHit = Physics2D.Raycast(transform.localPosition, lDir, 3f, 1 << LayerMask.NameToLayer("waterObstacles"));
 RaycastHit2D rHit = Physics2D.Raycast(transform.localPosition, rDir, 3f, 1 << LayerMask.NameToLayer("waterObstacles"));   
Comment
Add comment · Show 2 · 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 SquigglyFrog · Jun 20, 2014 at 06:40 PM 0
Share

Thank you, I knew I was doing something wrong.. time to read up more on the angleaxis and why I use vector3.forward.. and to move on to making it steer the way I want to now..

avatar image robertbu · Jun 20, 2014 at 08:10 PM 0
Share

Vector3.forward is the axis of rotation, so this caused the vector to be rotated around the 'z' axis. You can multiple a vector by a Quaternion in this order (Q v) but you cannot multiply (v Q)...the compiler will generate an error.

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

2 People are following this question.

avatar image avatar image

Related Questions

How do I make a button interactible when it's overlaped with an other button ?,How to make a button hit area fit the shape of its image 1 Answer

Object jumps through roof when holding "Up" button? 1 Answer

Questions about movement on uneven/slopped terrain and terrain coliders 0 Answers

Getting a character to fly(follow cursor in 2D) in Unity4.3 0 Answers

Optimize 2D menu game by disabled raycast target 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