0

Whenever I run my project my sprite cannot jump because the IsOnFloor() method always returns false although my character is still in contact with the tiles. I very new to Godot so any help to configure my collision settings ? would be very useful. My other controls and collision works fine. enter image description here

using Godot;
using System;
public partial class Player1 : CharacterBody2D
{
    private int gravity = 10;
    private int maxSpeed = 500;
    private int maxFallSpeed = 100;
    public int speed = 200;
    public int jumpForce = 100;
    public Vector2 screenSize;
    private bool jumpCheck = false;
    private float jumpTimer= 0f;
    private float maxJumpTime = 0.5f;
    
    public override void _Ready()
    {
        screenSize = GetViewportRect().Size;
        
    }
    
    public override void _Process(double delta)
    {
        var velocity = Vector2.Zero;
        if(IsOnFloor()==false)
        {
            velocity.Y += gravity * (float)delta;
        }
        if (IsOnFloor())
        {
                GD.Print("isonfloor = true");
        }
        else
        {
                GD.Print("isonfloor = false");
        }

        
        if (Input.IsActionPressed("left"))
        {
                            GD.Print("left");
            velocity.X -= speed; 
        }
        if (Input.IsActionPressed("right"))
        {
            GD.Print("right");
            velocity.X += speed;
        }
        if (Input.IsActionPressed("down"))
        {
            GD.Print("down");
            velocity.Y += speed;
        }
        if(Input.IsActionPressed("jump") )
        {
            GD.Print("jump");
            if(IsOnFloor() && !jumpCheck)
            {
                GD.Print("hello");
                jumpCheck = true;
                jumpTimer = 0f;
                velocity.Y = -jumpForce;
            }
            
            else if (jumpCheck && jumpTimer < maxJumpTime)
            {
                GD.Print("jump2");
                jumpTimer += (float)delta;
                velocity.Y = -jumpForce;
            }
            else 
            {
                GD.Print("jump3");
                jumpCheck = false;
            }
                    

        }

        velocity = velocity.Normalized() * speed;
        //MoveAndCollide(velocity);
        bool  collision = MoveAndCollide(velocity * (float)delta)!= null;   
        //Position += velocity * (float)delta;
    }
}
Theraot
  • 31,890
  • 5
  • 57
  • 86
Sreyas
  • 1

1 Answers1

0

In your code MoveAndCollide should update the result of IsOnFloor(). If the character body didn't move against the floor, it won't register it.

IsOnFloor() will return true if:

  • The motion_mode is set to Grounded. If motion_mode is Floating, you will only get IsOnWall, never IsOnFloor or IsOnCeiling.
  • You have set an up_direction. If the up_direction is a zero vector, it won't work.
  • The character body collider with something such that the angle between the normal of the collision and up_direction is less than floor_max_angle. Otherwise you get IsOnWall or IsOnCeiling.
Theraot
  • 31,890
  • 5
  • 57
  • 86