unity相机旋转,缩放与跟随

  

本人工作时所写的相机的旋转,缩放与跟随代码,希望对所帮助



// 相机的旋转

using UnityEngine;

using System.Collections;


public class RotateCamera : MonoBehaviour
{
    public Transform target;


    //距离
    public float distanceX = 0;
    public float distanceY = 0;
    public float distanceZ=-5;
    public float minDistance = -2;
    public float maxDistance =-50;


    //角度
    public float angleX=30f;
    public float angleY=0;
    public float angleZ=0;


    public float angleSpeed = 5f;
    public float scrollSpeed = 5f;
    public float moveSpeed = 5f;


// Use this for initialization
void Start ()
{
        Debug.Log(Quaternion.Euler(0, 90, 0));
  Debug.Log(Quaternion.Euler(0, 90, 0)*new Vector3(0, 0, -10));
}

// Update is called once per frame
void Update ()
{
        Mouse();
        UpdateCamera();
}


    private void Mouse()
    {
        if (Input.GetMouseButton(1))
        {
            angleY += Input.GetAxis("Mouse X")*angleSpeed;
            angleX -= Input.GetAxis("Mouse Y")*angleSpeed;
        }
        if (Input.GetAxis("Mouse ScrollWheel") != 0)
        {
            //根据鼠标滚轮的值计算出这一帧应该移动的距离。
            distanceZ += Mathf.Lerp(0, Input.GetAxis("Mouse ScrollWheel")*scrollSpeed, Time.deltaTime);
            distanceZ = Mathf.Clamp(distanceZ,maxDistance, minDistance);
        }
        if (Input.GetMouseButton(2))
        {
            //根据鼠标滑动的值计算出这一帧一个移动的距离
            distanceX -= Mathf.Lerp(0, Input.GetAxis("Mouse X") * moveSpeed, Time.deltaTime);
            distanceY -= Mathf.Lerp(0, Input.GetAxis("Mouse Y") * moveSpeed, Time.deltaTime);
        }
    }


    private void UpdateCamera()
    {
        Quaternion rotation=Quaternion.Euler(angleX,angleY,angleZ);


        //创建一个位移变量
        Vector3 pos=new Vector3(distanceX,distanceY,distanceZ);


        Vector3 postion = rotation * pos;
        if (target != null)
            postion += target.position;


        transform.position = postion;
        transform.rotation = rotation;
    }

}


// 相机的缩放


using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class CameraScale : MonoBehaviour
{
    void Start()
    {
        int ManualWidth = 400;
        int ManualHeight = 300;
        int manualHeight;
        if (System.Convert.ToSingle(Screen.height) / Screen.width > System.Convert.ToSingle(ManualHeight) / ManualWidth)
            manualHeight = Mathf.RoundToInt(System.Convert.ToSingle(ManualWidth) / Screen.width * Screen.height);
        else
            manualHeight = ManualHeight;
        Camera camera = GetComponent<Camera>();
        float scale = System.Convert.ToSingle(manualHeight / 300f);
        camera.fieldOfView *= scale;
    }
}


// 相机的跟随


using UnityEngine;
using System.Collections;
using Unity;


/// <summary>
/// 摄像机跟随 虚拟体
/// </summary>
public class CameraFollow : MonoBehaviour
{
    public static CameraFollow instrance;
    public GameObject objXuNiTi;
    //摄像机旋转角度
    public Vector3 angles = Vector3.zero;
    public Vector3 anglesSpeed = new Vector3(10, 10, 5);
    public Vector2 anglesDu = new Vector2(-80, 80);
    //摄像机距离
    public float targetHeight = 1, distanceSpeed = 5;
    public Vector3 distance = new Vector3(0, 10, 15);


    private float moveSpeed;
    private Transform objCamera;
    private Transform objPivot;
    private float dist;
    private bool isCorrected;
    //--------------------------------
    /// <summary>
    /// 摄像模式:是否0:1定点/2漫游
    /// </summary>
    public int FollowState = 0;
    public delegate void DelegateMoveEnd();
    public event DelegateMoveEnd OnMoveEnd;

   void Awake()
    {
        instrance = this;
        angles = transform.eulerAngles;
        objCamera = gameObject.GetComponentInChildren<Camera>().transform;
        objPivot = objCamera.parent;
    }
    void LateUpdate()
    {
        if (UICamera.hoveredObject != null && UICamera.hoveredObject.name != "UI Root") return;


        //定点模式
        if (FollowState == 1)
        {
            this.SetCameraRote();//设置摄像机旋转角度
            this.SetCameraDistance();//设置摄像机与物体距离
        }
        else if (FollowState == 2)
        {
            this.SetCameraRote();//设置摄像机旋转角度
            this.CameCubeMove();//摄像机盒子自由移动
            this.SetCameraDistance();//设置摄像机与物体距离
        }
    }




    //---------------------------------------------------------------------------------------------------------------
    /// <summary>
    /// 定点模式下,调整摄像机距离
    /// </summary>
    public void SetCameraDistance(float _dis)
    {
        if (this.FollowState == 1 && _dis > 0)
        {
            this.distance.y = _dis;
        }
    }
    /// <summary>
    /// 定点模式下,调整摄像机角度
    /// </summary>
    public void SetCameraAngle(Vector3 _vet)
    {
        if (this.FollowState != 2 && _vet != Vector3.zero)
        {
            this.angles = _vet;
        }
    }
    /// <summary>
    /// 设置物体移动到虚拟体
    /// </summary>
    /// <param name="_isMove">是否有移动动画</param>
    public void SetCameraXuNiTi(GameObject _xuNiTi, bool _isMove)
    {
        if (_xuNiTi == null)
        {
            Debug.Log("【确实关联】没有给摄像机指定虚拟体对象!");
            return;
        }


        this.objXuNiTi = _xuNiTi;
        if (_isMove == true)
        {
            iTween.MoveTo(this.gameObject, iTween.Hash("position", objXuNiTi.transform.position, "time", 1, "easetype", "linear", "oncomplete", "CameraMoveEnd"));
        }
    }
    private void CameraMoveEnd()
    {
        if (this.OnMoveEnd != null)
            this.OnMoveEnd();
    }

    //---------------------------------------------------------------------------------------------------------------
    /// <summary>
    /// 摄像机盒子自由移动
    /// </summary>
    private void CameCubeMove()
    {
        if (Input.GetMouseButton(2))
        {
            moveSpeed = Vector3.Distance(transform.position, objCamera.transform.position) / 40;
            transform.Translate(objCamera.up * (-Input.GetAxis("Mouse Y")) * moveSpeed, Space.World);
            transform.Translate(Vector3.right * (-Input.GetAxis("Mouse X")) * moveSpeed, Space.Self);
        }
    }
    /// <summary>
    /// 设置摄像机旋转角度
    /// </summary>
    private void SetCameraRote()
    {
        if (Input.GetMouseButton(1))
        {
            angles.x -= anglesSpeed.x * Input.GetAxis("Mouse Y");
            angles.y += anglesSpeed.y * Input.GetAxis("Mouse X");
            angles.x = Mathf.Clamp(angles.x, anglesDu.x, anglesDu.y);
        }
        transform.rotation = Quaternion.Euler(0f, angles.y, 0f);
        this.objPivot.localRotation = Quaternion.Euler(angles.x, 0f, 0f);
    }
    /// <summary>
    /// 设置摄像机与物体距离
    /// </summary>
    private void SetCameraDistance()
    {
        dist = Vector3.Distance(objCamera.position, objPivot.position);
        if (Input.GetAxis("Mouse ScrollWheel") > 0)
            distance.y -= Time.deltaTime * dist * distanceSpeed;
        else if (Input.GetAxis("Mouse ScrollWheel") < 0)
            distance.y += Time.deltaTime * dist * distanceSpeed;
        distance.y = Mathf.Clamp(distance.y, distance.x, distance.z);
        Vector3 position = objPivot.position - (objPivot.rotation * Vector3.forward * distance.y + new Vector3(0, -targetHeight, 0));


        isCorrected = false;
        RaycastHit collisionHit;
        if (Physics.Linecast(objPivot.position, position, out collisionHit, 1 << LayerMask.NameToLayer("Default")))
        {
            position = collisionHit.point;
            isCorrected = true;
        }
        if (isCorrected)
        {
            Debug.DrawRay(objPivot.position, -transform.forward * Camera.main.transform.localPosition.magnitude, Color.red);
        }
        objCamera.position = position + new Vector3(0, 0.1f, 0);
    }

}



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值