- Home /
Raycast not working as intended
In this game the Player can faint by jumping against a wall, and they "recover" by touching the ground.
To recover them I first need to check if they are touching the ground. I do this by running two raycasts downwards, one on the left side of the collider and one on the right.
My problem is that the Raycast on the left side works as intended but if the player hits a wall with their right side they immediately recover without touching the ground.
void UnfaintPlayerIfGrounded()
{
Vector2 colliderSize = player.GetComponent<BoxCollider2D>().size;
var distToGround = colliderSize.y/2;
Vector2 startPosLeft = new Vector2(player.transform.position.x - (colliderSize.x / 2), player.transform.position.y);
Vector2 startPosRight = new Vector2(player.transform.position.x + (colliderSize.x / 2), player.transform.position.y);
if (Physics2D.Raycast(startPosLeft, -Vector2.up, distToGround + 0.5f) || Physics2D.Raycast(startPosRight, -Vector2.up, distToGround + 0.5f))
{
playerHasFainted = false;
}
}
I would add some logging so you can see exactly what's happening.
For example, you could store the RayCast results in RaycastHit structs and log the name of the object(s) that have been hit. This would confirm that this code is what's causing the behaviour as well as revealing what object is doing so.
Answer by Laviniagg · Sep 06, 2021 at 11:24 AM
void Update () {
direction = (player.transform.position - hook.transform.position).normalized;
lastNode = hook;
RaycastHit2D hit = Physics2D.Raycast(player.transform.position, Vector2.right);
if(hit.collider == lastNode)
{
Debug.Log("sd");
}
}
There is Hook and Player.When player move left to hook,"sd"print must come but it doesn't.What is wrong?
I'm sorry but I don't seem to understand what "hook" or "lastNode" reference.
Your answer
Follow this Question
Related Questions
Multiple raycast not working? 0 Answers
Player sinking into walls and floor with Raycasts 1 Answer
Why does my 2D raycast not always work? 1 Answer
How to determine if the player can jump, without using raycasts. (2D) 1 Answer
Findout if an object that was hit by raycast is still being hit (30 object that are instantiated) 0 Answers