2017-06-01 259 views
0

我正在制作一个游戏,玩家可以控制两个不同的角色(每个角色都有一个带有摄像头的空物体),并通过按下控制键进行切换。事情是,我试图通过使用另一台相机在两个角色相机之间进行一点点转换,所以它不会在一个和另一个之间进行传输,但我似乎无法做到。我尝试过使用lerp,但我不知道我是否正确,所以我阅读并尝试了Vector3.MoveTowards,但仍无法做到。这是到目前为止我的代码(在while是因为在最后时刻,新空房禁地我):在两台或更多台摄像机之间切换unity3D

public class CameraController : MonoBehaviour 
{ 
    public Camera cam1; 
    public Camera cam2; 
public Camera movingCamera; 

public bool isCurrentPlayer; 
public Transform target1; 
public Transform target2; 
public float speed = 0.2f; 
void FixedUpdate() 
{ 
    float step = speed * Time.deltaTime; 
    if (Input.GetButtonDown("Control")) 
    { 
     if (isCurrentPlayer) 
     { 
      movingCamera.enabled = true; 
      cam2.enabled = false; 
      while (transform.position != target1.position) 
      { 
       transform.position = Vector3.MoveTowards(transform.position, target1.position, step); 
      } 
      if (transform.position == target1.transform.position) 
      { 
       movingCamera.enabled = false; 
       cam1.enabled = true; 
      } 
      isCurrentPlayer = false; 
     } 
     else if (!isCurrentPlayer) 
     { 
      movingCamera.enabled = true; 
      cam1.enabled = false; 
      while (transform.position != target2.position) 
      { 
       transform.position = Vector3.MoveTowards(transform.position, target2.position, step); 
      } 
      if (transform.position == target2.transform.position) 
      { 
       movingCamera.enabled = false; 
       cam2.enabled = true; 
      } 
      isCurrentPlayer = true; 
     } 
    } 
} 
+0

添加到相机停靠的字符为空的游戏对象。使用一个摄像头并更改角色将摄像头移动到另一个对接对象并将其指定为其子对象。所以你可以控制你的相机移动速度,或者甚至可以添加某种路径,使相机从特性飞跃到人物。 – maximelian1986

+0

你的'CameraController'脚本附带了什么? – Hristo

回答

0

transform.position点CameraController游戏对象的位置。如果你想移动movingCamera你可能想使用movingCamera.transform.position。另外请记住,在您的脚本中,MoveTowards()只会在按下“控制”按钮时触发。

由于memBrain表示这将是最好的做法,只使用一个摄像头 - 视觉上它看起来是一样的。

脚本应该是这个样子:

// Assuming target1 is player 1 and target2 is player 2 

private float snapThreshold = 0.1f; 

private Vector3 movingCameraDestination = Vector3.zero; 

void FixedUpdate() 
{ 
    if(Input.GetButtonDown("Control")) 
    { 
     if(isCurrentPlayer) 
     { 
      //Set position of transition camera to player 1 and set it's destination to player's 2 position 
      movingCamera.transform.position = player1.position; 
      movingCameraDestination = player2.position; 

      //Disable player 1 camera and enable transition camera 
      cam1.enabled = false; 
      movingCamera.enabled = true; 
     } 
     else 
     { 
      //Set position of transition camera to player 21 and set it's destination to player's 1 position 
      movingCamera.transform.position = player2.position; 
      movingCameraDestination = player1.position; 

      //Disable player 1 camera and enable transition camera 
      cam2.enabled = false; 
       movingCamera.enabled = true; 
     } 
    } 

    //If transition camera is enabled and its destination is not Vector3.zero - move it 
    if(movingCameraDestination != Vector3.zero && movingCamera.enabled) 
    { 
     movingCamera.transform.position = Vector3.Lerp(movingCamera.transform.position, movingCameraDestination, speed * Time.deltaTime); 

     //If the distance between transition camera and it's destination is smaller or equal to threshold - snap it to destination position 
     if(Vector3.Distance(movingCamera.transform.position, movingCameraDestination) <= snapThreshold) 
     { 
      movingCamera.transform.position = movingCameraDestination; 
     } 

     //If transition camera reached it's destination set it's destination to Vector3.zero and disable it 
     if(movingCamera.transform.position == movingCameraDestination) 
     { 
      movingCameraDestination = Vector3.zero; 
      movingCamera.enabled = false; 
     } 
    } 
} 
0

我好奇的两件事情。为什么你使用FixedUpdate来管理你的更新?这不是物理代码。你有使用多台摄像机的特殊原因吗?如果可以的话,我提出以下更改。

您可以简单地使用主摄像头而不是多个摄像头。此外,您可以通过使用一组玩家GameObjects来增加玩家对象的数量,并通过将输入参数更改为左控制和右控制,您可以在下一个玩家和前一个玩家之间进行切换,以双向导航球员阵列。

下面是实现这些改变我的示例代码(测试和工程,但可以改进。)

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 

// Attached to Main Camera 
public class CameraController : MonoBehaviour { 
    // set manually in inspector 
    public GameObject[] players; 
    public float movementSpeed = 1.0f; 
    public float rotationSpeed = 1.0f; 

    private int currentPlayer; 
    private float startTime; 
    private float distanceToPlayer; 
    private Vector3 startPosition; 
    private Quaternion startOrientation; 

    // Use this for initialization 
    void Start() { 
     currentPlayer = 0; 
     ResetCamera(); 
    } 

    // Update is called once per frame 
    void Update() { 
     float distanceCovered; 
     float rotationCovered; 
     float fractionTraveled; 

     // switch to previous 
     if (Input.GetButtonDown("left ctrl")) { 
      if (currentPlayer == 0) currentPlayer = players.Length - 1; 
      else currentPlayer--; 
      ResetCamera(); 
     } 

     // switch to nextPlayer 
     if (Input.GetButtonDown("right ctrl")) { 
      if (currentPlayer == players.Length - 1) currentPlayer = 0; 
       else currentPlayer++; 
       ResetCamera(); 
      } 

      // Keep moving camera 
      if (transform.position != players[currentPlayer].transform.position) 
      { 
       distanceCovered = (Time.time - startTime) * movementSpeed; 
       fractionTraveled = distanceCovered/distanceToPlayer; 
       rotationCovered = (Time.time - startTime) * rotationSpeed; 
       // Lerp to player position 
       transform.position = Vector3.Lerp(
        startPosition, 
        players[currentPlayer].transform.position, 
        fractionTraveled 
       ); 
       // match player orientation 
       transform.rotation = Quaternion.RotateTowards(
        transform.rotation, 
        players[currentPlayer].transform.rotation, 
        rotationCovered 
       ); 
      // Stop moving camera 
      } else { 
       // Match orientation 
       if (transform.rotation != players[currentPlayer].transform.rotation) 
        transform.rotation = players[currentPlayer].transform.rotation; 

       // Set parent transform to current player 
       transform.parent = players[currentPlayer].transform; 
      } 
    } 

    void ResetCamera() { 
     transform.parent = null; 
     startTime = Time.time; 
     startPosition = transform.position; 
     startOrientation = transform.rotation; 
     distanceToPlayer = Vector3.Distance(
      transform.position, 
      players[currentPlayer].transform.position 
     ); 
    } 
} 

显然,值需要进行调整,而运动算法是非常基本的。粗糙但功能。您还可以将播放器移动代码添加到相机中,只要确保它指向播放器[currentPlayer],它就可以用于每个播放器对象,而无需使用其他脚本,除非有理由这样做。

请随时使用此代码。就像我说的那样,它是有效的。但是,如果您选择这样做,只需简单地移除数组并重新启动单个GameObjects,就可以轻松修改它以像原始代码一样运行。

+0

因为我有等距视图,所以我使用多个摄像头,所以我可以通过让他们有一个空的游戏对象的孩子来轻松控制角度。我会尝试代码的两种方式,并告诉你如何去做,非常感谢! –

相关问题