zoukankan      html  css  js  c++  java
  • Unity实现摄像机以某个物体为中心旋转

    将下方代码赋给摄像机,并指定需要围绕的对象,即可实现摄像机随着该物体为中心以卫星的方式进行旋转了。

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    //摄像机操作   
    //删减版   在实际的使用中可能会有限制的需求  比如最大远离多少  最近距离多少   不能旋转到地面以下等
    public class RotateAround : MonoBehaviour
    {
        public Transform CenObj;//围绕的物体
        private Vector3 Rotion_Transform;
        private new Camera camera;
        void Start()
        {
            camera = GetComponent<Camera>();
            Rotion_Transform = CenObj.position;
        }
        void Update()
        {
            Ctrl_Cam_Move();
            Cam_Ctrl_Rotation();
        }
        //镜头的远离和接近
        public void Ctrl_Cam_Move()
        {
            if (Input.GetAxis("Mouse ScrollWheel") > 0)
            {
                transform.Translate(Vector3.forward * 1f);//速度可调  自行调整
            }
            if (Input.GetAxis("Mouse ScrollWheel") < 0)
            {
                transform.Translate(Vector3.forward * -1f);//速度可调  自行调整
            }
        }
        //摄像机的旋转
        public void Cam_Ctrl_Rotation()
        {
            var mouse_x = Input.GetAxis("Mouse X");//获取鼠标X轴移动
            var mouse_y = -Input.GetAxis("Mouse Y");//获取鼠标Y轴移动
            if (Input.GetKey(KeyCode.Mouse1))
            {
                transform.RotateAround(Rotion_Transform, Vector3.up, mouse_x * 5);
                transform.RotateAround(Rotion_Transform, transform.right, mouse_y * 5);
            }
        }
    
    }
    
    
  • 相关阅读:
    Jzoj4765 Crisis
    Jzoj4764 Brothers
    Jzoj4764 Brothers
    Jzoj4756 幻象
    Jzoj4756 幻象
    Jzoj4755 快速荷叶叶变换
    Jzoj4755 快速荷叶叶变换
    力扣算法题—059螺旋矩阵
    力扣算法题—058最后一个单词长度
    力扣算法题—057插入区间
  • 原文地址:https://www.cnblogs.com/ezhar/p/13263522.html
Copyright © 2011-2022 走看看