` So I have a player that, when shooting and pressing the right or left key to walk, he keeps sliding while shooting.
I don’t want that. I want the player to stop moving and sliding when pressing the shoot key only.
I want something like this
if Input.is_action_just_pressed("shoot"):
shoot()
sprite_2d.animation = "shoot"
Move and slide = stop()
Or = False
But i don’t how to do it. or what the code is? `
player script
extends CharacterBody2D
const SPEED = 400
const JUMP_VELOCITY = -750
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
sprite_2d.animation = "jump"
if Input.is_action_just_released("jump"):
sprite_2d.animation = "idle"
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction := Input.get_axis("left", "right")
if direction !=0:
$Sprite2d.flip_h=direction <0
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
if Input.is_action_just_pressed("shoot"):
shoot()
sprite_2d.animation = "shoot"
if Input.is_action_just_released("shoot"):
sprite_2d.animation = "idle"
if Input.is_action_just_pressed("shoot2"):
shoot2()
sprite_2d.animation = "shoot2"
if Input.is_action_just_pressed("shoot3"):
shoot3()
sprite_2d.animation = "shoot3"
# Animations
if Input.is_action_just_pressed("left"):
sprite_2d.animation = "left"
if Input.is_action_just_released("left"):
sprite_2d.animation = "idle"
if Input.is_action_just_pressed("right"):
sprite_2d.animation = "right"
if Input.is_action_just_released("right"):
sprite_2d.animation = "idle"
func shoot():
var b = preload ("res://bullet.tscn").instantiate()
b.direction = -1 if sprite_2d.flip_h else 1
get_tree().root.add_child(b)
b.transform = $Muzzle.global_transform
func shoot2():
var c = preload ("res://bullet2.tscn").instantiate()
c.direction = -1 if sprite_2d.flip_h else 1
get_tree().root.add_child(c)
c.transform = $Muzzle.global_transform
func shoot3():
var a = preload ("res://bullet_3.tscn").instantiate()
a.direction = -1 if sprite_2d.flip_h else 1
get_tree().root.add_child(a)
a.transform = $Muzzle.global_transform
Keep track of your player’s state. For instance, with a boolean:
@export var is_shooting : bool = false
Set the variable to true if the player is pressing any of the shoot keys, and set the variable to false is the player is not pressing any of the shoot keys. Then only change the player’s velocity if the variable is true.
Introducing states like this will make your code harder to read. Consider making a character controller state machine.
You want to toggle is_shooting true when the key is pressed and false when the key is released.
When the key “left” and probably “right” are pressed, before anything else, check if is_shooting == true and if so, return from that if statement without doing anything else.
EDIT: true, if it’s true, don’t do anything. Not when it’s false. Sorry.
Search a tutorial on a simple statemachine. You need to set up state control. So, that you block your code that you don’t want running wjen is_shooting is true.
You need to turn is_shooting to true and false in code.
Rather, than having someone give you working code you don’t understand it would be better to do some research on state machines. If there is something you don’t understand/struggle implementing in that state machine then come back and ask a more directed question.
All that to say. Focus your area of study on state machine logic, since that is what you need here.