admin管理员组文章数量:1402842
I have a problem where my if condition work but end her function in just a moment.
var speed = 75
var dir: Vector2 = Vector2.ZERO
var enter: bool = false
@onready var camera = $"../camera"
func _ready():
randomize()
_choose_random_direction()
func _physics_process(delta: float) -> void:
velocity = dir * speed
if Input.is_action_just_pressed("accept"):
camera.unzoomi(delta)
move_and_slide()
func _choose_random_direction():
var directions = [
Vector2(-1, 0), Vector2(1, 0), # Left, Right
Vector2(0, -1), Vector2(0, 1), # Up, Down
Vector2(-1, -1), Vector2(1, -1), # Top-left, Top-right
Vector2(-1, 1), Vector2(1, 1), # Bottom-left, Bottom-right
Vector2(0, 0)
]
dir = directions[randi() % directions.size()]
await get_tree().create_timer(randf_range(1, 3)).timeout
_choose_random_direction()
The if inside the func _physics_process supose to be a zoom of the camera with a smooth transition, but when i call the function of the zoom, the zooming end in a instance instead that 2 sec that i programated.
this is the code of the camera:
var zooming: bool = false
@onready var player = $"../player"
func _process(delta: float) -> void:
position = player.position
func zoomi(delta):
var end: float = 3
zooming = true
if zooming == true:
zoom.x = lerp(zoom.x, end, 0.4 * delta)
zoom.y = lerp(zoom.y, end, 0.4 * delta)
if zoom.x > end * .8:
zooming = false
func unzoomi(delta):
var end: float = 3
zooming = true
if zooming == true:
zoom.x = lerp(zoom.x, end, 0.4 * delta)
zoom.y = lerp(zoom.y, end, 0.4 * delta)
if zoom.x > end * .8:
zooming = false
thanks.
I have a problem where my if condition work but end her function in just a moment.
var speed = 75
var dir: Vector2 = Vector2.ZERO
var enter: bool = false
@onready var camera = $"../camera"
func _ready():
randomize()
_choose_random_direction()
func _physics_process(delta: float) -> void:
velocity = dir * speed
if Input.is_action_just_pressed("accept"):
camera.unzoomi(delta)
move_and_slide()
func _choose_random_direction():
var directions = [
Vector2(-1, 0), Vector2(1, 0), # Left, Right
Vector2(0, -1), Vector2(0, 1), # Up, Down
Vector2(-1, -1), Vector2(1, -1), # Top-left, Top-right
Vector2(-1, 1), Vector2(1, 1), # Bottom-left, Bottom-right
Vector2(0, 0)
]
dir = directions[randi() % directions.size()]
await get_tree().create_timer(randf_range(1, 3)).timeout
_choose_random_direction()
The if inside the func _physics_process supose to be a zoom of the camera with a smooth transition, but when i call the function of the zoom, the zooming end in a instance instead that 2 sec that i programated.
this is the code of the camera:
var zooming: bool = false
@onready var player = $"../player"
func _process(delta: float) -> void:
position = player.position
func zoomi(delta):
var end: float = 3
zooming = true
if zooming == true:
zoom.x = lerp(zoom.x, end, 0.4 * delta)
zoom.y = lerp(zoom.y, end, 0.4 * delta)
if zoom.x > end * .8:
zooming = false
func unzoomi(delta):
var end: float = 3
zooming = true
if zooming == true:
zoom.x = lerp(zoom.x, end, 0.4 * delta)
zoom.y = lerp(zoom.y, end, 0.4 * delta)
if zoom.x > end * .8:
zooming = false
thanks.
Share Improve this question asked Mar 23 at 7:50 fabricio408 moralesfabricio408 morales 51 bronze badge1 Answer
Reset to default 1Your issue is that the zooming transition in your zoomi()
and unzoomi()
functions happens only once when the function is called. Since lerp()
requires multiple frames to create a smooth transition, you need to update the zoom continuously inside _process()
.
this is the code of the camera:
var zooming: bool = false
var zoom_target: float = 1.0
var zoom_speed: float = 0.4
@onready var player: CharacterBody2D = $"../player"
func _process(delta: float) -> void:
position = player.position
if zooming:
zoom.x = lerp(zoom.x, zoom_target, zoom_speed * delta)
zoom.y = lerp(zoom.y, zoom_target, zoom_speed * delta)
if abs(zoom.x - zoom_target) < 0.05:
zooming = false
func zoomi():
zoom_target = 3.0
zooming = true
func unzoomi():
zoom_target = 1.0
zooming = true
this is the code of the player:
var speed = 75
var dir: Vector2 = Vector2.ZERO
var enter: bool = false
@onready var camera: Camera2D = $"../camera"
func _ready():
randomize()
_choose_random_direction()
func _physics_process(delta: float) -> void:
velocity = dir * speed
if Input.is_action_just_pressed("zoom_in"):
camera.zoomi()
elif Input.is_action_just_pressed("zoom_out"):
camera.unzoomi()
move_and_slide()
func _choose_random_direction():
var directions = [
Vector2(-1, 0), Vector2(1, 0), # Left, Right
Vector2(0, -1), Vector2(0, 1), # Up, Down
Vector2(-1, -1), Vector2(1, -1), # Top-left, Top-right
Vector2(-1, 1), Vector2(1, 1), # Bottom-left, Bottom-right
Vector2(0, 0)
]
dir = directions[randi() % directions.size()]
await get_tree().create_timer(randf_range(1, 3)).timeout
_choose_random_direction()
本文标签: if statementGodot 4 Why my if condition end without doing all her processStack Overflow
版权声明:本文标题:if statement - Godot 4. Why my if condition end without doing all her process - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744293759a2599240.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论