【问题标题】:C# with Unity 3D: How do I make a camera move around an object when user moves mouse带有 Unity 3D 的 C#:当用户移动鼠标时,如何使相机围绕对象移动
【发布时间】:2015-12-06 12:48:11
【问题描述】:

我正在尝试在 Unity 4 中进行 3D 查看模拟,用户可以在其中选择一个对象并移动鼠标以围绕它旋转(360 度)我拍摄了很多照片以尝试让它工作,但我都失败了时间,任何帮助将不胜感激,如果它是用 C# 编写的,那就太好了! (但这不是必须的) 提前致谢!

【问题讨论】:

  • 我听不懂你在说什么,//用户可以选择一个对象并移动他们的鼠标来围绕它旋转 // part.你能进一步解释一下吗?

标签: c# object unity3d camera mouse


【解决方案1】:

这是一种不同而有趣的方式:)(我使用它)

(这里,立方体是目标)

1) 创建球体 - 名称:“相机轨道” - 添加材质:透明 (Alpha = 0) - 任意缩放 - 旋转:(0,0,0.1f)
2) 将相机作为“孩子”添加到 Camera Orbit 的表面。 Position = (0,"y = 相机轨道比例",0) 旋转 = (90,0,0)
3) 创建空游戏对象 - 名称:输入控件。

InputControl.cs:

public class InputControl : MonoBehaviour
{
   public GameObject cameraOrbit;

   public float rotateSpeed = 8f;

   void Update()
   {
       if (Input.GetMouseButton(0))
       {
           float h = rotateSpeed * Input.GetAxis("Mouse X");
           float v = rotateSpeed * Input.GetAxis("Mouse Y");

           if (cameraOrbit.transform.eulerAngles.z + v <= 0.1f || cameraOrbit.transform.eulerAngles.z + v >= 179.9f)
                v = 0;

           cameraOrbit.transform.eulerAngles = new Vector3(cameraOrbit.transform.eulerAngles.x, cameraOrbit.transform.eulerAngles.y + h, cameraOrbit.transform.eulerAngles.z + v);
       }

       float scrollFactor = Input.GetAxis("Mouse ScrollWheel");

       if (scrollFactor != 0)
       {
           cameraOrbit.transform.localScale = cameraOrbit.transform.localScale * (1f - scrollFactor);
       }

   }
}

CameraController.cs:

public class CameraController : MonoBehaviour
{
   public Transform cameraOrbit;
   public Transform target;

   void Start()
   {
       cameraOrbit.position = target.position;
   }

   void Update()
   {
       transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y, 0);

       transform.LookAt(target.position);
   }
}

4) 将 CameraController.cs 添加到 Camera.
5) 将 InputControl.cs 添加到输入控件。
6) 在脚本中设置公共变量。 (“相机轨道”和“目标”)

就是这样。鼠标单击并拖动:旋转 - 鼠标滚轮:放大缩小。

ps。如果需要,您可以将目标更改为运行时。

【讨论】:

    【解决方案2】:

    MouseOrbit 脚本会这样做:

    http://wiki.unity3d.com/index.php?title=MouseOrbitImproved#Code_C.23

    只需将此脚本附加到您的相机对象中,然后在检查器中链接目标对象。

    【讨论】:

      【解决方案3】:

      这是完美的。我所做的唯一更改是向 Camera Orbit 添加了一个脚本:

      public class FollowPlayer : MonoBehaviour {
      
          public GameObject player;
          private Vector3 playerPos;
      
          // Update is called once per frame
          void Update () {
              if (this.transform.localScale.x <= 1)
              {
                  this.transform.localScale = new Vector3(1, 1, 1);
              }
      
              if (this.transform.localScale.x >= 15)
              {
                  this.transform.localScale = new Vector3(15, 15, 15);
              }
      
              playerPos = player.transform.position;
              this.transform.position = playerPos;
          }
      }
      

      然后将您的“玩家”对象附加到输入控件,输入控件将移动到玩家所做的任何地方,允许您跟踪玩家,以及旋转和鼠标滚轮缩放。花哨的。

      localScale if 语句意味着您只能放大和缩小到目前为止。

      这个脚本现在唯一的问题是,如果你缩小到 15,然后继续尝试缩小,相机会反弹。不过,我确信这很容易解决,只是我还没有投入时间。

      【讨论】:

        【解决方案4】:

        -- 用于鼠标按下并拖动 -- 我这里修改了代码:http://wiki.unity3d.com/index.php?title=MouseOrbitImproved#Code_C.23

        public Transform target;
        public float distance = 5.0f;
        public float xSpeed = 120.0f;
        public float ySpeed = 120.0f;
        
        public float yMinLimit = -20f;
        public float yMaxLimit = 80f;
        
        public float distanceMin = .5f;
        public float distanceMax = 15f;
        
        private Rigidbody rigidbody;
        
        float x = 0.0f;
        float y = 0.0f;
        
        float mouseX = 0f;
        float mouseY = 0f;
        
        // Use this for initialization
        void Start()
        {
            Vector3 angles = transform.eulerAngles;
            x = angles.y;
            y = angles.x;
        
            rigidbody = GetComponent<Rigidbody>();
        
            // Make the rigid body not change rotation
            if (rigidbody != null)
            {
                rigidbody.freezeRotation = true;
            }
        }
        
        void LateUpdate()
        {
            if (target)
            {
                GetMouseButtonDown_XY();
        
                x += mouseX * xSpeed * distance * 0.02f;
                y -= mouseY * ySpeed * 0.02f;
        
                y = ClampAngle(y, yMinLimit, yMaxLimit);
        
                Quaternion rotation = Quaternion.Euler(y, x, 0);
        
                distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax);
        
                RaycastHit hit;
                if (Physics.Linecast(target.position, transform.position, out hit))
                {
                    distance -= hit.distance;
                }
                Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
                Vector3 position = rotation * negDistance + target.position;
        
                transform.rotation = rotation;
                transform.position = position;
            }
        }
        
        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);
        }
        
        Vector3 mousePosPrev;
        void GetMouseButtonDown_XY()
        {
            if (Input.GetMouseButtonDown(0))
            {
                mousePosPrev = Camera.main.ScreenToViewportPoint(Input.mousePosition);
            } 
        
            if (Input.GetMouseButton(0))
            {
                Vector3 newMousePos = Camera.main.ScreenToViewportPoint(Input.mousePosition);
        
                if (newMousePos.x < mousePosPrev.x)
                {
                    mouseX = -1;
                } else if (newMousePos.x > mousePosPrev.x)
                {
                    mouseX = 1;
                } else
                {
                    mouseX = -0;
                }
        
                if (newMousePos.y < mousePosPrev.y)
                {
                    mouseY = -1;
                }
                else if (newMousePos.y > mousePosPrev.y)
                {
                    mouseY = 1;
                }
                else
                {
                    mouseY = -0;
                }
        
                mousePosPrev = Camera.main.ScreenToViewportPoint(Input.mousePosition);
            }
        }
        

        【讨论】:

          【解决方案5】:

          您根本不需要CameraController,只需将相机的z旋转设置为-90即可。

          【讨论】:

          • (这篇文章似乎没有为问题提供quality answer。请编辑您的答案并改进它,或者将其作为对问题的评论发布。)
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多