![](/img/trans.png)
[英]How do you reference the Player Class in Photon Unity Networking 2?
[英]How to reference a Player in unity
代碼(游戲對象引用)沒有顯示,所以我需要一種不同的方式來引用玩家。 不過我還沒有嘗試太多。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CamaraBehaviour : MonoBehaviour {
public Object Player;
public float yOffset = 3.0f;
public float zOffset = 10.0f;
Vector3 newPos = Player.transform.position;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
newPos.y = newPos.y + yOffset;
newPos.z = newPos.z + zOffset;
transform.position = newPos;
transform.LookAt(player.transform);
}
}
這是我的相機修復代碼。 這就是為什么我首先需要參考。 我感謝你的幫助(沒有寫 gmae,請學校屏蔽這個詞,所以如果你用這個詞回復我將無法訪問該網站)。
Player
不會出現在檢查器中,因為它的類型是Object
不可序列化。 您想將GameObject
用於 Unity 對象。
public GameObject Player;
您在此代碼中還有其他一些錯誤。
您不能使用對方法之外的另一個對象的引用來設置newPos
。 改為在Update()
執行此操作。
Vector3 newPos; // Update is called once per frame void Update () { newPos = Player.transform.position; // your other code }
更新的最后一行有一個錯字,其中 Player 中的 P 需要大寫(這就是您命名變量的原因)。
transform.LookAt(Player.transform);
編輯:但是,由於您似乎只是在使用 Player.transform,因此您最好繼續引用轉換組件。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CamaraBehaviour : MonoBehaviour {
public Transform Player;
public float yOffset = 3.0f;
public float zOffset = 10.0f;
Vector3 newPos;
// Update is called once per frame
void Update () {
newPos = Player.position;
newPos.y = newPos.y + yOffset;
newPos.z = newPos.z + zOffset;
transform.position = newPos;
transform.LookAt(Player.position);
}
}
如果腳本附加到播放器,您可以執行以下操作:
private GameObject player;
void Start()
{
player = GetComponent<GameObject>();
}
但是,您可以創建一個公共變量,如 Marcus 的另一個答案中所示。
但是...如果你想在運行時找到游戲對象,你也可以這樣做:
private GameObject player;
void Start()
{
player = GameObject.FindWithTag("Player");
}
你只需要確定相應地標記你的玩家。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.