简体   繁体   English

单击后将 Area2D 旋转 90 度(Godot)

[英]Rotating an Area2D by 90 degrees after clicking (Godot)

The Godot forum for Q&A dont want to let me ask my question so here we go ^^.问答的戈多论坛不想让我问我的问题,所以我们开始吧^^。

Hey, so I want that when I press the right arrow button that my player (Area2D) turns 90 degrees.嘿,所以我希望当我按下右箭头按钮时,我的播放器 (Area2D) 会旋转 90 度。 However, this should not happen directly but with a certain time.但是,这不应该直接发生,而是需要一定的时间。 It should remain exactly at 0 90 180 270 360 450 ... degrees.它应该保持在 0 90 180 270 360 450 ... 度。 By pressing the left button it should move -90 degrees.通过按下左按钮,它应该移动 -90 度。 I currently have the code:我目前有代码:

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

This is how it works with the perfect turning of 90 degrees.这就是它如何完美地旋转 90 度。 But as soon as I want to build in the speed so that it turns faster because it is very slow, and the rectangle breaks.但是一旦我想提高速度以使其转得更快,因为它非常慢,并且矩形会破裂。 And it is no longer straight from the lines.它不再是直线。

Can someone help me to fix this?有人可以帮我解决这个问题吗?

If you want smooth transition between two value is best to use Tween node.如果要在两个值之间平滑过渡,最好使用 Tween 节点。 When you add Tween node as child to player node you want rotate and in player node you use this code.当您将 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()

More information you can find in Godot documentation https://docs.godotengine.org/en/3.2/classes/class_tween.html您可以在 Godot 文档https://docs.godotengine.org/en/3.2/classes/class_tween.html 中找到更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM