C# with Unity 3D:当用户移动鼠标时,如何使相机围绕对象移动

C# with Unity 3D: How do I make a camera move around an object when user moves mouse

我正在尝试在 Unity 4 中进行 3d 观看模拟,用户可以在其中 select 一个对象并移动他们的鼠标以围绕它旋转(360 度)我已经拍了很多照片试图让它成为工作,但我每次都失败了,我们将不胜感激,如果它是用 C# 编写的,那就太好了! (但没必要) 提前致谢!

MouseOrbit 脚本可以做到这一点:

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

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

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

(这里以立方体为目标)

1) 创建球体 - 名称:"Camera Orbit" - 添加 material:透明(Alpha = 0) - 根据需要缩放 - 旋转:(0,0,0.1f)
2) 将相机作为 "child" 添加到 Camera Orbit 的表面。位置 = (0,"y = camera orbit scale",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 到相机。
5) 添加 InputControl.cs 到输入控件。
6) 在脚本中设置 public 个变量。 ("Camera Orbit" 和 "Target")

就是这样。鼠标单击并拖动:旋转 - 鼠标轮:缩放 in-out。

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

这太完美了。我所做的唯一更改是向 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;
    }
}

然后将您的 "player" 对象附加到输入控件,输入控件将移动到玩家所在的位置,让您可以跟踪玩家,以及旋转和鼠标滚轮缩放。喜欢。

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

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

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

-- 将其用于鼠标按下并拖动 -- 我修改了这里的代码: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);
    }
}