IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> Unity相机自由观察场景模型方法总结 -> 正文阅读

[游戏开发]Unity相机自由观察场景模型方法总结

一:方法一

适用场景:三维场景中,需要自由、全方位观察模型
描述:通过WASD控制相机的移动,鼠标控制相机的旋转,相机正视的方向即为正前方,按W前进,S后退,A左边平移,D右边平移。滚轮控制视野的放大缩小。
相机需要加一个碰撞器,加碰撞器是为了限制相机在一个范围内移动,然后还需要把相机的可移动范围也用碰撞器包围起来,需要注意的是,不要用一个整的大的碰撞器,这样的话可能会把相机给弹飞,最好还是每一个面都加一个碰撞体,还有就是相机最好加球形碰撞体。
相机使用的事透视模式。
然后将下面脚本挂载在相机上:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MouseLook : MonoBehaviour
{

    //public Transform character;
    public Transform _Camera;

    public float XSensitivity = 1.5f;
    public float YSensitivity = 1.5f;
    public bool clampVerticalRotation = false;
    public float MinimumX = -45F;
    public float MaximumX = 45F;
    public bool smooth = true;
    public float smoothTime = 8f;


    private Quaternion m_CharacterTargetRot;
    private Quaternion m_CameraTargetRot;

    private bool isMove = true;

    private void Start()
    {
        Init();
    }

    private void Update()
    {
        //if (Input.GetMouseButton(1))
        //{
        //    LookRotation(_Camera);
        //}
        LookRotation(_Camera);

        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        Vector3 dir = transform.forward * v + transform.right * h;
        transform.GetComponent<Rigidbody>().velocity = dir * 20;

        if (Input.GetAxis("Mouse ScrollWheel") != 0)
        {
            //限制size大小
            transform.GetComponent<Camera>().fieldOfView = Mathf.Clamp(transform.GetComponent<Camera>().fieldOfView, 10, 75);
            //滚轮改变
            transform.GetComponent<Camera>().fieldOfView =
            transform.GetComponent<Camera>().fieldOfView - Input.GetAxis
            ("Mouse ScrollWheel") * 20;
        }
    }

    public void Init()
    {
        //m_CharacterTargetRot = character.localRotation;
        m_CameraTargetRot = _Camera.localRotation;
    }


    public void LookRotation(Transform _camera)
    {
        float yRot = Input.GetAxis("Mouse X") * XSensitivity;
        float xRot = Input.GetAxis("Mouse Y") * YSensitivity;


        m_CameraTargetRot = _camera.localRotation * Quaternion.Euler(-xRot, yRot, 0f);

        if (clampVerticalRotation)
            m_CameraTargetRot = ClampRotationAroundXAxis(m_CameraTargetRot);

        if (smooth)
        {
            _camera.localRotation = Quaternion.Slerp(_camera.localRotation, m_CameraTargetRot,
                smoothTime * Time.deltaTime);

        }
        else
        {

            _camera.localRotation = m_CameraTargetRot;
        }
        _camera.localRotation = Quaternion.Euler(new Vector3(_camera.eulerAngles.x, _camera.eulerAngles.y, 0f));

    }


    Quaternion ClampRotationAroundXAxis(Quaternion q)
    {
        q.x /= q.w;
        q.y /= q.w;
        q.z /= q.w;
        q.w = 1.0f;

        float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan(q.x);

        angleX = Mathf.Clamp(angleX, MinimumX, MaximumX);

        q.x = Mathf.Tan(0.5f * Mathf.Deg2Rad * angleX);

        return q;
    }

}

具体组件、参数设置如下图所示:
在这里插入图片描述

二:方法二

适用场景:二维场景中,需要自由、全方位观察模型
描述:通过WASD控制相机的移动,相机正视的方向即为正前方,按W向上移动,S向下移动,A左边平移,D右边平移。滚轮控制视野的放大缩小。
通过相机的位置坐标来控制相机的移动范围
相机使用的是正交模式。
然后将下面脚本挂载在相机上:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//巡游模式摄像机控制
public class CameraController : MonoBehaviour
{


    private Vector3 dirVector3;
    private Vector3 rotaVector3;
    [SerializeField]
    private float paramater = 1f;

    private float dis = 1;
    //相机缩放范围
    public int MinOrthographicSize=10;
    public int MaxOrthographicSize = 120;

    //相机移动的范围
    public Vector2 MoveX=new Vector2(-215f, 85f);
    public Vector2 MoveY = new Vector2(-75f, 92f);
    void Awake()
    {

    }

    private void Start()
    {
      
    }
    private void Update()
    {
        //移动
        dirVector3 = Vector3.zero;

        if (Input.GetKey(KeyCode.W)||Input.GetKey(KeyCode.UpArrow))
        {
            dirVector3.y = 1;
        }
        if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
        {
            dirVector3.y = -1;
        }
        if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
        {
            dirVector3.x = -1;
        }
        if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
        {
            dirVector3.x = 1;
        }

        if (Input.GetAxis("Mouse ScrollWheel") != 0)
        {
            //限制size大小
            transform.GetComponent<Camera>().orthographicSize = Mathf.Clamp(transform.GetComponent<Camera>().orthographicSize, MinOrthographicSize, MaxOrthographicSize);
            //滚轮改变
            transform.GetComponent<Camera>().orthographicSize =
            transform.GetComponent<Camera>().orthographicSize - Input.GetAxis
            ("Mouse ScrollWheel") * 20;
        }         

        //在不同的缩放下  相对的降低相机的移动速度 
        if (80< transform.GetComponent<Camera>().orthographicSize&&transform.GetComponent<Camera>().orthographicSize<100)
        {           
            paramater = 0.8f;        
        }
        if (50 < transform.GetComponent<Camera>().orthographicSize && transform.GetComponent<Camera>().orthographicSize < 80)
        {
            paramater = 0.5f;
        }
        if (30 < transform.GetComponent<Camera>().orthographicSize && transform.GetComponent<Camera>().orthographicSize < 50)
        {
            paramater = 0.25f;
        }
        if (10 < transform.GetComponent<Camera>().orthographicSize && transform.GetComponent<Camera>().orthographicSize < 30)
        {
            paramater = 0.15f;
        }
        if ( transform.GetComponent<Camera>().orthographicSize < 10)
        {
            paramater = 0.05f;
        }
        transform.Translate(dirVector3 * paramater, Space.Self);
        //限制摄像机范围
       
        float TempX = Mathf.Clamp(transform.position.x, MoveX.x, MoveX.y);
        float TempY = Mathf.Clamp(transform.position.y, MoveY.x,MoveY.y);
       
        transform.position = new Vector3(TempX, TempY, 30);
    }
    // Update is called once per frame
    void FixedUpdate()
    {
       
    }
}

具体组件、参数设置如下图所示:
在这里插入图片描述

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2021-09-02 11:43:50  更:2021-09-02 11:44:19 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年9日历 -2024/9/21 11:27:57-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码