如何在调用Teleport()函数时获取enemyTransform.position
?
当与敌人对象发生碰撞时,enemyTransform
会被保存下来,玩家可以在OnTriggerEnter
之后的任意时间按下传送按钮。
由于敌人可以移动,我希望传送能发生在敌人的当前位置上。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using ThrowOrbNamespace;
public class orbLogic : MonoBehaviour
{
public ThrowOrbNamespace.ThrowOrb throwOrb;
public Button teleportButton;
public CharacterController characterController;
private Transform enemyTransform;
void Start()
{
teleportButton.interactable = false;
}
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Enemy"))
{
enemyTransform = other.transform;
throwOrb.StopAll();
Destroy(gameObject);
teleportButton.interactable = true;
Debug.Log(enemyTransform.position); // 在这里enemyTransform不为空
}
}
public void Teleport()
{
Debug.Log(enemyTransform.position); // 在这里enemyTransform为空
if (enemyTransform != null)
{
characterController.enabled = false;
characterController.transform.position = enemyTransform.position;
characterController.enabled = true;
teleportButton.interactable = false;
}
}
}