【问题标题】:C#/Unity Camera rotationC#/Unity 相机旋转
【发布时间】:2016-10-14 07:27:36
【问题描述】:

我试图让相机始终跟随球,但我只能在按住右键时旋转它。虽然,现在,它只有在我按住右键时才会跟随球。有没有办法把两者分开?

using UnityEngine;
using System.Collections;

public class Orbit2 : 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
        && Input.GetMouseButton(1))
    {
        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);
}
}

【问题讨论】:

  • 你可能想修改你的问题:目前你说它应该在“右键单击”时发生,并且在“右键单击”时你想要它
  • 那是因为我确实希望它在我按住右键时发生。所以我不太清楚你这个问题是什么意思......
  • 我只希望在按住它时发生旋转,而不是相机移动,我希望它保持不变。

标签: c# unity3d camera rotation


【解决方案1】:

看起来您希望仅在按住鼠标右键时进行旋转。如果这是真的,则从现在的位置删除Input.GetMouseButton(1),然后将其包裹在transform.rotation = rotation; 周围。它是如此简单。仅供参考,您甚至不需要if (target)。这是不必要的,但我保持原样。

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;

        //Move Input.GetMouseButton(1) here
        if (Input.GetMouseButton(1))
        {
            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);
}

【讨论】:

  • 嗯,它奏效了。有点。当我不使用右键单击时,它现在确实跟随球,但现在的问题是,当我不使用鼠标右键时,屏幕仍然相对于我按住鼠标的位置移动。生病上传视频真的很快我的意思。 youtube.com/watch?v=vaesOfRcR4k
猜你喜欢
  • 1970-01-01
  • 2012-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多