在Unity中寻求旋转方面的帮助,我有一个手持3D指南针,手指向“北”是一个空的变换。我试图锁定手的旋转,这样当指南针倾斜时,手仍然准确地朝向北方。
问题是,当倾斜时手会从指南针中弹出。
以下是我目前拥有的代码和问题的图片。
public Transform target;
public Transform housing;
public float speed = 1.0f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
var lookPos = target.position - transform.position; // assumes this script is attached to the needle
var housingLocal = housing.InverseTransformPoint(lookPos); // change of coordinates from world space to housing space
var localProjection = new Vector3(housingLocal.x, 0, housingLocal.z); // Remove the vertical offset so that we will look along the plane of the housing
var worldProjection = housing.TransformPoint(localProjection); // change of coordinates back into world space
var rotation = Quaternion.LookRotation(worldProjection);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * speed);
}