【问题标题】:Rotating an Area2D by 90 degrees after clicking (Godot)单击后将 Area2D 旋转 90 度(Godot)
【发布时间】:2020-03-05 22:35:03
【问题描述】:

Godot 问答论坛不想让我问我的问题,所以我们开始吧^^。

嘿,所以我希望当我按下右箭头按钮时,我的播放器 (Area2D) 会转动 90 度。但是,这不应该直接发生,而是需要一定的时间。它应该精确地保持在 0 90 180 270 360 450 ... 度。通过按下左按钮,它应该移动 -90 度。 我目前有代码:

func _process(delta):
 print($Player.rotation)
 if rotate_to > $Player.get_rotation_degrees():
   $Player.rotate((1 * delta)) ####### $Player.rotate((1 * delta) * speed)
   abc = true
 elif abc == true:
   abc = false
   $Player.set_rotation_degrees(int($Player.get_rotation_degrees())) 
 elif $Player.get_rotation_degrees() >= 360.0:
   $Player.set_rotation_degrees(0)
   rotate_to = 0
 print($Player.get_rotation_degrees())

func _input(event):
  if Input.is_key_pressed(KEY_RIGHT) and not event.is_echo():
    rotate_to += 90

这就是它在完美旋转 90 度时的工作原理。但是一旦我想建立速度,因为它非常慢,所以它会变得更快,并且矩形会破裂。而且它不再是直线。

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 欢迎您使用 Stack Overflow,但我建议您在提问时保持专业,避免使用粗话
  • 对了,这个问题和C#有什么关系?
  • @史蒂夫嘿。您也可以在 godot 中用 c# 编写代码,这样我也可以使用它。
  • 澄清一下,在您的代码中,您似乎希望旋转平滑过渡,即一次旋转 1 度,直到完成完整的 90 度。对吗?

标签: c# rotation sprite godot


【解决方案1】:

如果想要两个值之间的平滑过渡最好使用 Tween 节点。当您将 Tween 节点作为子节点添加到您想要旋转的播放器节点并在播放器节点中使用此代码时。

#get child node with name Tween
onready var tween = get_node("Tween")
func _process(delta):
    #test if action is presed and tween node is not running
    if Input.is_action_just_pressed("ui_right") and not tween.is_active():
        tween.interpolate_property(self,"rotation_degrees",rotation_degrees, rotation_degrees + 90,1,Tween.TRANS_LINEAR,Tween.EASE_IN)
        tween.start()
    if Input.is_action_just_pressed("ui_left") and not tween.is_active():
        tween.interpolate_property(self,"rotation_degrees",rotation_degrees, rotation_degrees - 90,1,Tween.TRANS_LINEAR,Tween.EASE_IN)
        tween.start()

您可以在 Godot 文档https://docs.godotengine.org/en/3.2/classes/class_tween.html中找到更多信息

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-22
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    • 2015-03-22
    • 2022-01-13
    • 2011-09-24
    • 2020-09-04
    相关资源
    最近更新 更多