Exemple #1
0
    // Update is called once per frame
    void FixedUpdate()
    {
        OwnRigid playerBody = player.GetComponent <OwnRigid>();

        //  X axis of the camera will be always the same as x axis of the player, so we can keep track the player along the map
        transform.position = new Vector3(playerBody.gameObject.transform.position.x, transform.position.y, transform.position.z);
    }
    // Collision
    void Collide(OwnRigid a, OwnRigid b)
    {
        Vector2 normal = Vector2.down;            // Normal vector
        Vector2 rv     = b.velocity - a.velocity; // Relative velocity from player to object
        float   VAN    = Vector2.Dot(rv, normal); // Vector along normal

        if (VAN > 0)                              // <--- Bounce ( commenting will stop the bounce )
        {
            return;
        }

        float j = -2.0f * VAN;                              // e = 1; it need some bounce

        j = j / (1 / a.mass + 1 / b.mass);                  // bounce increase every time 2 object collide

        Vector2 imp     = j * normal;                       // calculate impulse along  normal
        Vector3 impulse = new Vector3(imp.x, imp.y, 0.0f);  // Transform impulse from Vector2 to Vector3

        a.velocity -= 1 / a.mass * impulse;                 // apply the impulse

        something = impulse;                                // DISPLAY IMPULSE ( just for test )

        //if (impulse.x == 0.0f)
        if (impulse.y > -2.5)                               // if bounce stop
        {
            a.velocity.y = gravity * Time.deltaTime;        // keep the player above the grond
        }
        //a.velocity = -a.velocity;
    }
    // Update is called once per frame
    void FixedUpdate()
    {
        OwnRigid playerBody = player.GetComponent <OwnRigid>();
        OwnRigid bomb       = gameObject.GetComponent <OwnRigid>();

        // If player body and coin body collide, remove coin and increase score
        if (AABBvsAABB(playerBody.boundary, bomb.boundary))
        {
            playerBody.gameObject.SetActive(false);
        }
    }
    // Update is called once per frame
    void FixedUpdate()
    {
        // Take bodies
        OwnRigid playerBody = player.GetComponent <OwnRigid>();
        OwnRigid pickBody   = gameObject.GetComponent <OwnRigid>();
        Phisics  pEngine    = engine.GetComponent <Phisics>();

        // If player body and coin body collide, remove coin and increase score
        if (AABBvsAABB(playerBody.boundary, pickBody.boundary))
        {
            gameObject.SetActive(false);
            pEngine.count++;
            pEngine.countText.text = "Score: " + pEngine.count.ToString();
        }
    }
Exemple #5
0
    // Update is called once per frame
    void FixedUpdate()
    {
        OwnRigid playerBody = player.GetComponent <OwnRigid>();
        OwnRigid end        = gameObject.GetComponent <OwnRigid>();
        Phisics  pEngine    = engine.GetComponent <Phisics>();


        if (AABBvsAABB(playerBody.boundary, end.boundary))
        {
            if (pEngine.count > 5)
            {
                pEngine.message.gameObject.SetActive(true);
                pEngine.next.gameObject.SetActive(true);
            }
        }
    }
Exemple #6
0
    // Update is called once per frame
    void FixedUpdate()
    {
        // Get the components
        movement = GetComponent <PlayerController>();
        OwnRigid playerBody = player.GetComponent <OwnRigid>();

        OwnRigid[] solidBody  = new OwnRigid[solid.Length];
        OwnRigid[] stairsBody = new OwnRigid[stairs.Length];

        for (int i = 0; i < solid.Length; i++)
        {
            solidBody[i] = solid[i].GetComponent <OwnRigid>();
        }

        for (int i = 0; i < stairs.Length; i++)
        {
            stairsBody[i] = stairs[i].GetComponent <OwnRigid>();
        }

        // Create gravity for player
        if (isGravity)
        {
            Gravity(playerBody);
        }

        //float counter = 0;

        // check collision and stop the gravity
        foreach (OwnRigid s in solidBody)
        {
            bool colisionHere;
            colisionHere = AABBvsAABB(playerBody.boundary, s.boundary);

            // apply colision here
            if (colisionHere)
            {
                Collide(playerBody, s);
            }
        }

        // for each stair, check collision
        foreach (OwnRigid st in stairsBody)
        {
            stairsBool = AABBvsAABB(playerBody.boundary, st.boundary);
        }
    }
Exemple #7
0
    // Update is called once per frame
    void FixedUpdate()
    {
        // Get components
        OwnRigid playerBody = GetComponent <OwnRigid>();
        Phisics  handler    = engine.GetComponent <Phisics>();


        // Left
        if (Input.GetKey(KeyCode.A))
        {
            playerBody.velocity.x += -speed * Time.deltaTime;
        }

        // Right
        if (Input.GetKey(KeyCode.D))
        {
            playerBody.velocity.x += +speed * Time.deltaTime;
        }

        // Release Movement Left or Right
        if (Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.D))
        {
            playerBody.velocity.x = 0.0f;
        }

        // if not in air, player can jump
        if (handler.grounded)
        {
            JumpSpeed = 200;

            if (Input.GetKeyDown(KeyCode.Space))
            {
                playerBody.velocity.y += JumpSpeed * Time.deltaTime;
            }
            if (JumpSpeed == 200)
            {
                handler.grounded = false;
            }
            if (handler.grounded == false)
            {
                JumpSpeed = 0;
            }
        }
    }
Exemple #8
0
 // Gravity
 public void Gravity(OwnRigid a)
 {
     a.velocity.y += -gravity * Time.deltaTime;
 }
Exemple #9
0
    // Collision
    void Collide(OwnRigid a, OwnRigid b)
    {
        Vector2 normal;

        // check collision and set normal vector
        if (Coltipe == "down")
        {
            normal = Vector2.down;
        }
        else if (Coltipe == "up")
        {
            normal = Vector2.up;
        }
        else if (Coltipe == "right")
        {
            normal = Vector2.right;
        }
        else if (Coltipe == "left")
        {
            normal = Vector2.left;
        }

        else
        {
            normal = Vector2.up;
        }

        Vector2 rv  = b.velocity - a.velocity;              // Relative velocity from player to object
        float   VAN = Vector2.Dot(rv, normal);              // Vector along normal

        if (VAN > 0)
        {
            return;
        }

        float j = -1.5f * VAN;                              // e = 0.5; it need some bounce

        j = j / (1 / a.mass + 1 / b.mass);                  // bounce decrease every time 2 object collide

        Vector2 imp     = j * normal;                       // calculate impulse along  normal
        Vector3 impulse = new Vector3(imp.x, imp.y, 0.0f);  // Transform impulse from Vector2 to Vector3

        a.velocity -= 1 / a.mass * impulse;                 // apply the impulse

        //something = impulse;                                // DISPLAY IMPULSE ( just for test )

        // manypulate velocity for each case of collision
        if (impulse.y > -4.0)                              // if bounce stop
        {
            if (Coltipe == "down")
            {
                a.velocity.y = gravity * Time.deltaTime;
            }
            else if (Coltipe == "right")
            {
                a.velocity = Vector3.left * movement.speed * Time.deltaTime;
            }
            else if (Coltipe == "left")
            {
                a.velocity = Vector3.left * (-movement.speed) * Time.deltaTime;
            }
        }
    }
 // Gravity
 public void Gravity(OwnRigid a)
 {
     a.velocity.y += -gravity * Time.deltaTime;
     a.gameObject.transform.position += a.velocity * Time.deltaTime;
 }