所以,我正在做一个游戏,主要的机械师是火箭跳跃(向你的脚发射火箭,爆炸的力量推动你(像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
发布于 2019-04-04 07:43:21
首先,您得到了一个语法错误: OnCollisionEnter、OnCollisionExit和Kill方法在更新方法中……你应该先解决这个问题。
然后,为了让你的代码工作,我假设你已经在火箭上放置了一个对撞机,并在地面上放置了一个对撞机。如果火箭在弹跳,这可能是刚体的原因。事实上,刚体使物体发生碰撞和反弹,因此不会抛出OnCollisionEnter。如果火箭或地面上有一个,请尝试将其移除。
https://stackoverflow.com/questions/55506666
复制