2016-10-14 6 views
0

Im пытается сделать так, чтобы камера всегда следила за мячом, но я могу только вращать ее, когда я удерживаю правый щелчок. хотя, прямо сейчас, он следует только за мячом, когда я держу правый щелчок. Есть ли способ разделить их?C#/Unity Вращение камеры

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

вы можете пересмотреть свой вопрос: в настоящее время вы говорите, что должно произойти, когда «RightClick вниз», и что вы хотите, когда «RightClick вниз» –

+0

Это потому, что я хочу, чтобы это произошло, когда я держать правый клик вниз. поэтому я не совсем уверен, что вы подразумеваете под этим вопросом ... –

+0

Я хочу, чтобы поворот произошел, когда я держу его, а не движение камеры, я хочу, чтобы это оставалось постоянным. –

ответ

2

Похоже, вы хотите, чтобы поворот произошел только при нажатии правой кнопки мыши. Если это так, то удалите Input.GetMouseButton(1), откуда он теперь, а затем оберните его вокруг transform.rotation = rotation;. Это так просто. FYI, вам даже не нужно 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); 
} 
+0

Ну, это сработало. Сорта. теперь он следит за мячом, когда im не использует rightclick, но проблема в том, что когда im не использует правую кнопку мыши, экран все равно перемещается относительно того места, где я держу мышь. плохо загрузите видео в реальном времени, что я имею в виду. https://www.youtube.com/watch?v=vaesOfRcR4k –

Смежные вопросы