Итак, у меня есть ракета, которая, когда не сталкивается с землей, летит вперед.Когда он поражает что-то с помощью тега «Земля», он должен остановиться и вызвать взрыв.Однако он не определяет, когда он касается «Земли», и проходит через него.
Я пытался изменить работу коллайдера, но он просто делал ошибки.
Я добавил несколько функций печати, чтобы увидетьесли он действительно запущен и не активирован.
using UnityEngine;
using System.Collections;
public class Rocket : MonoBehaviour
{
//Public changable things
public float speed = 20.0f;
public float life = 5.0f;
public bool canRunProgress = true;
public bool isGrounded;
public GameObject Explosion;
public Transform rocket;
public Rigidbody rb;
// If the object is alive for more than 5 seconds it disapears.
void Start()
{
Invoke("Kill", life);
}
//detects if tounching ground
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Ground")
{
print("working");
isGrounded = true;
print("working");
}
}
//detects if tounching ground
void OnCollisionExit(Collision other)
{
if (other.gameObject.tag == "Ground")
{
isGrounded = false;
}
}
//kill routine - explosion is summoned and explodes 2 seconds later it then destroys the rocket.
IEnumerator Kill()
{
GameObject go = (GameObject)Instantiate(Explosion, transform); // also this needs to have the explosion be summoned in the middel of the rocket.
yield return new WaitForSeconds(2f);
Destroy(gameObject);
}
// Update is called once per frame
void Update()
{
//if the object isn't tounching the ground and is able to run it's process
if (isGrounded == false && canRunProgress)
{
transform.position += transform.forward * speed * Time.deltaTime;
canRunProgress = true;
}
//if the object IS touching the ground it then makes the above process unable to work and then begins the kill routine
else if(isGrounded == true)
{
print("YES");
canRunProgress = false;
StartCoroutine(Kill());
}
}
}
Он должен остановить ракету и затем вызвать взрыв.однако в настоящее время он проходит через все.
Извините за вставку всего кода.Любая помощь будет принята с благодарностью: 3