Unity2D:敌人在向下移动时忽略碰撞

时间:2018-04-12 16:46:49

标签: unity3d collision

我的游戏是一个2D自上而下的平台,我有一个在舞台上下移动的僵尸敌人,我可以放置障碍物(如创造物)挡住敌人的方式,敌人有躲闪的能力任何随机时刻都有这些障碍。如果敌人与创造物相撞,敌人将停止其动作以打破创造。一旦创造被打破,敌人将继续向上移动,击中B点然后将向后移动到舞台上。我的问题:如果敌人向左躲闪(例如)然后继续向下移动并通过另一个创造,敌人将忽略碰撞并且其功能将停止并打破创造。我该如何解决这个问题?

public bool hitCollider = false;
public bool hitCollider2 = false;
public bool canMove = true;
public bool moveDown = false;
public bool isMovingUp = true;
public bool isMovingDown = false;
public bool isSomethere = false;
public bool ifMoving;

public float velocidadMax;

public float xMax;
public float xMin;
public float delay;

public float x;
private float tiempo;
public float countDown, countUp, startTime;

void Start () {
    Bcol = GetComponent<BoxCollider2D> ();
    Bcol.isTrigger = true;
    isMovingUp = true;
    rigid = GetComponent <Rigidbody2D> ();
    velocidadMax = 0.12f;
    x = velocidadMax;
    InvokeRepeating ("turnFalse", startTime, countDown); // changing lanes
    InvokeRepeating ("turnTrue", 0, countUp); // moving up or down 
    ifMoving = true;
    movedownSpeed = -4f;
    moveupSpeed = 4f;
    hascollidedwHouse = false;
}

void Update () {

    if (moveDown == false) {
        rigid.velocity = new Vector2 (0, moveupSpeed); // moving up
        if (ifMoving == true) {

            //irrelevant - don't need to know code here! 

        } else if (ifMoving == false) {
            movedownSpeed = -4;
            moveupSpeed = 0;
            Move ();
        }

    }

    if (moveDown == true) {
        if (hitMascot2 == true) {

        } else if (hitMascot2 == false) {
            movingDown = true;
            Bcol.isTrigger = true;

            StartCoroutine (waitMove ());
            StartCoroutine (destroyObj ()); 
        }
    }

    if (isMovingDown == true) {
        Debug.Log ("MovingDown");
        Bcol.isTrigger = true;

        if (canMove == false) {
            Debug.Log ("is False" + canMove);  
            moveupSpeed = 4f;
        } 

        hitCollider = false;
        hitCollider2 = false;
    }

if (hitCollider == true) {
        StartCoroutine (wait ());
        Debug.Log ("I HAVE COLLIDED");
        if (canMove == true) {
            movedownSpeed = 0f;
        }
        if (canMove == false) {
            movedownSpeed = -4f;
        }
        moveupSpeed = 0f;
    }

    if (hitCollider2 == true) {
        StartCoroutine (wait2 ());
        Debug.Log ("I HAVE COLLIDED");
        if (canMove == true) {
            movedownSpeed = 0f;
        }
        if (canMove == false) {
            movedownSpeed = -4f;
        }
        moveupSpeed = 0f;
    }
}


void OnTriggerEnter2D (Collider2D col) {

    if (col.tag == "objects") {
        hitCollider = true;
        woodBox = true;
        canMove = false;
        moveDown = false;
        xMax = 0;
        xMin = 0;
        if (hascollidedwHouse == true) {
            moveDown = false;
            isMovingDown = false;
            movedownSpeed = 0;
            moveupSpeed = 0;
            hitCollider = false;
            ifMoving = false;
            xMax = 0;
            xMin = 0;
            StartCoroutine (waitDown ());
        }
    }

    if (col.tag == "objects2") {
        hitCollider2 = true;
        metalBox = true;
        canMove = false;
        hitwoodenC = false;
        hitmetalC = true;
        moveDown = false;
        xMax = 0;
        xMin = 0;
        if (hascollidedwHouse == true) {
            moveDown = false;
            isMovingDown = false;
            movedownSpeed = 0;
            moveupSpeed = 0;
            ifMoving = false;
            hitCollider2 = false;
            xMax = 0;
            xMin = 0;
            StartCoroutine (waitDown2 ());
        }
    }

    if (col.tag == "pointB") {
        if (ifMoving == false) {
            moveupSpeed = 0;
            movedownSpeed = -4;

        } else if (ifMoving == true) {
            moveupSpeed = 0;
            movedownSpeed = -4;

        }
        Debug.Log ("Collided");
        moveDown = true;
        isMovingDown = true;
        isMovingUp = false;
        canMove = false;
        hascollidedwHouse = true;
}

void Move () {
    if (isSomethere == false) {
        if (isMovingUp == true) {
            tiempo += Time.deltaTime;
            if (transform.localPosition.x < xMin) {
                x = Random.Range (0.0f, velocidadMax);
                x = velocidadMax;
                tiempo = 0.0f; 
                Debug.Log ("Left");
            }
            if (tiempo > 1.0f) { //1.0   0.3
                x = -velocidadMax;
                tiempo = 0.0f;
                Debug.Log ("Right");
            }
            transform.localPosition = new Vector3 (transform.localPosition.x + x, transform.localPosition.y);
        }
    } else if (isSomethere == true) {
        movedownSpeed = -4;
        moveupSpeed = 4;
        if (iSPdeath == true) {
            movedownSpeed = 0;
            moveupSpeed = 0;
        }
    }
}

void turnFalse ()
{
    if( hitCollider == false) {
        ifMoving = false; // moves left and right
    }

    if( hitCollider2 == false) {
        ifMoving = false; // moves left and right
    }
}
void turnTrue() {
    if( hitCollider == false) {
        ifMoving = true; // moves up and down
    }

    if( hitCollider2 == false) {
        ifMoving = true; // moves up and down
    }
}


IEnumerator wait() {

    yield return new WaitForSeconds (2.5f);
    hitCollider = false; 

    if (isMovingUp == true) {
        canMove = true;
    } 

    if (isMovingUp == false) {
        canMove = false;
        moveupSpeed = 0f;
        movedownSpeed = -4f;

    } 

    if (canMove == true) {
        moveupSpeed = 4f;
    }
}

IEnumerator wait2() {
    yield return new WaitForSeconds (5.2f);
    hitCollider = false; 
    if (isMovingUp == true) {
        canMove = true;
    } 

    if (isMovingUp == false) {
        canMove = false;
        moveupSpeed = 0f;
        movedownSpeed = -4f;

    } 

    if (canMove == true) {
        moveupSpeed = 4f;
    }
}

IEnumerator waitDown() {
    yield return new WaitForSeconds (2.5f);
    movedownSpeed = -4f;
    moveupSpeed = 0f;
    moveDown = true;
}

IEnumerator waitDown2() {
    yield return new WaitForSeconds (5.2f);
    movedownSpeed = -4f;
    moveupSpeed = 0f;
    moveDown = true;
}

}

0 个答案:

没有答案
相关问题