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 chrisd313 · May 13, 2020 at 12:26 PM · 2draycastraycasthit2dflip

Raycast2D will point right, but won't point left when player turns.

Hey everyone,

I'm attempting to make a 2D Metroidvania, and I'm currently trying to implement ledge grabbing using 2 raycasts, which seems to be the recommended solution.

However, my raycasts will point right when my character faces right, as normal. However when my player turns to the left, the raycast doesn't point left with it. I have a feeling that it is something to do with how I am flipping the player, as I am flipping it with local.Scale. I'll include sections of my script below.

My raycasts are sitting in my fixed update.

         isTouchingWall = Physics2D.Raycast(wallCheck.position, wallCheck.right, wallCheckDistance, m_WhatIsGround);
         isTouchingLedge = Physics2D.Raycast(ledgeCheck.position, ledgeCheck.right, wallCheckDistance, m_WhatIsGround);

Here is the part of the script that grips on to the ledge:

     private void CheckLedgeClimb()
     {
         if (ledgeDetected && !canClimbLedge)
         {
 
             canClimbLedge = true;
 
             if (m_FacingRight)
             {
                 ledgePos1 = new Vector2(Mathf.Floor(ledgePosBottom.x + wallCheckDistance) - ledgeClimbXOffset1, Mathf.Floor(ledgePosBottom.y) + ledgeClimbYOffset1);
             }
             else if (!m_FacingRight)
             {
                 ledgePos1 = new Vector2(Mathf.Floor(ledgePosBottom.x - wallCheckDistance) + ledgeClimbXOffset1, Mathf.Floor(ledgePosBottom.y) + ledgeClimbYOffset1);
             }
         }
 
         if (canClimbLedge)
         {
             transform.position = ledgePos1;
             m_Rigidbody2D.isKinematic = true;
         }
     }

And here is the flip function: public void Flip() { m_FacingRight = !m_FacingRight;

         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }

I'd really appreciate any help, thanks!

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
0

Answer by ADiSiN · May 14, 2020 at 10:22 PM

Hi!

You are right in your suggestions - it because you are flipping by applying negative scaling.

Take a look at this picture:alt text The yellow and red lines is your Physics2d.Raycast functions.

By the way you can Debug it by yourself, remember it's always useful to do it:

 Debug.DrawRay(wallCheck.position, wallCheck.right * wallCheckDistance, Color.red, 1f);
 Debug.DrawRay(ledgeCheck.position, ledgeCheck.right * ledgeCheckDistance, Color.yellow, 1f);

I only added separate value for ledgeCheckDistance so it will be taller.


As you can see, the issue is that even though the image is flipped but only for view - axises orientation stays the same: transform.right is still the same.

One way to solve it is to actually rotate your object:

 transform.Rotate(Vector2.up, 180);

Here is the result:alt text As you can see - the axises now pointing in facing direction.


However, if for some reason you need it to be negative scaled, not rotated then you can instead of using wallCheck.right and ledgeCheck.right use variable that will be switched after flipping, for example:

 Vector2 direction = Vector2.right;
 
 private void Update()
 {
     isTouchingWall = Physics2D.Raycast(wallCheck.position, direction, wallCheckDistance);
     isTouchingLedge = Physics2D.Raycast(ledgeCheck.position, direction, ledgeCheckDistance);
 
     if (Input.GetKeyDown(KeyCode.M))
     {
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
         direction = -direction;
     }
 }

As you can see I am using Vector2 direction and each time I reverse it after flipping.

I'm using Update function and GetKeyDown for myself, of course, you don't need to do that.


Hope it helps.


example2.png (255.4 kB)
example.png (262.0 kB)
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

303 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

2D Raycast over multiple objects not working? 1 Answer

2D Raycast effect only what it hits? 2 Answers

Raycast for a 2D objects 0 Answers

Layer Mask on raycast2d not working? 1 Answer

How to find out in 2d the distance betwen beginning of the 2d raycast and the point of meeting with 2d collider. 2 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