I can't get my speed powerup to work

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

Hello, I can’t get a powerup in my game to work, I know I am doing something wrong, but I also don’t know what I have to do to get it working

Here is my code:


extends KinematicBody2D

var velocity = Vector2(0,0)
var crystals = 0
const SPEED = 100
const GRAVITY = 15
const JUMPFORCE = -300

func _physics_process(delta):
	if Input.is_action_pressed("right"):
		velocity.x = SPEED
		$Sprite.flip_h = false
	elif Input.is_action_pressed("left"):
		velocity.x = -SPEED
		$Sprite.flip_h = true
	if not is_on_floor():
	velocity.y = velocity.y + GRAVITY

func _on_speed_body_entered(body):
	SPEED == 150

The Powerup:

extends Area2D

signal powerup_collected

func _on_crystal_body_entered(body):
	$AnimationPlayer.play("bounce") #plays the bounce animation
	emit_signal("coin_collected") #emits the coin_collected signal
	set_collision_mask_bit(0,false) #sets the player collision mask to false so that we can't collide with it after we collect it
	$SoundCollect.play() #plays the collection sound

func _on_AnimationPlayer_animation_finished(anim_name):
	queue_free() #deletes itself after the bouncing animation finishes

Could someone please explain to me how do I get it to work?

You should provide a bit more context. What is the expected behavior and what’s happening. Also, how are the nodes related, how does the player knows he got a speed boost. Do you receive the signal from the coin?

MrEliptik | 2021-03-23 18:50

I’m trying to make make the player faster when he collects the object
I made a function so that when the player enters the body of the powerup, it changes his speed from 100 to 150, but I think it doesn’t work because SPEED is a const

the name of the powerup in game is just called speed, but SPEED (with all caps) is the constant for the velocity of the player

mrspaceblock | 2021-03-23 19:00

Yeah you’re right! I’ve posted an answer with another error.

MrEliptik | 2021-03-23 20:02

:bust_in_silhouette: Reply From: MrEliptik

I’m seeing two problems here:

  1. SPEED is a const which mean it can’t be modified.
  2. In func _on_speed_body_entered(body) you’re doing a comparison with == instead of an assignment with =

If you correct those two points, it should work better.

Just in case you missed it though, with the code you’re showing right now, the player won’t move unless they press something. It might be okay for you depending on your type of boost. Also, you don’t have code to revert the speed back. If you didn’t do it, remember to add a timer or something.

Thank you, what I did was add a variable called power, which by default is set to one, then I made the SPEED be multiplied by “power”, then when I collect the powerup, it changes “power” to 2 which changes the player’s speed, then I added a timer so that the player is only faster for a small amount of time

mrspaceblock | 2021-03-23 20:21

Seems good! Glad it worked.

MrEliptik | 2021-03-23 21:38