- Home /
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!
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: 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: 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.
Your answer
Follow this Question
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