第一种
float vertical = Input.GetAxis("Mouse X")
float horizontal = Input.GetAxis("Mouse Y")
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveForce : MonoBehaviour
{
private Rigidbody m_Rigidbody;//变量定义:开辟空间
private int force = 60;//赋值一个力
private float m_MouseSensitive = 3.0f;//控制鼠标敏感程度
// Start is called before the first frame update
void Start()
{
m_Rigidbody = GetComponent<Rigidbody>();//获得组件
}
// Update is called once per frame
void FixedUpdate()//0.02s执行一次,固定添加Fixed
{
// float vertical = Input.GetAxis("Vertical");//虚拟轴(-1.1)
// float horizontal = Input.GetAxis("Horizontal");//水平轴
float vertical = Input.GetAxis("Mouse X");//上下
float horizontal = Input.GetAxis("Mouse Y");//左右
// m_Rigidbody.AddForce(new Vector3(horizontal, 0, vertical) * force);
transform.Rotate(vertical *m_MouseSensitive ,horizontal *m_MouseSensitive ,0);//x.y.z
// var rotation = transform.rotation * Quaternion.AngleAxis(horizontal * m_MouseSensitive, Vector3.up)
// * Quaternion.AngleAxis(vertical *m_MouseSensitive ,Vector3.left );//*乘积 Quaternion四元数旋转以乘积连接 t*p
// transform.rotation = rotation;
}
}
第二种
通过
计算坐标时只需要通过Quaternion.Euler
来取得旋转四元数,以玩家为原点衍生一条(0,0,-1)的向量并乘上四元数以旋转至Player
指向摄像机的方向,最后乘上Distance
,即可得到摄像机相对玩家的偏移。文章来源:https://www.toymoban.com/news/detail-514012.html
var rotation = transform.rotation * Quaternion.AngleAxis(horizontal * m_MouseSensitive, Vector3.up)
* Quaternion.AngleAxis(vertical * m_MouseSensitive, Vector3.left);//*乘积 Quaternion四元数旋转以乘积连接 t*p
transform.rotation = rotation;
最后只要将摄像机放置在那个位置,然后LookAt
旋转到z
轴正方向指向玩家就完事儿了。文章来源地址https://www.toymoban.com/news/detail-514012.html
到了这里,关于unity摄像机跟随鼠标旋转的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!