OnCollisionEnter无法正常工作于Unity3D [英] OnCollisionEnter not working Unity3D
问题描述
我正在尝试开发一款游戏,您需要躲避掉落的物体.我造成了危险,但似乎危险克隆"的行为有所不同.
I'm trying to build a game where u need to dodge falling objects. I've made a hazard but it seems as if the hazard 'clone' is behaving diffrently.
当危险击中需要消除的平台时,我制作了一个碰撞脚本.这适用于危险对象,但不适用于掉落的危险克隆对象.
I've made a collision script when the hazard hits the platform it needs to disappear. This works for the hazard object, but not the hazard clone objects that fall.
- 如您在第一个屏幕截图中所看到的,红色圆圈代表 喜欢它过去.但是蓝色圆圈(克隆)掉落一次 对象.
- 您在第二张屏幕截图中看到,红色圆圈消失了, 因为它击中了平台.但是蓝色仍然落在右边 通过.
- As u can see in the first screenshot, the red circled block behaves like it use to. But the blue circled once (clones) fall right through objects.
- As u can see in the second screenshot, the red circled one is gone, because it hit the platform. But still the blue once fall right through.
提前谢谢!
在u下面将找到Collision脚本,下面是Hazard Spawn脚本:
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 .
OnCollisionEnter
OnCollisionEnter
takes Collision
object as a parameter and it requires the isTrigger
property of the attached Collider
component to be FALSE.
void OnCollisionEnter(Collision collision)
{
foreach (ContactPoint contact in collision.contacts)
{
Debug.DrawRay(contact.point, contact.normal, Color.white);
}
}
OnTriggerEnter
OnTriggerEnter
需要 Collider
对象作为参数,并且需要Collider组件的> isTrigger
属性为 TRUE .
OnTriggerEnter
OnTriggerEnter
takes Collider
object as a parameter and it requires the isTrigger
property of the attached Collider
component to be TRUE.
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("CheckPoint"))
{
Destroy(other.gameObject);
}
}
-
如果要从
prefab
实例化对象,请确保prefab
具有必需的组件(rigidbody
/collider
)和 属性以实现所需的行为.
If you are instantiating the object from
prefab
, make sure thatprefab
have required components (rigidbody
/collider
) and properties to achieve the desired behaviour.
要检测碰撞/触发,至少一个对象必须具有一个
物理组件( Rigidbody
)
To detect Collision/Trigger, at least one of the object must have a
physics component (Rigidbody
)
刚体必须附加到移动对象.
希望这会有所帮助:)
Hope this helps :)
这篇关于OnCollisionEnter无法正常工作于Unity3D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!