
测试.png
using UnityEngine;
/// <summary>
/// 脚本挂载到模型即可,模型添加 collider,isTrigger为true
/// </summary>
public class ModelRotation : MonoBehaviour
{
public float mRotationSpeed = 800f;
/// <summary>
/// 如果初始模型Y轴有旋转,需要把模型旋转值赋值到这里
/// </summary>
private float mYRaation = 180;
void Update()
{
if (Input.GetMouseButton(0))
{
// 射线检测
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo))
{
if (hitInfo.collider.isTrigger)
{
//获取鼠标输入
mYRaation += Input.GetAxis("Mouse X") * mRotationSpeed;
//计算旋转
Quaternion mRotation = Quaternion.Euler(transform.rotation.x, -mYRaation, 0);
// 插值旋转
transform.rotation = Quaternion.Lerp(transform.rotation, mRotation, Time.deltaTime * mRotationSpeed);
}
}
}
}
}

3d相机