C#和Unity 3D:当用户移动鼠标时,如何使相机在对象周围移动 [英] C# with Unity 3D: How do I make a camera move around an object when user moves mouse

查看:215
本文介绍了C#和Unity 3D:当用户移动鼠标时,如何使相机在对象周围移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Unity 4中进行3D观看模拟,用户可以选择一个对象并移动鼠标使其绕其旋转(360度).时间,任何帮助将不胜感激,如果它是用C#编写的,那就太好了! (但不一定) 预先感谢!

I am trying to make a 3d viewing simulation in Unity 4 where the user can select an object and move their mouse to rotate around it (360 degrees) I have taken many shots to try get it to work, but I fail each time, any help will be appreciated and if it is written in C# that would be great! (But it doesn't have to) Thanks in advance!

推荐答案

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

This is a different and interesting way :) (I use it)

(这里,多维数据集是目标)

(Here, the cube is the target)

1)创建球体-名称:摄像机轨道"-添加材质:透明(Alpha = 0)-根据需要缩放-旋转:(0,0,0.1f)
2)将相机作为子级"添加到相机轨道"的表面.位置=(0,"y =摄像机轨道比例",0) 旋转=(90,0,0)
3)创建一个空的GameObject-名称:输入控件.

1) Create sphere - Name: "Camera Orbit" - Add material: Transparent (Alpha = 0) - As scale as you want - Rotation: (0,0,0.1f)
2) Add the camera as a "child" to Camera Orbit's surface. Position = (0,"y = camera orbit scale",0) Rotation = (90,0,0)
3) Create empty GameObject - Name: Input Control.

InputControl.cs:

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:

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)在脚本中设置公共变量. (摄像机轨道"和目标")

4) Add CameraController.cs to Camera.
5) Add InputControl.cs to Input Control.
6) Set public variables in scripts. ("Camera Orbit" and "Target")

仅此而已.鼠标单击并拖动:旋转-鼠标旋转:放大.

That's all. Mouse click and drag: Rotate - Mouse whell: Zoom in-out.

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

ps. If you want, you can change target as runtime.

这篇关于C#和Unity 3D:当用户移动鼠标时,如何使相机在对象周围移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆