将GameObject旋转到Line

问题描述 投票:1回答:1

目标:给定两个3D矢量,旋转一个框,它在两个矢量Vector3.Lerp(A, B, 0.5f)之间产生,以便它与顶点定义的线对齐。 “盒子”本身基本上是2D-Canvas GameObject(即Sprite),但它是在3D世界中产生的。

另请参阅下图。

Rotation of GameObject towards Line

请记住,这是一个3D问题,因此所有这些都会发生,例如在立方体的表面上。

3D Rotation of GameObject

一个想法是以某种方式获取两个顶点的交叉向量并使用Quaternion.LookRotation()或Quaternion.RotateTowards方法将RectTransform.rotation对齐它,但因为我对所有这些都相当新我'我欣赏任何暗示。

unity3d 3d rotation quaternions
1个回答
1
投票

您想要将Canvas的X轴与(B - A)方向对齐。

在数学上,有无限的方法只能将一个轴与给定方向对齐,因此您需要指定第二个对齐规则(通常是UP向量)。

Quaternion.LookRotation的文档说:

Z轴(旋转物体的)将与正向,X轴(旋转物体)对齐,与正向和向上之间的叉积对齐,Y轴(旋转对象)与Z和X之间的叉积对齐。

从画布的角度来看(当正确定向时),Z轴朝向立方体的中心,所以......

// This is the direction from the Box to the center of the cube
Vector3 boxToCube = Vector3.Cross(B - A, Vector3.up);
// So this should be the correct orientation of the Canvas
Quaternion orientation = Quaternion.LookRotation(boxToCube, Vector3.up);
© www.soinside.com 2019 - 2024. All rights reserved.