2016-03-07 51 views
1

我有一个摄像头脚本,我正在努力,我只是无法让相机平底锅工作。它识别输入,但它根本不移动相机。平移不能在我的相机脚本内工作

它在一个点上工作,但它在缩放或旋转时重置相机,现在变焦和旋转工作完美。这个问题是由于功能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); 
} 
} 

回答

0

问题出在你的更新函数中。它调用CameraZoom所有的时间,因为你设置

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

而且CameraZoom调用PositionRotation,在那里你相机的当前位置恢复到原来的位置。

尝试将其设置为

if (Input.GetAxis("Mouse ScrollWheel") <= -0.1) 
{ 
    CameraZoom(); 
} 
if (Input.GetAxis("Mouse ScrollWheel") >= 0.1) 
{ 
    CameraZoom(); 
} 

这应该做的伎俩。

Cheers Tobi