预制多年平均值激活时碰撞对象 [英] Prefab doesnot activate when collide with object
问题描述
我在做一个项目,也就是我面临的一个问题。
我有两个游戏对象的2D撞机(从预制未来)的移动从右到左。当他们互相接触,他们关闭。
我也有一个空的游戏对象,其中我添加脚本 Respawner
其中随机生成障碍。
现在的问题是,当他们互相接触一次,它们不会再重新启动。
Respawner空游戏对象:
边框:
公共类重生:MonoBehaviour {
[SerializeField]
私人游戏对象[]障碍;
私有列表<游戏物体> LISTNAME =新的List<游戏物体>();
无效唤醒(){
InitilizeObstacle();
}
空隙开始(){
StartCoroutine(RandomObstacleSpawn());
}
无效InitilizeObstacle(){
INT指数= 0;
的for(int i = 0; I< obstacles.Length * 3;我++){
游戏对象的obj =实例化(障碍[指数],新的Vector3(transform.position.x,transform.position.y ,-2F),Quaternion.identity)作为游戏对象;
listname.Add(OBJ);
LISTNAME [I] .SetActive(假);
指数++;
如果(指数== obstacles.Length){
指数= 0;
}
}
}
无效洗牌(){
的for(int i = 0; I< listname.Count;我++){
游戏物体温度= LISTNAME [I]
INT随机= Random.Range(I,listname.Count);
LISTNAME [I] = LISTNAME [随机]
LISTNAME [随机] =温度;
}
}
的IEnumerator RandomObstacleSpawn(){
收益回报新WaitForSeconds(Random.Range(1.5F,2.5F));
INT指数= Random.Range(0,listname.Count);
,而(真){
如果{
LISTNAME [指数] .SetActive(真)(LISTNAME [指数] .activeInHierarchy!);
LISTNAME [指数] .transform.position =新的Vector3(transform.position.x,transform.position.y,-2F);
中断;
}其他{
指数= Random.Range(0,listname.Count);
}
StartCoroutine(RandomObstacleSpawn());
}
}
}
脚本附加到预制的举动
公共类ObstacleMove:MonoBehaviour {
私人浮法速度= -1.25f;
无效的start(){}
无效更新(){
的Vector3 POS = transform.position;
pos.x + =速度* Time.deltaTime;
transform.position = POS;
}
}
脚本安装到预制触控边框:
公共类BorderTouch:MonoBehaviour {
无效OnTriggerEnter2D(Collider2D目标){
如果(target.tag ==边框){
gameObject.SetActive(假);
}
}
}
<基于新的问题strong>新的答案:
如果你想使事情发生碰撞后。写一个脚本程序上边框:
使用UnityEngine; System.Collections中使用
;
公共类borderListener:MonoBehaviour {
公共重生rS型;
无效OnTriggerEnter2D(Collider2D目标){
rS.spawnIt();
}
}
在统一编辑器,拖动重生对象边境脚本上层次。
不要跳过此步骤,或事情不会工作!
在重生脚本,删除最后一个 StartCoroutine(RandomObstacleSpawn()) ;
在行的IEnumerator RandomObstacleSpawn()
方法。并创建一个公共方法(从其他脚本访问)的随时随地的重生脚本中:
公共无效spawnIt(){
StartCoroutine(RandomObstacleSpawn());
}
基于旧代码的老答案:
从我看到你的包:
,而(真){ //一个
如果{
// b
LISTNAME [指数] .SetActive(真)(LISTNAME [指数] .activeInHierarchy!);
LISTNAME [指数] .transform.position =新的Vector3(transform.position.x,transform.position.y,-2F);
中断; // C
}其他{
指数= Random.Range(0,listname.Count);
}
StartCoroutine(RandomObstacleSpawn()); // D
}
我是一个可爱的小白,我会尽我所能来帮助。但这段代码让我怀疑:
,而(真)
是什么?什么是真实? (编辑:发现了一些观察波纹管)
中的代码似乎做这个路径:
while循环(A)往里走
转到在if语句(b)
转至断行的第一个选项; (C)
永远不会到达StartCoroutine(D)> 这就是为什么它不会再次激活。
如果你试图把 StartCoroutine(RandomObstacleSpawn());
在突破;
你可能会得到一个统一的崩溃。如果你脱下while语句呢?你需要调整产量寿的时间
这是代码,我会用:
的IEnumerator RandomObstacleSpawn(){
收益回报新WaitForSeconds(Random.Range(3.5F,4.5F));
INT指数= Random.Range(0,listname.Count);
如果{
LISTNAME [指数] .SetActive(真)(LISTNAME [指数] .activeInHierarchy!);
LISTNAME [指数] .transform.position =新的Vector3(transform.position.x,transform.position.y,-2F);
}其他{
指数= Random.Range(0,listname.Count);
}
StartCoroutine(RandomObstacleSpawn());
}
编辑:有关,而(真)我设法找到更多信息关于这个概念在这里: R,如何同时(TRUE)工作
,而仍然在... 突破;
中的代码确实是使得访问 StartCoroutine(RandomObstacleSpawn());
可达。
I'm making a project and there is a problem that I am facing.
I have two gameObject with 2D colliders (coming from a prefab) which moves right to left. When they touch each other they deactivate.
I also have an empty game object in which i add a script Respawner
which randomly generates obstacles.
The problem is when they touch each other once, they never get re-activated again.
Respawner Empty GameObject :
Border :
Prefabs : Respawn Script:
public class Respawn : MonoBehaviour {
[SerializeField]
private GameObject[] obstacles;
private List<GameObject> listname = new List<GameObject>();
void Awake(){
InitilizeObstacle();
}
void Start() {
StartCoroutine(RandomObstacleSpawn());
}
void InitilizeObstacle(){
int index = 0;
for (int i=0; i<obstacles.Length * 3 ; i++) {
GameObject obj = Instantiate(obstacles[index],new Vector3(transform.position.x,transform.position.y,-2f),Quaternion.identity) as GameObject;
listname.Add(obj);
listname[i].SetActive(false);
index++;
if(index==obstacles.Length){
index =0;
}
}
}
void shuffle(){
for (int i=0; i<listname.Count; i++) {
GameObject temp = listname [i];
int random = Random.Range (i, listname.Count);
listname [i] = listname [random];
listname [random] = temp;
}
}
IEnumerator RandomObstacleSpawn(){
yield return new WaitForSeconds(Random.Range(1.5f,2.5f));
int index = Random.Range (0, listname.Count);
while (true) {
if(!listname[index].activeInHierarchy){
listname[index].SetActive(true);
listname[index].transform.position = new Vector3(transform.position.x,transform.position.y,-2f);
break;
} else {
index = Random.Range(0,listname.Count);
}
StartCoroutine(RandomObstacleSpawn());
}
}
}
Script attach to prefab for move:
public class ObstacleMove : MonoBehaviour {
private float speed = -1.25f;
void Start() { }
void Update() {
Vector3 pos = transform.position;
pos.x += speed * Time.deltaTime;
transform.position = pos;
}
}
Scripts attach to prefab for touch border:
public class BorderTouch : MonoBehaviour {
void OnTriggerEnter2D(Collider2D target){
if(target.tag=="Border"){
gameObject.SetActive(false);
}
}
}
New answer based on new question:
If you want to make things happen AFTER the collision. Put a script on Border:
using UnityEngine;
using System.Collections;
public class borderListener : MonoBehaviour {
public Respawn rS;
void OnTriggerEnter2D(Collider2D target){
rS.spawnIt ();
}
}
On Unity Editor, drag the Respawn object to the Border Script on hierarchy. Do not skip this step or things won't work!
On Respawn script, remove the last StartCoroutine(RandomObstacleSpawn());
line on IEnumerator RandomObstacleSpawn()
method. And create a public method (to access from other script) anywhere inside Respawn script:
public void spawnIt(){
StartCoroutine(RandomObstacleSpawn());
}
Old answer based on old code:
From what I see on your package:
while (true) { //A
if(!listname[index].activeInHierarchy){
//B
listname[index].SetActive(true);
listname[index].transform.position = new Vector3(transform.position.x,transform.position.y,-2f);
break; //C
} else {
index = Random.Range(0,listname.Count);
}
StartCoroutine(RandomObstacleSpawn()); //D
}
I am a lil noob, I will try my best to help. But this piece of code makes me wonder:
while(true)
what? what is true? (EDIT: found some observation bellow)
The code seem to do this path: Go inside the while loop (A)
Go at the first option in if statement (B)
Go to the line break; (C)
Never reaches the StartCoroutine (D) > that is why it does not activate again.
If you try and put a StartCoroutine(RandomObstacleSpawn());
before the break;
you probably will get an Unity crash. What if you take off the while statement at all? You need to adjust time of yield tho.
This is the code I would use:
IEnumerator RandomObstacleSpawn(){
yield return new WaitForSeconds(Random.Range(3.5f,4.5f));
int index = Random.Range (0, listname.Count);
if(!listname[index].activeInHierarchy){
listname[index].SetActive(true);
listname[index].transform.position = new Vector3(transform.position.x,transform.position.y,-2f);
}else{
index = Random.Range(0,listname.Count);
}
StartCoroutine(RandomObstacleSpawn());
}
EDIT: about the while(true) I've manage to find more information about this concept here: R, How does while (TRUE) work?
But still... the break;
on the code is really making the Access to StartCoroutine(RandomObstacleSpawn());
unreachable .
这篇关于预制多年平均值激活时碰撞对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!