![](/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.