C#/Unity摄像机旋转

C#/Unity摄像机旋转,c#,unity3d,camera,rotation,C#,Unity3d,Camera,Rotation,我试着使相机始终跟随球,但我只能在按住rightclick键时旋转它。虽然,现在,只有当我按住右键单击时,它才会跟随球。有办法把两者分开吗 using UnityEngine; using System.Collections; public class Orbit2 : MonoBehaviour { public Transform target; public float distance = 5.0f; public float xSpeed = 120.0f; public flo

我试着使相机始终跟随球,但我只能在按住rightclick键时旋转它。虽然,现在,只有当我按住右键单击时,它才会跟随球。有办法把两者分开吗

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);
}
}
使用UnityEngine;
使用系统集合;
公共类轨道2:单一行为{
公共转型目标;
公共浮动距离=5.0f;
公共浮点数xSpeed=120.0f;
公共浮动Y速度=120.0f;
公共浮动yMinLimit=-20f;
公共浮动yMaxLimit=80f;
公共浮动距离最小值=0.5f;
公共浮动距离最大值=15f;
私人刚体;
浮动x=0.0f;
浮动y=0.0f;
//用于初始化
void Start()
{
Vector3角度=transform.eulerAngles;
x=角度y;
y=角度x;
刚体=GetComponent();
//使刚体不改变旋转
if(刚体!=null)
{
刚体旋转=真;
}
}
void LateUpdate()
{
如果(目标)
&&输入。GetMouseButton(1))
{
x+=Input.GetAxis(“鼠标x”)*xSpeed*distance*0.02f;
y-=Input.GetAxis(“鼠标y”)*ySpeed*0.02f;
y=抓斗(y,yMinLimit,yMaxLimit);
四元数旋转=四元数欧拉(y,x,0);
距离=数学夹具(距离-输入.GetAxis(“鼠标滚轮”)*5,距离最小,距离最大);
雷卡斯特击中;
if(物理线投射(目标位置、变换位置、出击))
{
距离-=命中距离;
}
矢量3负距离=新矢量3(0.0f,0.0f,-距离);
矢量3位置=旋转*负距离+目标位置;
transform.rotation=旋转;
变换位置=位置;
}
}
公共静态浮子夹持器(浮子角度、浮子最小值、浮子最大值)
{
如果(角度<-360F)
角度+=360F;
如果(角度>360F)
角度-=360F;
返回数学夹具(角度、最小值、最大值);
}
}

看起来您只希望在按住鼠标右键时旋转。如果这是真的,则从现在的位置移除
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);
}
公共转型目标;
公共浮动距离=5.0f;
公共浮点数xSpeed=120.0f;
公共浮动Y速度=120.0f;
公共浮动yMinLimit=-20f;
公共浮动yMaxLimit=80f;
公共浮动距离最小值=0.5f;
公共浮动距离最大值=15f;
私人刚体;
浮动x=0.0f;
浮动y=0.0f;
//用于初始化
void Start()
{
Vector3角度=transform.eulerAngles;
x=角度y;
y=角度x;
刚体=GetComponent();
//使刚体不改变旋转
if(刚体!=null)
{
刚体旋转=真;
}
}
void LateUpdate()
{
如果(目标)
{
x+=Input.GetAxis(“鼠标x”)*xSpeed*distance*0.02f;
y-=Input.GetAxis(“鼠标y”)*ySpeed*0.02f;
y=抓斗(y,yMinLimit,yMaxLimit);
四元数旋转=四元数欧拉(y,x,0);
距离=数学夹具(距离-输入.GetAxis(“鼠标滚轮”)*5,距离最小,距离最大);
雷卡斯特击中;
if(物理线投射(目标位置、变换位置、出击))
{
距离-=命中距离;
}
矢量3负距离=新矢量3(0.0f,0.0f,-距离);
矢量3位置=旋转*负距离+目标位置;
//将输入移动到此处。GetMouseButton(1)
if(输入。GetMouseButton(1))
{
transform.rotation=旋转;
}
变换位置=位置;
}
}
公共静态浮子夹持器(浮子角度、浮子最小值、浮子最大值)
{
如果(角度<-360F)
角度+=360F;
如果(角度>360F)
角度-=360F;
返回数学夹具(角度、最小值、最大值);
}

看起来您只希望在按住鼠标右键时旋转。如果这是真的,则从现在的位置移除
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);
}
公共转型目标;
公共浮动距离=5.0f;
公共浮点数xSpeed=120.0f;
公共浮动Y速度=120.0f;
公共浮动yMinLimit=-20f;
公共浮动yMaxLimit=80f;
公共浮动距离最小值=0.5f;
公共浮动距离最大值=15f;
私人刚体;
浮动x=0.0f;
浮动y=0.0f;
//用于初始化
void Start()
{
Vector3角度=transform.eulerAngles;
x=角度y;
y=角度x;
刚体=GetComponent();
//使刚体不改变旋转
if(刚体!=null)
{
刚体旋转=真;
}
}
void LateUpdate()
{
如果(目标)
{
x+=Input.GetAxis(“鼠标x”)*xSpeed*distance*0.02f;
y-=Input.GetAxis(“鼠标y”)*ySpeed*0.02f;
y=抓斗(y,yMinLimit,yMaxLimit);
四元数旋转=四元数欧拉(y,x,0);
距离=数学夹具(距离-输入.GetAxis(“鼠标滚轮”)*5,距离最小,距离最大);
雷卡斯特击中;
if(物理线投射(目标位置、变换位置、出击))
{
距离-=命中距离;
}
矢量3负距离=新矢量3(0.0f,0.0f,-距离);
矢量3位置=旋转*负距离+目标位置;
//将输入移动到此处。GetMouseButton(1)
if(输入。GetMouseButton(1))
{
transform.rotation=旋转;
}
变换位置=位置;
}
}
公共静态浮子夹持器(浮子角度、浮子最小值、浮子最大值)
{
如果(角度<-360F)
角度+=360F;
如果(角度>360F)
角度-=360F;
回归数学