繁体   English   中英

带有 Unity 3D 的 C#:当用户移动鼠标时,如何让相机围绕对象移动

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

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

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

截屏

(这里,立方体是目标)

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

输入控件.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);
       }

   }
}

相机控制器.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 添加到 Input Control。
6) 在脚本中设置公共变量。 (“相机轨道”和“目标”)

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

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

MouseOrbit 脚本执行此操作:

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

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

太棒了。 我所做的唯一更改是向 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,然后继续尝试缩小,相机会反弹。 我相信这很容易解决,不过,我还没有花时间。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM