将Vector3变量从一个脚本(WayPointPositioner)传输到另一个脚本并将其更改为转换(行走)

时间:2014-10-08 08:15:04

标签: c# unity3d monodevelop

将Vector3 wayPointPosition转换为另一个名为Walking的脚本并将其更改为Transform目标时遇到了一些麻烦。我的麻烦在于我试图从WayPointPositioner获取这个动态变量(它根据在舞台中点击的对象以及玩家是否与此路点重叠而改变)并导入并在另一个脚本中使用它。

以下是我正在使用的代码。

WayPointPositioner

using UnityEngine;
using System.Collections;

public class WayPointPositioner : MonoBehaviour {

public Vector3 wayPointPosition = Vector3.zero;
private bool checkPlayerWaypointCollision;

void Start()
{

}

void OnTriggerStay2D (Collider2D other) 
{
    // Check if collision is occuring with player character.
    if (other.gameObject.name == "Player")
    {
        checkPlayerWaypointCollision = true;
    }
    else
    {
        checkPlayerWaypointCollision = false;
    }

}

//Check if object is clicked
void OnMouseDown () 
{

    // If its the player, then return a new position for the player to move to for walking
    // Else debug that its not so
        if (checkPlayerWaypointCollision == false)
        {

            Debug.Log ("Object not colliding and retrieving position");
            Debug.Log (wayPointPosition);
            Debug.Log (gameObject.name);

            wayPointPosition = new Vector3 (transform.position.x, transform.position.y, 10);
            wayPointPosition = Camera.main.ScreenToWorldPoint(wayPointPosition);

        }
        else
        {

            Debug.Log ("Object is colliding, no movement needed");

        }

}

}

步行

using UnityEngine;
using System.Collections;

public class Walking : MonoBehaviour {


public Transform target;
public WayPointPositioner wayPointPosition;

public bool walkingAnimation = false;
private Animator anim;

void Awake ()
{
    anim = GetComponent<Animator> ();
    wayPointPosition = GameObject.FindGameObjectWithTag ("Waypoint").GetComponent<WayPointPositioner> ();

}


void Start () 
{

}


void Update () 
{
    Debug.Log ("This is in Walking, WPP =" + wayPointPosition);
}
}

正如你所看到的,我正在尝试从附加到名为“Waypoint”的游戏对象的单独类中导入wayPointPosition(在我当前的布局中,这些是带有圆形碰撞器的空对象,以检查它们是否已被点击)。但是当我运行这个时,我没有得到我的Vector,但是我得到了层次结构中最后一个航点的名称(我目前有6个可以点击的航点)而不是Vector。

我希望有人能够帮助我/指出我的错误。我还在学习C#,所以我可能做了一个奇怪的/奇怪的假设,但是没有用。

亲切的问候,

Veraduxxz。

1 个答案:

答案 0 :(得分:0)

看起来调用GameObject.FindGameObjectWithTag("Waypoint").GetComponent<WayPointPositioner>();从游戏对象中检索与指定标记匹配的组件,以及从MonoBehavior派生的类型参数T.

调用它实际上应该为您提供WayPointPositioner类的实例,然后您可以将其传递给您想要的任何方法,并根据您的需要与其Vector3进行交互。