嗨,我正在创造一个2D无止境的跑步。背景有两个动画--滚动和stopScroll,当角色碰撞和死亡时,我想做以下操作
请帮帮忙!
这是我建议的更新代码
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.name == "Obstacle(Clone)")
{
StartCoroutine (DoMyThings(other.gameObject, this.gameObject, false));
}
}
IEnumerator DoMyThings(GameObject obstacle, GameObject player, bool ninjaObjBool)
{
ninjaObj = ninjaObjBool;
Destroy (obstacle);
animator.SetBool("dead", true);
yield return new WaitForSeconds(1.2f);
Destroy (player);
Time.timeScale=0;
//timerIsStopped = true;
yield break;
}
背景动画我复制了一个bg精灵,并将它们并排排列。RHS精灵是LHS雪碧在等级体系中的产物。然后我点击LHS精灵->窗口->动画。用加法曲线对X轴上的bg进行变换,得到它的无限运动。
发布于 2015-02-12 02:46:35
首先,在Update()中查找游戏对象不是一个好做法。创建它的一个实例可能是预期的。你可以这样做-
private Ninja ninjaClass;
.....
void Awake(){ //You can do it in Start() too if there is no problem it causes
ninjaClass = GameObject.Find("Ninja").GetComponent<Ninja>();
}
//Now in Update(),
void Update(){
if(!ninjaClass.ninjaObj){
animator.SetBool("stopScroll", true);
}
}
现在,OnCollisionEnter2D()将设置Time.timeScale =0,这将停止场景中每个随时间变化的游戏对象(这对暂停游戏很有好处)。执行发生的事情有很多种方法(1.2.3.4)。如果您提供代码来显示您是如何动画和使用计时器,这将是更好的。但正如你提到的,我会给你举个例子-
float timer = 0.0f;
float bool timeIsStopped = false;
.........
void Update(){
if(!timeIsStopped){timer += Time.deltaTime;}
}
void OnCollisionEnter2D(Collision2D other){
if (other.gameObject.name == "Obstacle(Clone)")
{
StartCoroutine(DoMyThings(other.gameObject, this.gameObject, false));
}
}
IEnumerator DoMyThings(GameObject obstacle, GameObject player, bool ninjaObjBool){
ninjaObj = ninjaObjBool;
yield return new WaitForSeconds(1.0f);
animator.SetBool("dead", true);
yield return new WaitForSeconds(1.5f);
Destroy(obstacle);
yield return new WaitForSeconds(2.0f);
timeIsStopped = true;
yield return new WaitForSeconds(0.5f);
Destroy(player);
yield break;
}
希望它能帮助你了解如何实现你的代码。
https://stackoverflow.com/questions/28467966
复制