gpt4 book ai didi

c# - OnCollisionEnter 不工作 Unity3D

转载 作者:行者123 更新时间:2023-11-30 18:15:47 26 4
gpt4 key购买 nike

我正在尝试构建一个游戏,您需要在其中躲避掉落的物体。我已经制造了危险,但似乎危险“克隆”的行为有所不同。

我已经制作了一个碰撞脚本,当危险撞击到它需要消失的平台时。这适用于危险对象,但不适用于掉落的危险克隆对象。

  • 正如您在第一个屏幕截图中看到的,红色圆圈 block 的行为喜欢它使用。但是蓝色圆圈一次(克隆)直接穿过对象。
  • 正如你在第二张截图中看到的,红色圆圈消失了,因为它撞到了平台。但还是蓝色一旦落下就对了通过。

提前致谢!

enter image description here enter image description here

下面是碰撞脚本,下面是危险生成脚本:

 using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HazardCollisionFunctions : MonoBehaviour {

#region Variables
//Public

//Private
#endregion

#region UnityFunctions
void Start()
{

}
void Update()
{

}
#endregion

private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "platform")
{
this.gameObject.SetActive(false);
}

if(collision.gameObject.tag == "Player")
{

}
}
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnHazards : MonoBehaviour {

#region Variables
//Public

//Private
[SerializeField]
public float minX = 0.0f;
[SerializeField]
public float maxX = 0.0f;
[SerializeField]
private GameObject[] hazards; //potential array of hazards
[SerializeField]
private float timeBetweenSpawns = 0.0f;
private bool canSpawn = false;
private int amountOfHazardsToSpawn = 0;
private int hazardToSpawn = 0;
#endregion

#region UnityFunctions
public void Start()
{
canSpawn = true; //Temp start
}
public void Update()
{
if(canSpawn == true)
{
StartCoroutine("GenerateHazard");
}
}
#endregion

private IEnumerator GenerateHazard()
{
canSpawn = false;
timeBetweenSpawns = Random.Range(0.5f, 2.0f); //Testing values
amountOfHazardsToSpawn = Random.Range(1, 5); //Testing values
for(int i = 0; i < amountOfHazardsToSpawn; i ++)
{
Vector3 spawnPos = new Vector3(Random.Range(minX, maxX), 8.0f, 0.0f); //Gen spawnpoint for the hazard
Instantiate(hazards[hazardToSpawn], spawnPos, Quaternion.identity); //Spawn the hazard
}
yield return new WaitForSeconds(timeBetweenSpawns);
canSpawn = true;
}
}

最佳答案

OnCollisionEnter

OnCollisionEnter需要 Collision对象作为参数,它要求附加的 Collider 组件的 isTrigger 属性为 FALSE

void OnCollisionEnter(Collision collision)
{
foreach (ContactPoint contact in collision.contacts)
{
Debug.DrawRay(contact.point, contact.normal, Color.white);
}
}

触发输入

OnTriggerEnter需要 Collider对象作为参数,它要求附加的 Collider 组件的 isTrigger 属性为 TRUE

void OnTriggerEnter(Collider other)
{
if (other.CompareTag("CheckPoint"))
{
Destroy(other.gameObject);
}
}

  1. If you are instantiating the object from prefab, make sure that prefab have required components (rigidbody/collider) and properties to achieve the desired behaviour.

  2. To detect Collision/Trigger, at least one of the object must have a physics component (Rigidbody)

  3. Rigidbody MUST be attached to the moving object.


希望这有帮助:)

关于c# - OnCollisionEnter 不工作 Unity3D,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47752522/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com