Rigidbody2d velocity.y = 0 still moving

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By LotusOrb

Im using gameendeavors finite state machine. Physics are called every frame. For some reason when I call a state like block or crouch to have 0 velocity. My character still creeps forward toward the direction of the right or left keypress. I have found some work arounds but they dont really fix the problem, such as providing an opposing force. I also tried not calling the physics which contain move and slide when attack, crouch, or block state is active but this makes it so if I am on top or another rigidbody, my character will stay stuck in the air while the rigidbody passes underneath, which is undesired behavior. I want my character to perform block, attack, or crouch, firmally planted in place, but moving along with the rigidbody they are on top of.

An example code would help in your case
Also you know your question … yeah is not really a question

Wakatta | 2020-12-07 18:38

func _apply_movement():

var snap = Vector2.DOWN * Globals.UNIT_SIZE if !is_on_floor() else Vector2.ZERO
if move_direction == 0 && abs(velocity.x) < SLOPE_STOP:
	velocity.x = 0

if is_jumping && velocity.y >= 0:

is_jumping = false

#velocity.x = lerp(velocity.x, 0, -.12)

if (Input.is_action_pressed(“down_crouch”) ||Input.is_action_pressed(“attack”) ||Input.is_action_pressed(“block”)) && is_on_floor():# && ![states.jump, states.fall, states.run, states.idle].has(state):

velocity.x = clamp(velocity.x, 0,0)

velocity = move_and_slide_with_snap(velocity, snap, Vector2.UP)

if is_on_floor() && !jump_buffer.is_stopped():
	jump_buffer.stop()
	jump()

This is from the fsm

if ![states.attack, states.crouch, states.block].has(state) || !parent.is_on_floor():
if state != states.ledge_grab:
parent._apply_movement()

the question im asking is how to stop my player from moving from left or right when in a certain state but still have gravity applied to it, without just stopping the apply_movement function for said state.

LotusOrb | 2020-12-07 21:46

The solution if find myself using is to oppose the existing gravity when in the state. This was a number I had to figure out by testing. it has the added effect of letting me slide along the ground to a stop which is more appealing. There is probably an easier way to do this.

states.crouch:
#parent.velocity.x = 0
parent.velocity.x = parent.velocity.x + 11.17 * -parent.facing
if Input.is_action_pressed(“left”) && Input.is_action_pressed(“right”):
#parent.velocity.x = 0
parent.velocity.x = parent.velocity.x - 11.17 * -parent.facing
parent.crouch()

LotusOrb | 2020-12-08 03:05