Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 GameDevH2O · Sep 16, 2021 at 10:28 PM · bugfloatraycasthit2d

Raycast2D Bug

Hello, I have a small error in my game im not sure what causing it. Basically I have a 2D sprite that flips X in SpriteRenederer & when it does a ray should grown in value until it collides with given layers. But for some reason when facing left (Flip X is True) & the ray collides with a gameobject the ray flips facing right. But for the right side the ray is working as it should.

 public Transform viewpos;
 public float viewdis;
 public LayerMask OBJ;
 public bool off;
 
 // DETECTION
 if (GetComponent<SpriteRenderer>().flipX)
 {
         RaycastHit2D viewl = Physics2D.Raycast(viewpos.position, -transform.right, viewdis, OBJ);
         if (viewl.collider != null)
         { 
             if (!off)
             {
                 viewdis = viewl.collider.transform.parent.gameObject.transform.position.x - 0.54f;
                 off = true;
             }
             }
             else
             {
                 off = false;
                 viewdis += 0.05f;
             }
             Vector3 dirV = transform.TransformDirection(Vector3.left) * viewdis;
             Debug.DrawRay(viewpos.position, dirV, Color.red);
         }
         else
         {
             RaycastHit2D viewr = Physics2D.Raycast(viewpos.position, transform.right, viewdis, OBJ);
             
             if (viewr.collider != null)
             {
 
                 if (!off)
                 {
                     viewdis = viewr.collider.transform.gameObject.transform.position.x - 0.54f;
                     off = true;
                 }
             }
             else
             {
                 off = false;
                 viewdis += 0.05f;
             }
             Vector3 dirVr = transform.TransformDirection(Vector3.right) * viewdis;
             Debug.DrawRay(viewpos.position, dirVr, Color.red);
         }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by skagontale · Sep 16, 2021 at 11:43 PM

have the code flip the sprite renderer instead of the axis and make a LayerMask on the raycast to haveit only interact with a certain layer. most of this can be researched individuaklly and put togethjer.

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 GameDevH2O · Sep 17, 2021 at 12:34 PM 0
Share

it is flipping the sprite renderer & not the axis & their is a layer mask on the ray the problem is the ray flips to the right when it collides with a layer when it should either grow leftwards or stay facing left when colliding with given layers.

avatar image
0

Answer by GameDevH2O · Sep 17, 2021 at 04:49 PM

Figured it out had to put Physics2D.Raycast(viewpos.position, -transform.right, -viewdis, OBJ); & viewdis -= 0.05f; when facing left. Wish I never posted this question lol.

Comment
Add comment · 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

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

146 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

Related Questions

Float bug... no errors. 1 Answer

Float sets itself to zero 1 Answer

Rotation Values Garbled When Set By Code 2 Answers

Error with my float variable? 1 Answer

Unity4 Ignoring Inspector/Initialized Public Value in Build 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