Unity C# Limit Orbit RotateAround Position
Unity C# Limit Orbit RotateAround Position
所以这是我到目前为止编写的代码,它用于围绕 space 中的一个点旋转的相机。
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);
}
}
到目前为止效果很好,但是有两件事很烦人,我不知道如何解决
- 最大的一个是当相机到达顶部时,如果用户继续向上移动,事情会变得很奇怪。相机开始转动,世界开始旋转等等...我希望相机在接近顶部或底部时停止。
- 相机的旋转可能会变得很奇怪,我希望它始终指向上方,并且永远不会稍微或完全地向右或向左旋转,尤其是上下颠倒。总是向上。
经过大量 Google 搜索后,我尝试了这些链接
- http://wiki.unity3d.com/index.php?title=MouseOrbitImproved
- https://answers.unity.com/questions/363353/how-to-limit-a-transform-movement-in-x-axis.html
- https://answers.unity.com/questions/438836/limit-camera-rotation-with-rotatearound.html
- https://answers.unity.com/questions/1087351/limit-vertical-rotation-of-camera.html
- https://answers.unity.com/questions/1370422/limit-y-axis-transformrotatearound.html
但其中 none 工作正常,或者我无法让它们适应我的需要
任何帮助都将非常有用,在此先感谢
所以我想到目前为止我找到了答案。我也让别人试玩了,他们很喜欢,所以我可能找到了解决方案。
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 为我指明了正确的方向
所以这是我到目前为止编写的代码,它用于围绕 space 中的一个点旋转的相机。
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);
}
}
到目前为止效果很好,但是有两件事很烦人,我不知道如何解决
- 最大的一个是当相机到达顶部时,如果用户继续向上移动,事情会变得很奇怪。相机开始转动,世界开始旋转等等...我希望相机在接近顶部或底部时停止。
- 相机的旋转可能会变得很奇怪,我希望它始终指向上方,并且永远不会稍微或完全地向右或向左旋转,尤其是上下颠倒。总是向上。
经过大量 Google 搜索后,我尝试了这些链接
- http://wiki.unity3d.com/index.php?title=MouseOrbitImproved
- https://answers.unity.com/questions/363353/how-to-limit-a-transform-movement-in-x-axis.html
- https://answers.unity.com/questions/438836/limit-camera-rotation-with-rotatearound.html
- https://answers.unity.com/questions/1087351/limit-vertical-rotation-of-camera.html
- https://answers.unity.com/questions/1370422/limit-y-axis-transformrotatearound.html
但其中 none 工作正常,或者我无法让它们适应我的需要
任何帮助都将非常有用,在此先感谢
所以我想到目前为止我找到了答案。我也让别人试玩了,他们很喜欢,所以我可能找到了解决方案。
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 为我指明了正确的方向