简体   繁体   English

如何统一引用播放器

[英]How to reference a Player in unity

The code (GameObject reference) doesn't show up so I need a different way to reference a player.代码(游戏对象引用)没有显示,所以我需要一种不同的方式来引用玩家。 I have not tried much though.不过我还没有尝试太多。

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);
    }
}

This is my camera fixing code.这是我的相机修复代码。 Which is why I need the reference in the first place.这就是为什么我首先需要参考。 I thank you for the help (NO gmae written please the school blocks the word so if you reply with the word I wont be able to access the site).我感谢你的帮助(没有写 gmae,请学校屏蔽这个词,所以如果你用这个词回复我将无法访问该网站)。

The Player does not show up in the inspector because it's type, Object is not serializable. Player不会出现在检查器中,因为它的类型是Object不可序列化。 You want to use GameObject for Unity objects.您想将GameObject用于 Unity 对象。

public GameObject Player;

You have a few other errors in this code.您在此代码中还有其他一些错误。

  1. You can't set the newPos using a reference to another object outside of a method.您不能使用对方法之外的另一个对象的引用来设置newPos Do this in Update() instead.改为在Update()执行此操作。

     Vector3 newPos; // Update is called once per frame void Update () { newPos = Player.transform.position; // your other code }
  2. There is a typo on the last line of update, where the P in Player needs to be capital (that's what you named your variable).更新的最后一行有一个错字,其中 Player 中的 P 需要大写(这就是您命名变量的原因)。

     transform.LookAt(Player.transform);

EDIT: However, since you only seem to be using the Player.transform anyways, you might as well go ahead and reference the transform component instead.编辑:但是,由于您似乎只是在使用 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);
    }
}

If the script is attached to the player you can do this:如果脚本附加到播放器,您可以执行以下操作:

private GameObject player;

void Start()
{
    player = GetComponent<GameObject>();
}

However, you can make a public variable as demonstrated in the other answer by Marcus.但是,您可以创建一个公共变量,如 Marcus 的另一个答案中所示。

But... If you want to find the game object at runtime, you can also do this:但是...如果你想在运行时找到游戏对象,你也可以这样做:

private GameObject player;

void Start()
{
    player = GameObject.FindWithTag("Player");
}

You just have to be certain to tag your player accordingly.你只需要确定相应地标记你的玩家。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM