繁体   English   中英

从另一个脚本获取Vector 3(C#Unity)

[英]Getting Vector 3 from another Script (C# Unity)

我有一个脚本来定位玩家从平台上掉下来的位置。

Platform Tracker.cs

public GameObject platformTrackerPoint;
public Vector3 platformTransform;

public PlayerMovement thePlayerMovement;

void Start()
{
    platformTrackerPoint = GameObject.Find("PlatformTrackerPoint");

    thePlayerMovement = FindObjectOfType<PlayerMovement>(); 
}

void Update()
{
    if ((transform.position.x < platformTrackerPoint.transform.position.x) && thePlayerMovement.grounded)
    {
        platformTransform = gameObject.transform.position;
        Debug.Log ("Plaform Transform =" + platformTransform);
    }
}

在调试时,platformTransform显示了我需要的值。 但它没有反映在下面的代码中。

GameController.cs

    public PlatformTracker thePlatformTracker;

    public Vector3 tempPosition;

void Start () {
        platformStartPoint = platformGenerator.position;
        playerStartPoint = thePlayer.transform.position;

        thePlatformTracker = FindObjectOfType<PlatformTracker>();
        thePlayer = FindObjectOfType<PlayerMovement>();
    }


    public void RespawnPlayer()
    {
       thePlayer.transform.position = thePlatformTracker.platformTransform;
    }

非常感谢您的帮助。 如果有任何不清楚的地方,请告诉我。

要正确访问平台跟踪器,您需要使用GetComponent。 在GameController.cs中,Start()只需更改:

thePlatformTracker = FindObjectOfType<PlatformTracker>();

至:

thePlatformTracker = GameObject.Find("").GetComponent<Platform Tracker>();

确保在GameObject.Find(“”)引号之间添加游戏控制器对象名称。

然后你应该能够访问platformTransform没问题,因为thePlatformTracker现在正确地引用了脚本。

暂无
暂无

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

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