相机视野控制效果实现

原创 2015年11月18日 10:29:21

项目中常用到对相机视野里的对象进行旋转,放大等操作,做笔记,上代码

using UnityEngine;

public class CameraController : MonoBehaviour {

    public Transform target;
    public float xSpeed = 200, ySpeed = 200, mSpeed = 2;//x,y,m缩放的速度
    public float xMinLimit = -60, xMaxLimit = 60,yMinLimit = 0, yMaxLimit = 90;
    public float distance = 15, minDistance = 2, maxDistance = 30;
    public bool needDamping = true;//平滑

    [HideInInspector]
    public bool targerFllowMove = true;
    public float downMoveYDistance;//向下平移距离
    public float upMoveYDistance;//向上平移距离
    public float moveXSpeed = 3;//平移x速度
    public float moveYSpeed = 3;//平移y速度
    public float damping = 5.0f;//平滑值

    private float x = 0.0f;
    private float y = 0.5f;
    private Transform targetTemp;
    private float distanceTemp;
    private Vector3 cameraPositionTemp;
    private Vector3 cameraAngleTemp;
    private float xTemp;
    private float yTemp;
    private float xMove;
    private float yMove;
    private Vector3 currentCameraPosition;
    private Vector3 currentTargetPosition;
    private Vector3 panOffset = Vector3.zero;
    private float IdealDistance;

    public void SetTarget(GameObject go)
    {
        target = go.transform;
    }

    public enum State
    {
        Rota,
        MOve,
        Scale,
        None
    }
    public State state = State.Rota;

    void Start()
    {
        IdealDistance = distance;
        Vector3 angles = transform.eulerAngles;
        x = angles.y;
        y = angles.x;
        targetTemp = target;
        distanceTemp = distance;
        cameraPositionTemp = transform.position;
        cameraAngleTemp = transform.eulerAngles;
        xTemp = x;
        yTemp = y;
    }

    void LateUpdate()
    {
        if (target)
        {
            if (Input.GetMouseButtonDown(1))
            {
                state = State.Rota;
            }
            if (Input.GetMouseButtonDown(0))
            {

            }
            //缩放
            if (Input.GetAxis("Mouse ScrollWheel") != 0)
            {
                state = State.Scale;
            }

            #region 旋转
            if (state == State.Rota || state == State.Scale)
            {
                if (Input.GetMouseButton(1))
                {
                    x += Input.GetAxis("Mouse X") * xSpeed * Time.deltaTime;
                    y -= Input.GetAxis("Mouse Y") * ySpeed * Time.deltaTime;
                }
                x = ClampAngle(x, xMinLimit, xMaxLimit);
                y = ClampAngle(y, yMinLimit, yMaxLimit);

                //离目标越近速度越慢
                //distance -= Input.GetAxis("Mouse ScrollWheel") * mSpeed * distance*Time.deltaTime;
                distance = Mathf.Lerp(distance, IdealDistance, Time.deltaTime * damping);//平滑距离变化
                IdealDistance -= Input.GetAxis("Mouse ScrollWheel") * distance * mSpeed;//滚动时距离变化

                distance = Mathf.Clamp(distance, minDistance, maxDistance);//限制距离
                IdealDistance = Mathf.Clamp(IdealDistance, minDistance, maxDistance);//限制距离
                Quaternion rotation = Quaternion.Euler(y, x, 0.0f);
                Vector3 disVector = new Vector3(0.0f, 0.0f, -distance);
                Vector3 position = rotation * disVector + target.position;
                if (needDamping)
                {
                    transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * damping);
                    transform.position = target.position - distance * transform.forward;
                }
                else
                {
                    transform.rotation = rotation;
                    transform.position = position;
                }
            }

        }
            #endregion

        if (Input.GetMouseButtonDown(2))
        {
            state = State.MOve;
            GetCurrentCameraPosition();
        }

        #region  //平移
        if (Input.GetMouseButton(2))
        {
            xMove -= Input.GetAxis("Mouse X") * moveXSpeed * distance * Time.deltaTime;
            yMove -= Input.GetAxis("Mouse Y") * moveYSpeed * distance * Time.deltaTime;
            yMove = Mathf.Clamp(yMove, -downMoveYDistance, upMoveYDistance);

            Vector3 position1 = transform.TransformDirection(xMove, yMove, 0) + currentCameraPosition;

            transform.position = Vector3.Lerp(transform.position, position1, Time.deltaTime * damping);
            if (targerFllowMove)
            {
                target.rotation = transform.rotation;
                Vector3 position = target.TransformDirection(xMove, yMove, 0) + currentTargetPosition;
                target.position = Vector3.Lerp(target.position, position, Time.deltaTime * damping);
            }
        }
        #endregion

    }
    static float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360)
            angle += 360;
        if (angle > 360)
            angle -= 360;
        return Mathf.Clamp(angle, min, max);
    }
    /// <summary>
    /// 摄像机复位
    /// </summary>
    public void InitCamera()
    {
        transform.position = cameraPositionTemp;
        transform.eulerAngles = cameraAngleTemp;
        x = xTemp;
        y = yTemp;
        target = targetTemp;
        distance = distanceTemp;
    }
    public void ResetCamera()
    {
        state = State.None;
        distance = 1;
    }
    /// <summary>
    /// 获取当前摄像机位置
    /// </summary>
    public void GetCurrentCameraPosition()
    {
        currentCameraPosition = transform.position;
        currentTargetPosition = target.position;
        xMove = 0;
        yMove = 0;
    }

    private Ray ray;
    private RaycastHit hit;

    void FixedUpdate()
    {
        Ray ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out hit, 500))
        {
            if (Input.GetMouseButtonUp(0))
            {
                GameObject other = hit.collider.gameObject;
                if (other)
                {
                    Debug.Log("Camera");
                }
            }
        }
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Web_CSS_通过不断设置广告位置来实现广告停留视野效果;

>html源码 对联广告效果 `                                   ...

Unity相机视野的缩放,旋转及跟随

首先声明,博主使用的是Unity5.3.2(64)游戏开发中,经常会遇到相机视野的拉近,拉远以及旋转,跟随的问题,这里我们只搞一种方式直接上代码:using UnityEngine; using Sy...

Genesis-3D 入门教程—25.相机的切换与视野调整

该文章转载自Genesis-3D官网, 更多教程请访问Genesis-3D官网:http://www.genesis-3d.com.cn 相机的切换与视野调整 正如电...

iOS自定义相机界面,实现微信小视频UI效果

在开发的时候,要实现微信小视频的效果只用系统框架中的picker是不行的,它是个navi 而我们需要一个view把它放到tableview后边 所以我们需要自定义一个摄像界面 下边的文章摘抄自ht...
  • wxs0124
  • wxs0124
  • 2015年03月24日 16:55
  • 1766

相机滤镜效果实现

基于github上面gpuimage库和magiccamera库, 1:改写了OpenGlUtil工具。 2:自定义surfaceview组件,使其可以左右滑动, 关键代码: package com....

图像滤镜艺术---美颜相机之高级柔焦效果实现

给大家分享一下美颜相机中高级柔焦特效的算法实现!

three.js中3D视野的缩放实现

首先,不再废话了,什么是three.js,是干什么的,知道的就是知道,不知道的就百度吧。 昨儿发现three.js中的3D视野的缩小和放大效果可以用照相机的远近焦来实现。    缩小后:  这里...

简单的实现两个物体在摄像机视野内的代码

这个方法是看视频得到的,有些游戏可能会用到,有点记录意义。 首先说一下,这个方法适用于摄像机镜头角度相对于地面来说有了一定倾斜程度的情况下(如图一),如果摄像机镜头接近于垂直地面(如图二), 则不...

three.js中3D视野的缩放实现

首先,不再废话了,什么是three.js,是干什么的,知道的就是知道,不知道的就百度吧。昨儿发现three.js中的3D视野的缩小和放大效果可以用照相机的远近焦来实现。 缩小后: 这里采用的是...
  • U_9_5
  • U_9_5
  • 2016年01月19日 15:32
  • 4675
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:相机视野控制效果实现
举报原因:
原因补充:

(最多只允许输入30个字)