美文网首页
unity 第一人称和第三人称视角的切换

unity 第一人称和第三人称视角的切换

作者: Zi_Fi | 来源:发表于2021-02-17 22:06 被阅读0次

    滑动屏幕旋转摄像机

    image.png
    
    using UnityEngine;
    
    public class CameraSwitch : MonoBehaviour
    {
        private float lastPosX;
        private float lastPosY;
        public Transform CameraX;
        public Transform CameraY;
        private float RotaSpeed ;
        public float Speed = 100;
        private float range = 2;
        
        void Update()
        {
            RotaSpeed = Speed * Time.deltaTime;
    
            if (Input.GetMouseButton(1))
            {
                if (Input.mousePosition.x > lastPosX + range)
                {
                    CameraX.Rotate(Vector3.up, RotaSpeed);
                    lastPosX = Input.mousePosition.x;
    
                }
                else if (Input.mousePosition.x < lastPosX - range)
                {
                    CameraX.Rotate(Vector3.up, -RotaSpeed);
                    lastPosX = Input.mousePosition.x;
                }
    
                if (Input.mousePosition.y > lastPosY + range)
                {
                    CameraY.Rotate(Vector3.right, -RotaSpeed);
                    lastPosY = Input.mousePosition.y;
    
                }
                else if (Input.mousePosition.y < lastPosY - range)
                {
                    CameraY.Rotate(Vector3.right, RotaSpeed);
                    lastPosY = Input.mousePosition.y;
                }
            }
        }
    }
    
    

    第三视角相机跟随

    image.png
    
    using UnityEngine;
    
    public class CameraFollower : MonoBehaviour
    {
        public Transform Target;
        public Transform Offset;
        private float mMoveSpeed=1f;
        private Vector3 offset;
    
        private void Start()
        {
            offset =  Target.position - Offset.position;
        }
    
        void Update()
        {
    
            transform.position = Vector3.Lerp(transform.position, Target.position-offset, Time.deltaTime * mMoveSpeed);
        }
        
    }
    
    

    视角互相切换

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class ViewCtrl : MonoBehaviour
    {
        public Button ViewChangeBtn;
        private bool firstViewState=true;
    
        public Transform FirstCamera;
        public Transform ThirdCamera;
    
        public Transform Offset;
        private void Start()
        {
            ViewChangeBtn.onClick.AddListener(ChangeView);
    
            FirstCamera.gameObject.SetActive(firstViewState);
            ThirdCamera.gameObject.SetActive(!firstViewState);
        }
    
        private void ChangeView()
        {
            firstViewState = !firstViewState;
            FirstCamera.gameObject.SetActive(firstViewState);
            if(!firstViewState)
            {
                ThirdCamera.position = Offset.position;
            }
            ThirdCamera.gameObject.SetActive(!firstViewState);
        }
    }
    

    相关文章

      网友评论

          本文标题:unity 第一人称和第三人称视角的切换

          本文链接:https://www.haomeiwen.com/subject/nquwxltx.html