What the heck is wrong with my code?
Asked Answered
E

6

0

So, the movement code in my game broke. It's only the player's movement that has broke and all other things like enemies have normal movement

extends CharacterBody2D

@onready var _animated_sprite = $AnimatedSprite2D

#CONSTs
const MAX_SPEED = 300
const ACCEL = 1000
const friction = 900

#HP and firing vars
var noHP = true
var hp = 10 #starting hp
var input = Vector2.ZERO
var charge = 0 # charge of bullet.
var _canFire = true # if off, can't shoot

#UI Vars
var noCharge = true
var ChargeBar = load("res://Scenes/UI/charge_bar_v3.tscn")
var chargeBar = ChargeBar.instantiate()

var HPBar = load("res://Scenes/UI/hp_bar_v3.tscn")
var hpBar = HPBar.instantiate()

#Pause Vars
var paused = false
var pause = self.get_parent()
var canPause = true


#Movement Functions
func get_input():
	input.y = int(Input.is_action_pressed("ui_down")) - int(Input.is_action_pressed("ui_up"))
	return input.normalized()


func movement(delta):
	get_input()
	if input == Vector2.ZERO:
		if velocity.length() > (friction* delta):
			velocity -= velocity.normalized() * (friction * delta)
		else:
			velocity = Vector2.ZERO
	else:
		velocity += (input *ACCEL * delta)
		velocity = velocity.limit_length(MAX_SPEED)

func shoot():
	if charge < 10:
		var bullet_uncharged = load("res://Scenes/Bullets/Bullet_Uncharged.tscn")
		var bullet = bullet_uncharged.instantiate()
		bullet.position = get_global_position()
		get_parent().add_child(bullet)
		$BulletTimer.start()
		_canFire = false
	else:
		var bullet_charged = load("res://Scenes/Bullets/bullet_charged.tscn")
		var chargedBullet = bullet_charged.instantiate()
		chargedBullet.position = get_global_position()
		get_parent().add_child(chargedBullet)
		$BulletTimer.start()
		_canFire = false
		
	move_and_slide()

func chargeFunc():
	if noCharge == true:
		get_parent().add_child(chargeBar)
		noCharge = false
	chargeBar.max_value = 10


func hpFunc():
	if noHP == true:
		get_parent().add_child(hpBar)
		noHP = false
	hpBar.value = hp
	hpBar.max_value = 10



func _physics_process(delta):
	chargeFunc()
	hpFunc()
	movement(delta)
	chargeBar.value = charge
	if hp == 0:
		get_tree().change_scene_to_file("res://Scenes/Game_Over.tscn")
	if charge >= 10:
		_animated_sprite.play("Pulse")
		
	if Input.is_action_pressed("attack") && charge < 10:
		charge += 0.25
	if Input.is_action_just_released("attack") && _canFire == true:
		shoot()
		charge = 0
		chargeBar.value = charge


func _on_timer_timeout():
	_canFire = true


func _on_hurtbox_area_entered(_area):
	hp -= 1

func _on_pause_timer_timeout():
	canPause = true
Euglena answered 1/3 at 21:0 Comment(0)
E
0

I can't seem to properly make it display code. oh well

Euglena answered 1/3 at 21:4 Comment(0)
G
0

Euglena I can't seem to properly make it display code. oh well

The code should be placed between ```

``` 
code
``` 
Gelatinous answered 1/3 at 21:12 Comment(0)
E
0

Gelatinous thanks

Euglena answered 1/3 at 21:41 Comment(0)
M
0

Euglena is there a reason that move_and_slide() is only in shoot and not in physics_process?

Mealymouthed answered 2/3 at 0:19 Comment(0)
E
0

Mealymouthed idk why but it seems like it got deleted by accicent. THANK YOU SO MUCH.

Euglena answered 2/3 at 0:25 Comment(0)
M
0

Euglena it happens to the best of us. Syntax errors love to sneak attack.

Mealymouthed answered 2/3 at 0:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.