2017-06-30 48 views
0

這是原來的代碼使相機跟隨玩家:如何讓用戶可以選擇相機是否在播放器後面?

using UnityEngine; 
using System.Collections; 

public class CameraFollow : MonoBehaviour 
{ 
    public GameObject objectToFollow;  //Public variable to store a reference to the player game object 
    private Vector3 offset;   //Private variable to store the offset distance between the player and camera 

    // Use this for initialization 
    void Start() 
    { 
     //Calculate and store the offset value by getting the distance between the player's position and camera's position. 
     offset = transform.position - objectToFollow.transform.position; 
    } 

    // LateUpdate is called after Update each frame 
    void LateUpdate() 
    { 
     // Set the position of the camera's transform to be the same as the player's, but offset by the calculated offset distance. 
     transform.position = objectToFollow.transform.position + offset; 
     transform.LookAt(objectToFollow.transform); 
    } 
} 

而這正是我試圖做的,但隨後球員它的自我(ThirdPersonController)不旋轉根據他移動。

隨着他原來的腳本。

using UnityEngine; 
using System.Collections; 

public class CameraFollow : MonoBehaviour 
{ 
    public GameObject objectToFollow;  //Public variable to store a reference to the player game object 
    public bool behindPlayer = false; 

    private Vector3 cameraStartPos; 

    // Use this for initialization 
    void Start() 
    { 
     cameraStartPos = transform.position; 

     // Put the camera behind the player 
     if (behindPlayer == true) 
     { 
      transform.position = (objectToFollow.transform.position - (objectToFollow.transform.forward * 5) + (objectToFollow.transform.up * 2)); 
     } 
    } 

    private void Update() 
    { 
     if (behindPlayer == true) 
     { 
      transform.position = (objectToFollow.transform.position - (objectToFollow.transform.forward * 5) + (objectToFollow.transform.up * 2)); 
     } 
     else 
     { 
      transform.position = cameraStartPos; 
     } 
    } 

    // LateUpdate is called after Update each frame 
    void LateUpdate() 
    { 
     transform.position = objectToFollow.transform.position; 
     transform.LookAt(objectToFollow.transform); 
    } 
} 

我想,相機會自動將玩家已經落後,而無需更改現場攝像機的位置,如果用戶希望使用布爾變量。

但腳本現在沒有在上面,如:

這條線在LateUpdate:

transform.position = objectToFollow.transform.position; 

使用布爾假/真它不改變相機位置的。 如果沒有這條線,玩家將會移動並且相機會跟隨,但是玩家不會像以上那樣旋轉和移動。

我想要的就像第一個腳本,但與behindPlayer變量。

回答

0

甲工作腳本:

using UnityEngine; 
using System.Collections; 

public class CameraFollow : MonoBehaviour 
{ 
    [SerializeField] 
    private Transform target; 

    [SerializeField] 
    private Vector3 offsetPosition; 

    [SerializeField] 
    private Space offsetPositionSpace = Space.Self; 

    [SerializeField] 
    private bool lookAt = true; 

    private void Update() 
    { 
     Refresh(); 
    } 

    public void Refresh() 
    { 
     if (target == null) 
     { 
      Debug.LogWarning("Missing target ref !", this); 

      return; 
     } 

     // compute position 
     if (offsetPositionSpace == Space.Self) 
     { 
      transform.position = target.TransformPoint(offsetPosition); 
     } 
     else 
     { 
      transform.position = target.position + offsetPosition; 
     } 

     // compute rotation 
     if (lookAt) 
     { 
      transform.LookAt(target); 
     } 
     else 
     { 
      transform.rotation = target.rotation; 
     } 
    } 
} 

在檢查器中設置的偏移量,例如,以0,5,-12和作爲目標設定例如ThirdPersonController。並將腳本附加到攝像機(因爲ThirdPersonController ThirdPersonUserControl腳本正在查找MainCamera,所以如果目標是ThirdPersonController,攝像機必須標記爲MainCamera)。

而且攝像頭不一定是目標的子攝像頭可以在層次結構中的任何位置。

無論如何,它的工作。

相關問題