Pressing Right and then Left shoots me into the sky

4.2

Much like the topic says, pressing D and then A in rapid succession while airborne turns the character into a missile. Here’s my code:

extends CharacterBody2D
var is_in_air = false
var move_modifier = 1
var player_color = 0
signal jump
@onready var jump_signal = connect("jump", _on_jump)
func _ready():
	pass
func _physics_process(_delta):
	#handles movement
	velocity = Vector2(0,0)
	if is_in_air == true:
		move_modifier == move_modifier * 0.5
	else:
		move_modifier != move_modifier
	if Input.is_action_pressed("right"):
		velocity.x = 300 * move_modifier
	if Input.is_action_pressed("left"):
		velocity.x = -300 * move_modifier
	if Input.is_action_just_pressed("jump") and is_on_floor() or is_on_wall():
		emit_signal("jump")
	if is_on_floor() == false: 
		velocity.y += 75
		is_in_air == true
	move_and_slide()
		
	#handles color
	if Input.is_action_just_pressed("blue_swap"):
		var blue = load("res://player-sprite-blue.png")
		$Sprite2D.texture = blue
		player_color = 2
	if Input.is_action_just_pressed("red_swap"):
		var red = load("res://player-sprite-red.png")
		$Sprite2D.texture = red
		player_color = 0
	if Input.is_action_just_pressed("green_swap"):
		var green = load("res://player-sprite-green.png")
		$Sprite2D.texture = green
		player_color = 1

func _on_jump():
	$"..".apply_impulse(Vector2(0,-1200))

Edit: I did some testing, and it can happen in either direction if there’s even a slight change in elevation.

This bit of code likely doesn’t do what you expect it to. This is a comparison, not an assignment. You probably want there to be just one = sign.

There a few things wrong with this code.

== doesn’t assign a value, it compares if the values are equal. So your code is never actually changing the values of move_modifer or is_in_air.

You are setting the velocity.x to either 300 or -300 depending on which key is pressed, if both are pressed you’d run both if conditions and end up with -300, as that is checked second.

connect returns an Error, so assigning that to jump_signal is a little odd.

I’m not entirely sure why this code would make you shoot into the sky. I’d assume is_on_floor() would still work as expected, so you should only set velocity.y = 75 once, until you touch the floor again.

Can you post a video of the issue?

I’d recommend reading the CharacterBody2D tutorial from the official docs.

The condition
Input.is_action_just_pressed("jump") and is_on_floor() or is_on_wall() does not do what you want.

Due to operator precedence (GDScript Reference: Operators) this is evaluated as

(Input.is_action_just_pressed("jump") and is_on_floor()) or is_on_wall()

which means that your player jumps as soon as it touches a wall.

You want to write that condition is

Input.is_action_just_pressed("jump") and (is_on_floor() or is_on_wall())

which lets your player jump if they press “jump” and either are on the floor or touching a wall.