Итак, я делаю игру, в которой основной механик - это прыжки с ракет (стрельба ракетой по вашим ногам и сила взрыва, толкающая вас (как TF2)), и я не могу заставить взрыв вызвать только один разИ это вызывает не в том месте: /
Я попытался добавить ожидания в оператор if и наткнулся на то, что я сейчас использую.Теоретически это должно работать, но это не так.
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 dissapears.
void Start()
{
Invoke("Kill", life);
}
// 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)
{
canRunProgress = false;
StartCoroutine(Kill());
}
//detects if tounching ground
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Ground")
{
isGrounded = true;
}
}
//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);
}
}
}
Он должен (когда ракета вызывается в игру с помощью пусковой установки) заставить ракету лететь вперед, когда она падает на землю (с меткой "земля ") прекратить движение и вызвать взрыв вокруг него и через 2 секунды быть уничтоженным.В настоящее время он просто патонно прыгает по земле.
Любая помощь будет принята с благодарностью.: 3