The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

Hey guys!

I'm trying to make a simple controller script for a plateformer project.

I tried something that work... a bit, but i have a behavior that i can't explain...

i use a tilemap to create level portion, and sometimes, the character randomly get stuck on the ground, and when i move a bit in the other direction, the player can move again...

Really strange.

Here is the script i did :

export var player_speed = 150;
export var acceleration = 10;
export var acceleration_air = 2;



export var gravity = 300;
export var jump = 50;


 var velocity;
 var ray_cast_ground;


var player_state_prev = "";
var player_state = "";
var player_state_next = "ground";


func move(accel,delta,speed):
velocity = lerp(get_linear_velocity().x,speed,accel*delta);
set_axis_velocity(Vector2(velocity,get_linear_velocity().y));

func isGrounded():
if ray_cast_ground.is_colliding():
    return true;
else:
    return false;


func ground_state(delta):
if Input.is_action_pressed("ui_left"):
    move(acceleration,delta,-player_speed);
elif Input.is_action_pressed("ui_right"):
    move(acceleration,delta,player_speed);
else:
    move(acceleration,delta,0);


if isGrounded():
    if Input.is_action_pressed("jump"):
        set_axis_velocity(Vector2(0,-jump));
        print("jump");

else:
    player_state_next="air"

func air_state(delta):
if isGrounded():
    player_state_next="ground";

if Input.is_action_pressed("ui_left"):
    move(acceleration_air,delta,-player_speed);
elif Input.is_action_pressed("ui_right"):
    move(acceleration_air,delta,player_speed);
else:
    move(acceleration_air,delta,0);

func _ready():

set_applied_force(Vector2(0,gravity));
ray_cast_ground = get_node("RayCast2D");
ray_cast_ground.add_exception(self)
set_process(true)
pass

func _process(delta):

player_state_prev = player_state;
player_state = player_state_next;

print(get_linear_velocity().x);

if (player_state == "ground"):
    ground_state(delta);
elif(player_state == "air"):
    air_state(delta);

If somebody have an idea...
best regards!

in Engine by (55 points)

Next time you run the game toggle "Visible collision shapes". Usually it gives one idea of what's happening.
Also try to print debug messages when changing from one state to other and see what happens when stuck in some place.

2 Answers

0 votes
Best answer

ok i found the solution!

I used a Rayshape2D for bottom collision and now it's working!

by (55 points)
0 votes

here are some screens from the game whith the collision shapes:

moving :
http://www.hostingpics.net/viewer.php?id=514230moving.png

http://www.hostingpics.net/viewer.php?id=471368Sanstitre.png

and when i'm stuck :

http://www.hostingpics.net/viewer.php?id=816337stuck.png

it's a bit hard for me to see what's wrong... i tried to custom friction, but no effect...
my RigidBody is a Character.

can you see something?

by (55 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.