2015-12-15 58 views
2

我有一段代码试图让玩家只通过点击在2个盒子之间跳跃立方体1(位于左侧)和立方体2(在右侧)。团结:玩家跳跃问题

Move()函数的问题在于它开始从多维数据集1跳到多维数据集2,然后多维数据集2再次跳转到多维数据集1,但是从这一点开始,玩家从多维数据集1跳到左边的对面立方体2.

跳转功能正在工作,但我认为逻辑错误。

移动:

void Move(){ 
    int i = 0; 
    while ((isGrounded == true) && (i < 10)) { 

     if(atCube1 == true){ 
      JumpRight(); 
     } 

     if(atCube2 == true){ 
      JumpLeft(); 
     } 

     i++; 
    } 
} 

OnCollisionEnter:

void OnCollisionEnter (Collision col) 
{ 
    Debug.Log("OnCollisionEnter"); 


    if (col.gameObject.name == "Cube 1"){ 
     Debug.Log ("++++++ C U B E 1 H I T ++++++++"); 

     atCube1 = true; 
     isGrounded = true; 

    } 

    if(col.gameObject.name == "Cube 2"){ 

     Debug.Log ("Cube 2 hit"); 
     atCube2 = true; 
     isGrounded = true; 

    } 
} 
+1

我修复了您的格式。两件事:1)你不需要'==真'。 2)如果'atCube1'和'atCube2'都是真的? –

+0

您的JumpRight功能是否重置atCube1和isGrounded字段值? –

+0

嗨@Wai,感谢您回应,我不认为atCube1和2可以在同一时间正确,因为这是在OnCollisionEnter功能,请参见下面的它,\t无效OnCollisionEnter(冲突COL) \t { \t \t调试.LOG( “OnCollisionEnter”); \t \t \t 如果\t(col.gameObject.name == “立方1”){ \t \t \t的debug.log( “++++++ CUBE 1个HIT ++++++++”) ; \t \t \t atCube1 = true; \t \t \t isGrounded = true; \t \t \t } \t \t \t如果(col.gameObject.name == “立方2”){ \t \t \t调试。日志(“Cube 2 hit”); \t \t \t atCube2 = true; \t \t \t isGrounded = true; \t \t \t \t \t}} – tony2016

回答

0

你应该atCube1 = false;后留下cube1(用于CUBE2做到这一点)。当它不碰撞任何东西时,您也可以添加isGrounded = false;

1 - 你开始在cube1,atCube1 = true

2跳转到CUBE2,atCube1 = trueatCube2 = true

3-跳转到再次cube1,它跳到左边,因为仍atCube2 = true。事实上,它首先跳转,然后跳转,因为你的订单号码为if。只需将false的值添加到布尔值来修复它。