hope someone here smarter than me can figure out my mistake. I have a simple platformer game using raycasts to check if you’re standing on the ground. I tried colliders, but started using raycasts to be able to jump just before the player collides and thereby losing his x-velocity to friction, which would be a problem to how the game is supposed to be played.
That is exactly what happens. If you fall fast enough, then the x-velocity turns to 0 instantly instead of the player jumping just before collision and retaining his x-velocity.
It seems my raycasts do not check in time, or something else fails that I just cant figure out. Here’s what I have tried so far by reading other posts complaining about raycasts not registering, all to no different result:
- Turning on Continous collision detection in my players rigidbody instead of Discrete
- Putting the raycasts in FixedUpdate (made it worse)
- Putting my Movement script right after Default Time in the section Script Execution Order in Project Files
public void Jump()
{
LayerMask ground = LayerMask.GetMask("Ground");
Vector2 bottomCenter = new Vector2(transform.position.x, transform.position.y - transform.GetComponent<Renderer>().bounds.size.y / 2);
Vector2 bottomLeftCornerPos = new Vector2(transform.position.x - transform.GetComponent<Renderer>().bounds.size.x / 2, transform.position.y - transform.GetComponent<Renderer>().bounds.size.y / 2);
Vector2 bottomRightCornerPos = new Vector2(transform.position.x + transform.GetComponent<Renderer>().bounds.size.x / 2, transform.position.y - transform.GetComponent<Renderer>().bounds.size.y / 2);
// Cast a ray straight down.
RaycastHit2D down = Physics2D.Raycast(bottomCenter, Vector2.down, 1f, ground);
RaycastHit2D downleft = Physics2D.Raycast(bottomLeftCornerPos, Vector2.down, 1f, ground);
RaycastHit2D downright = Physics2D.Raycast(bottomRightCornerPos, Vector2.down, 1f, ground);
// Determine the closest hit point
RaycastHit2D hit = down.collider != null ? down : (downleft.collider != null ? downleft : downright);
if (down.collider != null && (hit.collider == null || down.distance < hit.distance)) hit = down;
if (downleft.collider != null && (hit.collider == null || downleft.distance < hit.distance)) hit = downleft;
if (downright.collider != null && (hit.collider == null || downright.distance < hit.distance)) hit = downright;
// If it hits something...
if (hit.collider != null)
{
if (hit.distance >= 0.01 && hit.distance <= 0.1 && rb2d.velocityY <= 0)
{
rb2d.velocityY = jumpPower;
comboExperiration = ComboExpireTime;
comboCounter++;
//Since we are not colliding with floor, send a message to Player Stats that we jumped on a floor
if (hit.collider.gameObject.CompareTag("Floor"))
{
GetComponent<PlayerStats>().CollisionByJump(hit.collider.gameObject);
}
}
}
}
Jump() gets called in inputManager script:
void Update()
{
if (playerInput.MovementMap.Jump.inProgress)
{
player.GetComponent<PlayerMovement>().Jump();
}
Hope anyone here recognises this problem or can see my wrongings