C# 在我的相机脚本中平移不起作用

C# 在我的相机脚本中平移不起作用,c#,input,camera,unity5,C#,Input,Camera,Unity5,我有一个相机脚本,我正在工作,我只是不能让相机泛工作。它能识别输入,但根本不移动摄像机 它曾在某一点工作,但在缩放或旋转时重置了相机,现在缩放和旋转工作正常。此问题是由于底部的功能PositionRotation造成的 using UnityEngine; using UnityEngine.EventSystems; using System.Collections; [AddComponentMenu("Camera-Control/Mouse drag Orbit with zoom")]

我有一个相机脚本,我正在工作,我只是不能让相机泛工作。它能识别输入,但根本不移动摄像机

它曾在某一点工作,但在缩放或旋转时重置了相机,现在缩放和旋转工作正常。此问题是由于底部的功能
PositionRotation
造成的

using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
[AddComponentMenu("Camera-Control/Mouse drag Orbit with zoom")]
public class DragMouseOrbit : 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;
public float smoothTime = 2f;
public float zoomFactor = 5f;
public float panFactor = 10f;

private Transform m_Transform;
float rotationYAxis = 0.0f;
float rotationXAxis = 0.0f;
float velocityX = 0.0f;
float velocityY = 0.0f;
// Use this for initialization
void Start()
{
    Vector3 angles = transform.eulerAngles;
    rotationYAxis = angles.y;
    rotationXAxis = angles.x;
    // Make the rigid body not change rotation
    if (GetComponent<Rigidbody>())
    {
        GetComponent<Rigidbody>().freezeRotation = true;
    }
}
void Update()
{
if (!EventSystem.current.IsPointerOverGameObject())
    {
    if(Input.GetMouseButton(0))
        {
            CameraRotate();
        }
    if(Input.GetMouseButton(1))
        {
            CameraPan();
        }

    if(Input.GetAxis("Mouse ScrollWheel") <= 0)
        {
            CameraZoom();
        }

    if (Input.GetAxis("Mouse ScrollWheel") >= 0)
    {
        CameraZoom();
    }
    }
}


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);
}

public void CameraRotate()
{
    if (target)


            {
                velocityX += xSpeed * Input.GetAxis("Mouse X") * distance * 0.02f;
                velocityY += ySpeed * Input.GetAxis("Mouse Y") * 0.02f;
            }
            rotationYAxis += velocityX;
            rotationXAxis -= velocityY;
            rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
            PositionRotation();
}


public void CameraPan()
{
    if (!EventSystem.current.IsPointerOverGameObject())   
        {
        Debug.Log("right click has been pressed dumbfuck");            
            //transform.rotation = transform.rotation;
            transform.Translate(Vector3.right * -Input.GetAxis("Mouse X") * panFactor);
            transform.Translate(Vector3.forward * -Input.GetAxis("Mouse Y") * panFactor);
        }        
}


public void CameraZoom()
{
    if (!EventSystem.current.IsPointerOverGameObject())
    {
        //transform.Translate(Vector3.forward * +Input.GetAxis("Mouse ScrollWheel") * zoomFactor);

        PositionRotation();

        distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * zoomFactor, distanceMin, distanceMax);
        RaycastHit hit;
        if (Physics.Linecast(target.position, transform.position, out hit))
        {
            distance -= hit.distance;
        }

    }
}

void PositionRotation()
{
    Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
    Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
    Quaternion rotation = toRotation;

    Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
    Vector3 position = rotation * negDistance + target.position;

    transform.rotation = rotation;
    transform.position = position;
    velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
    velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
}
}
使用UnityEngine;
使用UnityEngine.EventSystems;
使用系统集合;
[AddComponentMenu(“摄影机控制/带缩放的鼠标拖动动态观察”)]
公共类DragMouseOrbit:单行为
{
公共转型目标;
公共浮动距离=5.0f;
公共浮点数xSpeed=120.0f;
公共浮动Y速度=120.0f;
公共浮动yMinLimit=-20f;
公共浮动yMaxLimit=80f;
公共浮动距离最小值=0.5f;
公共浮动距离最大值=15f;
公共浮动平滑时间=2f;
公共浮动zoomFactor=5f;
公共浮动系数=10f;
私有变换m_变换;
浮动旋转Y轴=0.0f;
浮动旋转X轴=0.0f;
浮动速度x=0.0f;
浮动速度y=0.0f;
//用于初始化
void Start()
{
Vector3角度=transform.eulerAngles;
旋转y轴=角度y;
旋转x轴=角度x;
//使刚体不改变旋转
if(GetComponent())
{
GetComponent().freezeRotation=true;
}
}
无效更新()
{
如果(!EventSystem.current.IsPointerOverGameObject())
{
if(输入。GetMouseButton(0))
{
摄影机旋转();
}
if(输入。GetMouseButton(1))
{
CameraPan();
}
if(Input.GetAxis(“鼠标滚轮”)=0)
{
CameraZoom();
}
}
}
公共静态浮子夹持器(浮子角度、浮子最小值、浮子最大值)
{
如果(角度<-360F)
角度+=360F;
如果(角度>360F)
角度-=360F;
返回数学夹具(角度、最小值、最大值);
}
公共无效摄像设备()
{
如果(目标)
{
velocityX+=xSpeed*Input.GetAxis(“鼠标X”)*距离*0.02f;
velocityY+=ySpeed*Input.GetAxis(“鼠标Y”)*0.02f;
}
旋转Y轴+=速度X;
旋转X轴-=速度Y;
rotationXAxis=ClampAngle(rotationXAxis,yMinLimit,yMaxLimit);
位置旋转();
}
公共空间
{
如果(!EventSystem.current.IsPointerOverGameObject())
{
Log(“右键单击已按下”);
//transform.rotation=transform.rotation;
transform.Translate(Vector3.right*-Input.GetAxis(“鼠标X”)*panFactor);
transform.Translate(Vector3.forward*-Input.GetAxis(“鼠标Y”)*panFactor);
}        
}
公共空间CameraZoom()
{
如果(!EventSystem.current.IsPointerOverGameObject())
{
//transform.Translate(Vector3.forward*+Input.GetAxis(“鼠标滚轮”)*zoomFactor);
位置旋转();
距离=数学钳制(距离-输入.GetAxis(“鼠标滚轮”)*zoomFactor、distanceMin、distanceMax);
雷卡斯特击中;
if(物理线投射(目标位置、变换位置、出击))
{
距离-=命中距离;
}
}
}
无效位置旋转()
{
来自旋转的四元数=四元数.Euler(transform.rotation.eulerAngles.x,transform.rotation.eulerAngles.y,0);
四元数旋转=四元数.Euler(旋转X轴,旋转Y轴,0);
四元数旋转=旋转;
矢量3负距离=新矢量3(0.0f,0.0f,-距离);
矢量3位置=旋转*负距离+目标位置;
transform.rotation=旋转;
变换位置=位置;
velocityX=Mathf.Lerp(velocityX,0,Time.deltaTime*smoothTime);
velocityY=Mathf.Lerp(velocityY,0,Time.deltaTime*smoothTime);
}
}

问题出在更新功能中。它一直调用Camerazom,因为您设置了

Input.GetAxis("Mouse ScrollWheel") <= 0

Input.GetAxis(“鼠标滚轮”)问题出在更新功能中。它一直调用Camerazom,因为您设置了

Input.GetAxis("Mouse ScrollWheel") <= 0
Input.GetAxis(“鼠标滚轮”)