【问题标题】:Unity C# Limit Orbit RotateAround PositionUnity C#限制轨道旋转位置
【发布时间】:2018-01-21 01:08:29
【问题描述】:

这是我到目前为止编写的代码,它用于围绕空间中的一个点运行的相机。

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. 相机的旋转可能会变得很奇怪,我希望它始终指向上方,并且永远不会稍微或完全向右或向左旋转,尤其是倒置。总是向上。

经过大量 Google 搜索后,我尝试了这些链接

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

任何帮助都会很棒,在此先感谢

【问题讨论】:

  • 好吧,在你旋转之前,检查你已经处于什么角度,然后相应地旋转(全帧旋转,只有剩余的角度,甚至根本没有)你遇到的怪异可能是云台锁定。 de.wikipedia.org/wiki/Gimbal_Lock
  • @yes 如何检查我的角度以及全帧旋转以及您所说的其他内容?
  • 全帧旋转是指帧中可能发生的最大旋转。

标签: c# unity3d rotation unity5 quaternions


【解决方案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 为我指明了正确的方向

【讨论】:

  • 很高兴我能帮上忙。不过,一件小事,我会检查 before 我旋转的角度,并且只在必要时旋转。但只要它有效且不会引起问题,它就有效;)
  • @yes 实际上,更有趣的是,虽然这个解决方案确实有效,但实际上我只是最终旋转了相机前面的对象而不是相机本身。老实说,我不知道为什么我没有早点想到这一点,因为它在很大程度上简化了整个事情并允许完全旋转,而不会出现全相机轨道和其他东西的任何问题或并发症。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-02
  • 1970-01-01
  • 1970-01-01
  • 2015-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多