2016-05-06 47 views
1

我無法讓我的相機與玩家一起移動。Unity Roll-A-Ball主相機

這是CameraController.cs

using UnityEngine; 
using System.Collections; 

public class CameraController : MonoBehaviour 
{ 

    public GameObject Player; 
    private Vector3 offset; 
    void Start() 
    { 
     transform.position = Player.transform.position; 
    } 

    void LateUpdate() 
    { 
     transform.position = Player.transform.position; 
     Debug.LogError(transform.position); 
    } 
} 

該腳本是主攝像機的一個組件。相機不是玩家對象的孩子,反之亦然。

調試表明位置正在更新爲玩家的位置,但是當遊戲運行時,相機是靜態的並且不會從其初始起點移動。

+0

這看起來應該起作用。相機的位置調試表明,如果它沒有明顯改變,它與玩家的相同是非常奇怪的。我認爲你的錯誤必須在你的會話中的其他地方。你的開始函數和成員變量'offset'的減速是不必要的,但是這對你試圖達到的目標沒有任何影響。會話中是否有其他腳本會改變相機的位置?你是否在控制檯中發現錯誤? –

回答

1

試試這個:

using UnityEngine; 
using System.Collections; 

public class CameraController: MonoBehaviour { 

public GameObject Player; 
private Vector3 offset; 

void Start() { 
    offset = transform.position - Player.transform.position; 
    } 

void LateUpdate() { 
    transform.position = Player.transform.position + offset; 
    } 
} 

偏移量是距離相機和球員之間。

另一種方法是讓相機成爲玩家的孩子。

+1

實際上,您不能將相機制作爲播放器的孩子,因爲播放器會滾動並導致整個相機旋轉。事實上,教程顯示。 –

+0

但是如果你根據它的方向凍結某些軸,它將工作 – trahane

0

非常感謝發佈和嘗試幫助。問題在於我試圖讓腳本在虛擬現實支持環境中移動攝像頭。我發現相機的行爲方式以及相機如何移動的方式與VR環境中的方式不同,並且相機需要是要移動的對象的子項,並且不能通過腳本移動。

1
using UnityEngine; 
using System.Collections; 

public class CameraFollower : MonoBehaviour 
{ 
    public Transform thePlayer; 

    private Vector3 offset; 

    void Start() 
    { 
     offset = transform.position - thePlayer.position; 
    } 

    void Update() 
    { 
     Vector3 cameraNewPosition = new Vector3(thePlayer.position.x + offset.x, offset.y, thePlayer.position.z + offset.z); 
     transform.position = cameraNewPosition; 
    } 
}