如何在 GDScript 中旋转一个移动对象以聚焦在另一个移动对象上?
How do I rotate a moving object to focus on another moving object in GDScript?
我正在尝试旋转绿球(读取玩家)以跟随红球。我已经将相机固定在绿球上,它会跟随绿球的旋转,这正是我想要的。现在我需要计算角度并用 Vector2 做一些事情(球只在 z,x 平面上移动)。我目前正在尝试使用绿球脚本中的以下代码来完成此操作:
var value = 0
var player1
func _ready():
player1 = get_transform()
func _physics_process(delta):
var direction = player2.get_transform().origin - player1.origin
var rotTransform = player1.looking_at(direction, Vector3(0,1,0))
var thisRotation = Quat(Player1_t.basis).slerp(rotTransform.basis,value)
set_transform(Transform(thisRotation, Player1_t.origin))
但这并没有达到我的预期。这也阻止了我的球员能够四处走动,我认为这来自 set_transform 也设置了位置。
有人可以告诉我我做错了什么或提出更好的解决方案吗?
关于我想要的结果的更多上下文的图片:
CLICK
如果你想让Spatial
看一个点,就用look_at
:
Rotates itself so that the local -Z axis points towards the target position.
The transform will first be rotated around the given up vector, and then fully aligned to the target by a further rotation around an axis perpendicular to both the target and up vectors.
Operations take place in global space.
extends Spatial
func _process(delta: float):
var target = get_node("../Target").global_transform.origin
look_at(target, Vector3.UP)
我正在尝试旋转绿球(读取玩家)以跟随红球。我已经将相机固定在绿球上,它会跟随绿球的旋转,这正是我想要的。现在我需要计算角度并用 Vector2 做一些事情(球只在 z,x 平面上移动)。我目前正在尝试使用绿球脚本中的以下代码来完成此操作:
var value = 0
var player1
func _ready():
player1 = get_transform()
func _physics_process(delta):
var direction = player2.get_transform().origin - player1.origin
var rotTransform = player1.looking_at(direction, Vector3(0,1,0))
var thisRotation = Quat(Player1_t.basis).slerp(rotTransform.basis,value)
set_transform(Transform(thisRotation, Player1_t.origin))
但这并没有达到我的预期。这也阻止了我的球员能够四处走动,我认为这来自 set_transform 也设置了位置。
有人可以告诉我我做错了什么或提出更好的解决方案吗?
关于我想要的结果的更多上下文的图片: CLICK
如果你想让Spatial
看一个点,就用look_at
:
Rotates itself so that the local -Z axis points towards the target position.
The transform will first be rotated around the given up vector, and then fully aligned to the target by a further rotation around an axis perpendicular to both the target and up vectors.
Operations take place in global space.
extends Spatial
func _process(delta: float):
var target = get_node("../Target").global_transform.origin
look_at(target, Vector3.UP)