将Vector3变量从一个脚本(WayPointPositioner)传输到另一个脚本,并将其更改为变换(Walking
本文关键字:脚本 Walking 变换 另一个 WayPointPositioner 变量 Vector3 传输 一个 | 更新日期: 2023-09-27 18:25:51
我在将Vector3wayPointPosition获取到另一个名为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。
调用GameObject.FindGameObjectWithTag("Waypoint").GetComponent<WayPointPositioner>();
看起来像是从游戏对象中检索与指定标记匹配的组件,以及从MonoBehavior
派生的类型参数T。
调用它实际上应该为您提供WayPointPositioner
类的一个实例,然后您可以将其传递给任何您想要的方法,并根据您的意愿与它的Vector3
进行交互。