Unity C#Limit Orbit RotateAround Position

时间:2018-01-21 01:08:29

标签: c# unity3d rotation unity5 quaternions

所以这是我到目前为止制作的代码,它用于围绕太空中的一个轨道运行的摄像机。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Lean.Touch;

public class CameraOrbit : TopClass
{
    [Tooltip("Ignore fingers with StartedOverGui?")]
    public bool ignoreGuiFingers = true;

    [Tooltip("Ignore fingers if the finger count doesn't match? (0 = any)")]
    public int requiredFingerCount = 1;

    [Tooltip("The sensitivity of the movement, use -1 to invert")]
    public float sensitivity = 0.25f;

    public Vector3 target = Vector3.zero;

    protected void LateUpdate()
    {
        // Get the fingers we want to use
        List<LeanFinger> fingers = LeanTouch.GetFingers(ignoreGuiFingers, requiredFingerCount);

        // Get the scaled delta of all the fingers
        Vector2 delta = LeanGesture.GetScaledDelta(fingers);

        transform.RotateAround(target, Vector3.up, delta.x * sensitivity);
        transform.RotateAround(target, Vector3.right, delta.y * sensitivity);

        transform.LookAt(target);
    }
}

到目前为止,这种方法很有效,但有两件事令人讨厌,而我却不知道如何修复

  1. 最大的一个是当相机到达顶部时,如果用户继续向上移动,事情就会变得怪异。相机开始变形,世界旋转等...我希望相机在接近顶部或底部时停止。
  2. 相机的旋转可能会变得怪异,我希望它始终指向上方并且从不向右或向左轻微或完全旋转,尤其是颠倒。总是指着。
  3. 经过大量Google搜索,我尝试过这些链接

    但它们都没有正常工作,或者我无法让它们适应我的需要

    任何帮助都会很棒,提前谢谢

1 个答案:

答案 0 :(得分:1)

我认为到目前为止我找到了答案。我也有人测试它,他们喜欢它,所以我可能找到了解决方案。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Lean.Touch;

public class CameraOrbit : TopClass
{
    [Tooltip("Ignore fingers with StartedOverGui?")]
    public bool ignoreGuiFingers = true;

    [Tooltip("Ignore fingers if the finger count doesn't match? (0 = any)")]
    public int requiredFingerCount = 0;

    [Tooltip("The sensitivity of the movement, use -1 to invert")]
    public float sensitivity = 0.25f;

    public float min = 45f;
    public float max = 315f;

    public Vector3 target = Vector3.zero; //this is the center of the scene, you can use any point here

    protected void LateUpdate()
    {
        // Get the fingers we want to use
        List<LeanFinger> fingers = LeanTouch.GetFingers(ignoreGuiFingers, requiredFingerCount);

        // Get the world delta of all the fingers
        Vector2 delta = LeanGesture.GetScaledDelta(fingers);

        transform.RotateAround(target, Vector3.up, delta.x * sensitivity);
        transform.RotateAround(target, Vector3.right, delta.y * sensitivity);

        Vector3 angles = transform.eulerAngles;
        angles.x = Mathf.Clamp(angles.x, min, max);
        angles.y = Mathf.Clamp(angles.y, min, max);
        angles.z = 0;
        transform.eulerAngles = angles;

        transform.LookAt(target);
    }
}

如果我希望相机始终具有向上的点,在我的情况下,它保持z轴为0. x和y轴只需要保持检查。

如果它对任何人有帮助,我从上面修改的唯一内容就是

...
public float min = 45f;
public float max = 315f;
...
Vector3 angles = transform.eulerAngles;
angles.x = Mathf.Clamp(angles.x, min, max);
angles.y = Mathf.Clamp(angles.y, min, max);
angles.z = 0;
transform.eulerAngles = angles;

感谢@yes指出我正确的方向