• 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);
            }
        }
    
    }
    
    
  • 相关阅读:
    leetcode211
    leetcode209
    leetcode201
    leetcode1396
    leetcode1395
    leetcode1394
    leetcode1386
    leetcode1387
    leetcode1382
    leetcode1376
  • 原文地址:https://www.cnblogs.com/ezhar/p/13263522.html
Copyright © 2020-2023  润新知