If statment true but not working (?)
Asked Answered
S

6

0

Noobie Question (maybe). Im very new to godot, and game dev in general.

So, I have this problem. My GDSCRIPT if statment is supposed to be true but its not working. The animation won't play idle after the player has moved, or increased their velocity and back to no velocity. :

extends CharacterBody2D

@export var speed = 200
var mouse_position = null

var engine_on = true

var rotation_speed = PI # or 180 degrees

@onready var animated_sprite_2d = $AnimatedSprite2D

var animation_played = false

func _physics_process(delta):

Reset the player's velocity

velocity = Vector2(0, 0)
mouse_position = get_global_mouse_position()

var r = global_rotation
var angle = mouse_position.angle()
var angle_delta = rotation_speed * delta
angle = lerp_angle(r, angle, 0.1)
angle = clamp(angle, r - angle_delta, r + angle_delta)

if Input.is_action_pressed("forward") && engine_on:
	
	$AnimatedSprite2D.play("flying-loop")
	var direction = (mouse_position - position).normalized()
	velocity = direction * speed
else:
	Vector2(0, 0)
	if velocity == Vector2(0, 0) && not animation_played:
		$AnimatedSprite2D.play("flying_to_idle")
		animation_played = true
		print(engine_on)
		




print("Velocity:", velocity)

move_and_slide()
global_rotation = angle
Szechwan answered 26/12, 2023 at 18:41 Comment(0)
R
0

Szechwan I don’t really understand the logic or how the animations should look like but try setting animation_played to false when you play the flying-loop animation.

Romalda answered 27/12, 2023 at 10:24 Comment(0)
F
0

Szechwan How do you know the condition is true and how do you know the if is "not working"?

Flanker answered 26/12, 2023 at 19:52 Comment(0)
S
0

Flanker I print out the players velocity, and when its 0, 0 it should play the "flying-to-idle" again but continues the "flying" animation. Here is a video that hopefully will give more context:

Szechwan answered 27/12, 2023 at 6:2 Comment(0)
F
0

Szechwan I print out the players velocity, and when its 0, 0 it should play the "flying-to-idle"

According to your code that's not the only condition. animation_played must be false as well.

Flanker answered 27/12, 2023 at 9:18 Comment(0)
S
0

Flanker The animation_played is to make it not repeat the animation when the if statment is true, is there any way to fix it?

Szechwan answered 27/12, 2023 at 10:1 Comment(0)
R
0

Szechwan I don’t really understand the logic or how the animations should look like but try setting animation_played to false when you play the flying-loop animation.

Romalda answered 27/12, 2023 at 10:24 Comment(0)
S
0

Romalda It works now as intended! Sorry for the messy code.

Szechwan answered 27/12, 2023 at 10:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.