【发布时间】:2018-10-28 21:24:23
【问题描述】:
我有一个脚本,它通过其变换检测对象的距离并将其显示给玩家,我希望目标对象在它被破坏后发生变化,以便目标标记指向不同的对象。到目前为止,这是我的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class DistanceToPickup : MonoBehaviour {
// Reference to checkpoint position
[SerializeField]
private Transform checkpoint;
// Reference to UI text that shows the distance value
[SerializeField]
private Text distanceText;
GameObject exit;
// Calculated distance value
private float distance;
private void Start()
{
exit = GameObject.Find("Door_01");
}
// Update is called once per frame
private void Update()
{
// Calculate distance value between character and checkpoint
distance = (checkpoint.transform.position - transform.position).magnitude;
// Display distance value via UI text
// distance.ToString("F1") shows value with 1 digit after period
// so 12.234 will be shown as 12.2 for example
// distance.ToString("F2") will show 12.23 in this case
distanceText.text = "Pickup: " + distance.ToString("F1") + " meters";
if (Pickup.isDestroyed == true)
{
//point transform checkpoint to new object
checkpoint = exit.transform;
distanceText.text = "Exit: " + distance.ToString("F1") + " meters";
}
}
}
我找到了我也想指向标记的游戏对象,并将检查点标记更改为指向出口变换,但它没有更改距离文本,我看不出代码有什么问题。任何帮助将不胜感激
接送班
public class Pickup : MonoBehaviour {
[SerializeField]
public GameObject m_ExplosionPrefab;
private ParticleSystem m_ExplosionParticles;
private int pickupCounter = 0;
[SerializeField]
private Text PickupText;
public static bool isDestroyed;
private void Update()
{
PickupText.text = "Pickups: " + pickupCounter + " /1";
}
private void OnEnable()
{
// Instantiate the explosion prefab and get a reference to the particle system on it.
m_ExplosionParticles = Instantiate(m_ExplosionPrefab).GetComponent<ParticleSystem>();
// Disable the prefab so it can be activated when it's required.
m_ExplosionParticles.gameObject.SetActive(false);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
Destroy(this.gameObject);
pickupCounter++;
isDestroyed = true;
// Move the instantiated explosion prefab to the tank's position and turn it on.
m_ExplosionParticles.transform.position = transform.position;
m_ExplosionParticles.gameObject.SetActive(true);
// Play the particle system of the tank exploding.
m_ExplosionParticles.Play();
PickupText.text = "Pickups: " + pickupCounter + " /1";
}
}
}
【问题讨论】:
-
你检查
Pickup是否被破坏,但Pickup是什么?我没有看到它在任何地方定义,如果对象被破坏,你应该检查检查点是否为空,对吗? -
嗨!是否在 Pickup 类中检查布尔 isDestroyed。 isDestroyed 布尔值是一个公共静态变量,因此我可以从此类访问它
-
如果你破坏了 this.gameObject,你也破坏了 this.gameObject 并且被破坏的东西,嗯,被破坏了,不能再做东西了。您可能希望通过实际存在的不同游戏对象来控制破坏以讲述故事;)