【问题标题】:How to make player to be able to rotate camera around cube in Unity?如何让玩家能够在 Unity 中围绕立方体旋转相机?
【发布时间】:2018-06-16 12:20:00
【问题描述】:

我创建了新的 Unity 项目,在中心添加了一个立方体,现在想让玩家能够通过滑动和/或鼠标拖动来围绕这个立方体旋转相机。

请说出简单的实施步骤或关键字以找到答案或在哪里阅读?

【问题讨论】:

    标签: unity3d user-input


    【解决方案1】:
    public Transform Target;
    
    public float distance = 2.0f;
    public float xSpeed = 20.0f;
    public float ySpeed = 20.0f;
    public float yMinLimit = -90f;
    public float yMaxLimit = 90f;
    public float distanceMin = 10f;
    public float distanceMax = 10f;
    public float smoothTime = 2f;
    float rotationYAxis = 0.0f;
    float rotationXAxis = 0.0f;
    float velocityX = 0.0f;
    float velocityY = 0.0f;
    
    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            velocityX += xSpeed * Input.GetAxis("Mouse X") * distance * 0.02f;
            velocityY += ySpeed * Input.GetAxis("Mouse Y") * 0.02f;
        }
        rotationYAxis += velocityX;
        rotationXAxis -= velocityY;
        rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
    
        Quaternion rotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
    
        Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
        Vector3 position = rotation * negDistance + Target.position;
    
        transform.rotation = rotation;
        transform.position = position;
        velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
        velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
    }
    
    public static float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360F)
            angle += 360F;
        if (angle > 360F)
            angle -= 360F;
        return Mathf.Clamp(angle, min, max);
    }
    

    取自https://answers.unity.com/questions/1257281/how-to-rotate-camera-orbit-around-a-game-object-on.html

    如果您只想围绕特定轴旋转,例如围绕 Y,您基本上可以这样做

    this.transform.RotateAround(Target.transform.position, Vector3.up, Input.GetAxis("Mouse X")*20.0f);
    

    【讨论】:

    • 在哪里写这个?为什么这么复杂?我不能只听拖动事件并相应地旋转我的相机吗?
    • 创建一个新脚本,添加此代码,将其附加到相机。在检查器视图中统一分配多维数据集作为“目标”。您可以在此处消除一些复杂性,例如消除平滑。
    • fromRotation 从未使用过
    【解决方案2】:

    创建一个名为“CameraRotate”的新 C# 脚本,打开它并粘贴此脚本:

        using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class CameraRotate : MonoBehaviour {
    
        public Transform cube;
    
        public float sensitivityX;
        public float sensitivityY;
        GameObject pivot;
    
        void Start () {
            pivot = new GameObject ("pivot");
            pivot.transform.position = cube.position;
            transform.SetParent (pivot.transform);
        }
    
        void Update () {
            pivot.transform.eulerAngles += new Vector3 (Input.GetAxis ("Mouse Y") * -sensitivityX * Time.deltaTime, Input.GetAxis ("Mouse X") * sensitivityY * Time.deltaTime, 0);
        }
    }
    

    将此脚本分配给您的相机,然后在 Inspector 选项卡中,选择相机,将您的 Cube(拖动)分配到“cube”插槽中,并为灵敏度字段分配一个值(我使用了大约 500),按玩和测试。

    这个脚本有什么作用? 它在 Cube 的中心创建一个新的 GameObject 作为旋转的参考,称为枢轴。因此它将相机设置为枢轴的子节点,并根据您的鼠标轴旋转枢轴。

    【讨论】:

      【解决方案3】:

      我是这样写的:

      float mouseX = -Input.GetAxis("Mouse X");
      float mouseY = -Input.GetAxis("Mouse Y");
      
      float magnitude = transform.position.magnitude;
      
      Vector3 mouseSwipe = new Vector3(mouseX, mouseY, 0);
      Vector3 startPoint = new Vector3((float)camera.pixelWidth / 2, (float)camera.pixelHeight / 2, magnitude - 1);
      
      Vector3 startPointWorld = camera.ScreenToViewportPoint(startPoint);
      Vector3 endPointWord = camera.ScreenToViewportPoint(startPoint + mouseSwipe);
      
      Vector3 mouseSwipeWord = endPointWord - startPointWorld;
      
      float dragLat = mouseSwipeWord.y;
      float dragLng = mouseSwipeWord.x;
      
      Vector3 oldPosition = transform.position / magnitude;
      
      float lat = Mathf.Asin(oldPosition.y);
      float rsmall = Mathf.Acos(oldPosition.y);
      
      float lng = Mathf.Atan2(oldPosition.z / rsmall, oldPosition.x / rsmall);
      
      lat += dragLat * 10 * 2 * Mathf.PI;
      if( lat*180/Mathf.PI > 80 )
      {
          lat = 80 * Mathf.PI / 180;
      }
      else if( lat*180/Mathf.PI < -80)
      {
          lat = -80 * Mathf.PI / 180;
      }
      
      lng += dragLng * 10 * 2 * Mathf.PI * 2;
      
      
      
      float y = Mathf.Sin(lat);
      rsmall = Mathf.Cos(lat);
      
      float x = rsmall * Mathf.Cos(lng);
      float z = rsmall * Mathf.Sin(lng);
      
      Vector3 newPosition = new Vector3(x, y, z);
      newPosition *= magnitude;
      
      transform.position = newPosition;
      
      LookAtTarget();
      

      目标是模拟鼠标通过拖动来旋转对象。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-22
        • 1970-01-01
        • 1970-01-01
        • 2017-02-21
        • 1970-01-01
        相关资源
        最近更新 更多