unity摄像机控制篇

2019-12-03 15:19:34 浏览数 (1)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/CJB_King/article/details/52091165

关于unity中摄像机控制,我总结了一些,希望自己在今后的学习中不会忘记,耶!!!

unity中第一,三人称控制器上绑定的都有一个叫Mouse Look的脚本,我把它写下来了;

代码语言:javascript复制
[AddComponentMenu("Camera-Control/Mouse Look")]
public enum RotationAxes{MouseXAndY=0,MouseX=1,MouseY=2}
public RotationAxes axes=RotationAxes.MouseXAndY;
public float SensitivityX=15F;
public float SensitivityY=15F;
public float minimumX=-360F;
public float maxmumX=360F;
public float minimumY=-60F;
public float maxmumY=60F;
float rotationY=0F;
void start()
{
    if(GetComponent<Rigidbody>())
    {
        GetComponent<Rigidbody>().freezeRotation=true;
    }
}
void update()
{
    if(axes==RotationAxes.MouseXAndY)
    {
        float rotationX=transform.localEulerAngles.y Input.GetAxis("Mouse X")*SensitivityX;
        rotationY =Input.GetAxis("MouseY")*SensitivityY;
        rotationY=Mathf.Clamp(rotationY,minimumY,maxmumY);
        transform.localEulerAngles=new Vector3(-rotationY,rotationX,0);
    }
    else if(axes==RotationAxes.MouseX)
    {
        transform.Rotate(0,Input.GetAxis("Mouse X")*SensitivityX,0);
    }
    else
    {
        rotationY =Input.GetAxis("Mouse Y")*SensitivityY;
        rotationY=Mathf.Clamp(rotationY,minimumY,maxmumY);
        transform.localEulerAngles=new Vector3(-rotationY,transform.localEulerAngles.y,0);
    }
}

主角面向鼠标点击的方向:

代码语言:javascript复制
  Vector3 m_pos=Input.mousePosition;     //获取鼠标的屏幕坐标;
  Vector3 p_pos=Camera.main.WorldToScreenPoint(transform.position);     //将主角的世界坐标转化为屏幕坐标,为的是获得他的z轴坐标;
  m_pos.z=p_pos.z;                              //将主角的屏幕z轴坐标赋值给鼠标;
  Vector3 wolrd;
  wolrd.x=Camera.main.ScreenToWorldPoint(m_pos).x;
  world.z=Camera.main.ScreenToWorldPoint(m_pos).z;
  world.y=transform.position.y                   //y轴坐标不变,主角才不会转进地底;
  transform.LookAt(world);            //主角看向鼠标点击的位置;

摄像机围绕物体旋转:

代码语言:javascript复制
  x  = Input.GetAxis("Mouse X") * speed * Time.deltaTime;
        y  = Input.GetAxis("Mouse Y") * speed * Time.deltaTime;
        y = Mathf.Clamp(y, 0, 180);   //x,z轴的旋转限制在0到180;
        Quaternion q = Quaternion.Euler(y, x, 0);
        Vector3 direction = q * Vector3.forward;
        this.transform.position = center.position - direction * distance;
        this.transform.LookAt(center.position);
        鼠标控制摄像机:
transform.RotateAround(transform.position, Vector3.up, Input.GetAxis("Mouse X") * 2f);
        transform.RotateAround(transform.position, Vector3.left, Input.GetAxis("Mouse Y") * 2f);

主角面向方向与主摄像机保持一致:

代码语言:javascript复制
   float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");

        Vector3 camForward = Vector3.Scale(camTrans.forward, new Vector3(1, 0, 1)).normalized;
        Vector3 move = camForward * v   camTrans.right * h;
        Vector3 localMove = transform.InverseTransformDirection(move);
        float turnAmount = Mathf.Atan2(localMove.x, localMove.z);		//计算弧度
        float forwardAmount = localMove.z;
        float turnSpeed = Mathf.Lerp(180, 360, forwardAmount);
        transform.Rotate(0, turnAmount * turnSpeed * Time.deltaTime * 2, 0);

滑动鼠标中建,调整视野的远近:

代码语言:javascript复制
 float fov = Camera.main.FieldOfView;       //获得摄像机调整视野的值;
        fov  = Input.GetAxis("Mouse ScrollWheel") * sensitivity;  //滑动中建,调整视野值得大小;
        fov = Mathf.Clamp(fov, minFov, maxFov);       //限制视野值得大小为(minFov,maxFov);
        Camera.main.FieldOfView = fov;             //再将调整好的值赋值给摄像机的视野;

写的不好,勿喷哦!!

本文出自 “51CTO_King” 博客,请务必保留此出处http://cjboking.blog.51cto.com/11020113/1784756

0 人点赞