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!