不同物体碰撞后改变场景
Change scene after collision of different objects
我在天空中有 10 个圆柱体,我希望当所有 10 个圆柱体都撞击地面(飞机)时,我可以将场景更改为不同的场景。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class contactGround : MonoBehaviour
{
void OnCollisionEnter(Collision col){
if(col.gameObject.name == "Cylinder" && col.gameObject.name == "Cylinder (1)"
&& col.gameObject.name == "Cylinder (2)" && col.gameObject.name == "Cylinder (3)"
&& col.gameObject.name == "Cylinder (4)" && col.gameObject.name == "Cylinder (5)"
&& col.gameObject.name == "Cylinder (6)" && col.gameObject.name == "Cylinder (7)"
&& col.gameObject.name == "Cylinder (8)" && col.gameObject.name == "Cylinder (9)"){
Debug.Log("Collision detected");
SceneManager.LoadScene(16);
}
}
}
我试过了,但没用。有什么想法吗??
一个对象 (col.gameObject
) 怎么能同时有 10 个不同的名称? ;)
您宁愿跟踪哪些对象发生碰撞并计算发生碰撞的数量。他们一旦你达到10就去下一个场景
public class contactGround : MonoBehaviour
{
private HashSet<GameObject> collided = new HashSet<GameObject>();
void OnCollisionEnter(Collision col)
{
if(col.gameObject.name.StartsWith("Cylinder") && !collided.Contains(col.gameObject))
{
Debug.Log("Collision detected");
collided.Add(col.gameObject);
if(collided.Count >= 10)
{
SceneManager.LoadScene(16);
}
}
}
}
虽然总的来说宁愿不要跟着我走,而是给他们一些Tag,比如Cyllinder
然后检查
if(col.gameObject.name.CompareTag("Cylinder") && !collided.Contains(col.gameObject))
我在天空中有 10 个圆柱体,我希望当所有 10 个圆柱体都撞击地面(飞机)时,我可以将场景更改为不同的场景。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class contactGround : MonoBehaviour
{
void OnCollisionEnter(Collision col){
if(col.gameObject.name == "Cylinder" && col.gameObject.name == "Cylinder (1)"
&& col.gameObject.name == "Cylinder (2)" && col.gameObject.name == "Cylinder (3)"
&& col.gameObject.name == "Cylinder (4)" && col.gameObject.name == "Cylinder (5)"
&& col.gameObject.name == "Cylinder (6)" && col.gameObject.name == "Cylinder (7)"
&& col.gameObject.name == "Cylinder (8)" && col.gameObject.name == "Cylinder (9)"){
Debug.Log("Collision detected");
SceneManager.LoadScene(16);
}
}
}
我试过了,但没用。有什么想法吗??
一个对象 (col.gameObject
) 怎么能同时有 10 个不同的名称? ;)
您宁愿跟踪哪些对象发生碰撞并计算发生碰撞的数量。他们一旦你达到10就去下一个场景
public class contactGround : MonoBehaviour
{
private HashSet<GameObject> collided = new HashSet<GameObject>();
void OnCollisionEnter(Collision col)
{
if(col.gameObject.name.StartsWith("Cylinder") && !collided.Contains(col.gameObject))
{
Debug.Log("Collision detected");
collided.Add(col.gameObject);
if(collided.Count >= 10)
{
SceneManager.LoadScene(16);
}
}
}
}
虽然总的来说宁愿不要跟着我走,而是给他们一些Tag,比如Cyllinder
然后检查
if(col.gameObject.name.CompareTag("Cylinder") && !collided.Contains(col.gameObject))