Beispiel #1
0
 void CheckBoxCollision()
 {
     if (phys.collisions.collided != null && phys.collisions.collided.tag == "Box")
     {
         BoxController bc = phys.collisions.collided.GetComponent <BoxController> ();
         if (bc.velocity.magnitude == 0)
         {
             bc.Push(Collision(), pushPower);
         }
     }
 }
 private void OnTriggerStay2D(Collider2D collision)
 {
     if (collision.tag == "Player" && Input.GetKey(KeyCode.Space))
     {
         Direction d = Direction.None;
         if (direction == Direction.Left)
         {
             d = Direction.Right;
         }
         if (direction == Direction.Right)
         {
             d = Direction.Left;
         }
         box.Push(d);
     }
 }
Beispiel #3
0
    void FixedUpdate()
    {
        Debug.Log(gameObject.name + velocity);
        Debug.Log(gameObject.name + canBounce);
        againstWall = CheckWallCollision(velocity);
        //move this box
        phys.Move(velocity * Time.deltaTime);
        //check collision. If it's a box, move it accordingly
        BoxController hitBox = CheckBoxCollision();

        if (phys.collisions.above || phys.collisions.below || phys.collisions.right || phys.collisions.left)
        {
            if (hitBox != null)
            {
                float   bcmass   = hitBox.mass;
                Vector2 bcVel    = hitBox.velocity;
                float   bcVelMag = hitBox.velocity.magnitude;
                float   dot      = Vector2.Dot(bcVel, this.velocity);
                if (mass < bcmass && bcVelMag == 0)
                {
                    canBounce = true;
                    //box I hit is massive and stationary: I stop
                    velocity = Vector2.zero;
                }
                else if (mass < bcmass && bcVelMag != 0)
                {
                    canBounce = true;
                    if (dot <= 0)
                    {
                        float   velX      = velocity.x != 0 ? Mathf.Sign(velocity.x) : 0;
                        float   velY      = velocity.y != 0 ? Mathf.Sign(velocity.y) : 0;
                        Vector2 bulletDir = new Vector2(velX, velY);
                        float   bcVelX    = Mathf.Sign(bcVel.x);
                        float   bcVelY    = Mathf.Sign(bcVel.y);
                        bulletDir = new Vector2(velX + bcVelX, velY + bcVelY);
                        GameObject bulletInstance = GameObject.Instantiate(bullet.gameObject, gameObject.transform.position, gameObject.transform.rotation);
                        bulletInstance.gameObject.GetComponent <MoveBullet> ().SetVelocity(bulletDir.normalized);
                        bulletInstance.gameObject.GetComponent <MoveBullet> ().SetSpeed(Random.Range(5, 7));
                        DestroyImmediate(gameObject);
                    }
                }
                else if (mass >= bcmass && bcVelMag == 0 && !hitBox.againstWall)
                {
                    //box I hit is weak and stationary: Keep moving together
                    hitBox.Push(Collision(), velocity.magnitude * bcmass);
                }
                else if (mass > bcmass && bcVelMag != 0)
                {
                    canBounce = true;
                    //box I hit is weak and moving
                    if (dot <= 0)
                    {
                        float   velX      = velocity.x != 0 ? Mathf.Sign(velocity.x) : 0;
                        float   velY      = velocity.y != 0 ? Mathf.Sign(velocity.y) : 0;
                        Vector2 bulletDir = new Vector2(velX, velY);
                        float   bcVelX    = Mathf.Sign(bcVel.x);
                        float   bcVelY    = Mathf.Sign(bcVel.y);
                        bulletDir = new Vector2(velX + bcVelX, velY + bcVelY);
                        GameObject bulletInstance = GameObject.Instantiate(bullet.gameObject, hitBox.transform.position, hitBox.transform.rotation);
                        bulletInstance.gameObject.GetComponent <MoveBullet> ().SetVelocity(bulletDir.normalized);
                        bulletInstance.gameObject.GetComponent <MoveBullet> ().SetSpeed(Random.Range(5, 7));
                        DestroyImmediate(hitBox.gameObject);
                    }
                }
                else if (mass >= bcmass && hitBox.againstWall)
                {
                    velocity  = Vector2.zero;
                    canBounce = true;
                }
                else if (mass == bcmass && dot < 0)
                {
                    if (canBounce)
                    {
                        velocity        = -velocity;
                        hitBox.velocity = -bcVel;
                        canBounce       = !canBounce;
                    }
                    else
                    {
                        velocity = Vector2.zero;
                    }
                }
            }
            else if (hitBox == null)
            {
                canBounce = true;
                velocity  = Vector2.zero;
                wall      = Collision();
            }
            else
            {
                canBounce = true;
                Vector2 diff = velocity - velocity.magnitude * hitBox.wall;
                if (diff.magnitude == 0)
                {
                    velocity = Vector2.zero;
                }
            }
        }
        CheckPlayerCollision();
    }