C# 如何使OnCollisionCenter功能正常工作?
所以,我有一个火箭,当它不与地面相撞时,它会向前飞。当它撞上带有标签的地面时,它应该停止并发出爆炸声。然而,当它接触地面并穿过地面时,它无法检测 我试图改变对撞机的工作方式,但它只是犯了错误 我添加了一些打印功能,看看它是否真的被触发了C# 如何使OnCollisionCenter功能正常工作?,c#,unity3d,C#,Unity3d,所以,我有一个火箭,当它不与地面相撞时,它会向前飞。当它撞上带有标签的地面时,它应该停止并发出爆炸声。然而,当它接触地面并穿过地面时,它无法检测 我试图改变对撞机的工作方式,但它只是犯了错误 我添加了一些打印功能,看看它是否真的被触发了 using UnityEngine; using System.Collections; public class Rocket : MonoBehaviour { //Public changable things public float
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首先,请确保您的游戏对象上安装了碰撞器。如果是2D游戏,它应该是2D碰撞器,检测2D碰撞所需的功能是OnCollisionInter2D,并确保碰撞器未设置为inspector中的触发器。如果是3D游戏,请继续使用OnCollisionCenter和未设置为触发器的3D碰撞器
如果您确实需要触发器碰撞器,则可以使用函数OnTiggerEnter和OnTiggerEnter2D来检测到触发器碰撞器的碰撞。但是,请记住,触发器碰撞器将允许物体通过它,但仍将检测碰撞。如果它只是路过,则需要查看标记、碰撞器和刚体值。请注意,下面的代码适用于二维项目,如果您正在处理三维项目,请确保应用更改
public class Example : MonoBehaviour
{
public GameObject explosion;
public bool isGrounded;
private void Start()
{
Invoke("RemoveRocket", 5);
}
private void FixedUpdate()
{
if (!isGrounded)
{
MoveRocket();
}
}
private void MoveRocket()
{
transform.position += (transform.right / .1f) * Time.deltaTime;
}
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag("Ground"))
{
Instantiate(explosion, transform);
isGrounded = true;
CancelInvoke();
Destroy(gameObject, 2);
}
}
private void RemoveRocket()
{
Destroy(gameObject, 0);
}
}
你的碰撞盒是触发器吗?如果你在if之前打印,它会在OnCollisionCenter内部打印并退出吗?正如你所知,如果你使用2D碰撞器,碰撞功能会有所不同。好的,所以我摆弄了一下代码,使用了你的@Nikola G中的一些位。现在它可以工作了:D感谢所有回答我愚蠢问题的人lol。@bobshellby如果它对你有帮助,那么你应该接受这个答案,或者至少投票支持,这样其他人可能会发现它有用。我错过了刚体属性!谢谢你指出!