rotPart乃要旋转的部分
private void Update()
{
var angle = GetRoadAngle();
var nextRot=Quaternion.LookRotation(Vector3.ProjectOnPlane(rotPart.forward, angle), angle);
//LookRotatoin是用来创建一个指定forward和up的旋转角度
rotPart.rotation = Quaternion.Lerp(rotPart.rotation, nextRot, modelRotSpeed);
}
private Vector3 GetRoadAngle()
{
var isHit = false;
RaycastHit hit;
if (Physics.Raycast(rotPart.position + new Vector3(0, 0.25f, 0), Vector3.down, out hit, rayLength))
{
if (hit.transform is not null)
{
if (hit.transform.CompareTag("Ground"))
{
isHit = true;
}
}
}
return isHit ? hit.normal : Vector3.up;
}
Unity3D使物体始终垂直于地面
于 2023-07-24 20:19:44 首次发布