unity3d鼠标旋转视角 自由视角代码
·
//把下面的代码挂在摄像机中
//摄像机跟随的主角物体
public Transform target;
//摄像机距离模型的默认距离
public float distance = 20.0f;
//鼠标在x轴和y轴方向移动的速度
float x;
float y;
//鼠标在x和y轴方向移动的速度
float xSpeed = 250.0f;
float ySpeed = 120.0f;
void Start()
{
if (gameObject.GetComponent<Rigidbody>() != null)
{
gameObject.GetComponent<Rigidbody>().freezeRotation = true;
}
}
void LateUpdate()
{
if (target)
{
//我的分析:1.根据垂直方向的增减量修改摄像机距离参照物的距离
distance += Input.GetAxis("Vertical");
//根据鼠标移动修改摄像机的角度
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
Quaternion rotation = Quaternion.Euler(y, x, 0);
Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position;
//设置摄像机的位置与旋转
transform.rotation = rotation;
transform.position = position;
}
}
void Update()
{
}
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)