2017-02-22 73 views
0

这是它。我有一个人体解剖学,当我触摸任何地方时,我想旋转相机。但看着人体解剖学。我怎么能做到这一点:(当触摸统一时旋转摄像头3d

[https://www.youtube.com/watch?v=EfYeL2FYyyA&t=148s]这正是我真的很想帮帮我吧!

` 

    using UnityEngine; 
    using System.Collections; 
    [AddComponentMenu("Camera-Control/Mouse Orbit with zoom")] 
    public class MouseOrbitImproved : MonoBehaviour { 
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; 
// 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) 
    { 
     x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f; 
     y -= Input.GetAxis("Mouse Y") * 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); 
}} 
+1

请查看[How to ask](http://stackoverflow.com/help/how-to-ask),添加代码格式,并指定您正在尝试解决的具体_programming_问题。例如。 “我试图让这个框架做到这一点,它应该根据这个(链接)文档工作,但它不会”“。 – yeputons

回答

1

你可以使用的,而不是在这种情况下,使用过多的“数学”一招。

会有在每个你想你的相机绕元素的空gameObjects,让我们调用这些“锚”

当选择元素,你只需做锚的相机孩子。

然后如果你只是旋转锚点,你的相机将旋转,因为它是孩子。

这会得到你想要的效果。

Here是一个来自youtube的旋转对象的简单示例。

Here是来自Unity的另一个答案。

希望这会有所帮助!干杯!

+0

谢谢!我会试试看 –