我无法从我的函数更新我的vector2数据。 这个vector2的目的是保存死去的敌人的位置,但是当我全局使用它时,在函数内部它具有正确的值时将变为0,0。
这是代码
private Vector2 enemyPosition;
void Update()
{
Debug.Log("enemyPosition update: " + enemyPosition);
gameObject.transform.position = Vector2.MoveTowards(transform.position, enemyPosition, blobSpeed * Time.deltaTime);
}
public void createBlobMelee(GameObject enemy)
{
enemyPosition = new Vector2(enemy.transform.position.x, enemy.transform.position.y);
Debug.Log("enemyPosition: " + enemyPosition);
randomBlobx = Random.Range(-1, 1);
randomBloby = Random.Range(-1, 1);
Vector2 randomBlobCreate = new Vector2(enemy.transform.position.x + randomBlobx, enemy.transform.position.y + randomBloby);
Instantiate(gameObject, randomBlobCreate, Quaternion.identity);
}
这是调试控制台的结果。
感谢您的时间, 安德烈·索萨(AndréSousa)
尝试使您的对象为
public static Vector2 enemyPosition;
If it can not be static try
public
or try juststatic
.I have this problem very often, you just need to play with the
public
andstatic
and see what works for you.