//Here we check if the player is jumping or moving away from the wall or ground.
    void OnCollisionExit2D(Collision2D coll)
    {
        animatorController.SetBool("Pushing", false);
        OnCollisionStayCounter = 0;
        foreach (ContactPoint2D contact in coll.contacts)
        {
            int CountHappenings     = 0;
            int CountHappeningsWALL = 0;
            foreach (GameObject GroundedObject in GroundedToObjectsList)
            {
                if (contact.collider.gameObject.GetInstanceID() == GroundedObject.GetInstanceID())
                {
                    CountHappenings += 1;
                }
            }
            foreach (GameObject WalledObject in WalledToObjectsList)
            {
                if (contact.collider.gameObject.GetInstanceID() == WalledObject.GetInstanceID())
                {
                    CountHappeningsWALL += 1;
                }
            }

            //was the object one of the grounded to objects?
            if (CountHappenings > 0)
            {
                GroundedToObjectsList.Remove(contact.collider.gameObject);
                if (GroundedToObjectsList.Count == 0)
                {
                    DJ_available          = true;
                    isGrounded            = false;
                    this.transform.parent = null;
                    FixStateTimer         = 0;
                }
            }

            //was the object one of the wall?
            if (CountHappeningsWALL > 0)
            {
                WalledToObjectsList.Remove(contact.collider.gameObject);
                if (WalledToObjectsList.Count == 0)
                {
                    if (NoNeedForSafeJump_bool == false)
                    {
                        //This makes the walljump a bit easier. Player is able to do the wall jump even few miliseconds after he let go of the wall.
                        walljump_count = 0.16f;
                    }
                    NoNeedForSafeJump_bool         = false;
                    DJ_available                   = true;
                    this.transform.parent          = null;
                    isWallSliding                  = false;
                    WallGripParticles.emissionRate = 0;
                    FixStateTimer                  = 0;
                }
            }
        }
    }
Exemplo n.º 2
0
    void OnCollisionEnter2D(Collision2D coll)
    {
        if (coll.gameObject.CompareTag("Enemy"))
        {
            return;
        }

        //This makes sure Ninja doesn't slide from previous force when hitting platform. Unless player is holding Left or Right button.
        if (IsGrounded == false && Btn_Left_bool == false && Btn_Right_bool == false)
        {
            this.GetComponent <Rigidbody2D>().velocity = new Vector2(this.GetComponent <Rigidbody2D>().velocity.x * 0.25f, -0.01f);
        }
        else if (IsGrounded == false)
        {
            this.GetComponent <Rigidbody2D>().velocity = new Vector2(this.GetComponent <Rigidbody2D>().velocity.x, -0.01f);
        }

        OnCollisionStayCounter += 1;
        OnCollisionBugThreshold = 0;
        UpInTheAir_Counter      = 0;

        foreach (ContactPoint2D contact in coll.contacts)
        {
            //If Ninja hits his head to the roof. Stop Jump Force.
            if (0.1f > contact.normal.y && ((contact.normal.x * contact.normal.x) < (0.85f * 0.85f)))
            {
                JumpForceCount = 0f;
            }

            //If it wasn't the roof. Was it a ground perhaps?
            else if (contact.normal.x >= Ground_X_MIN && contact.normal.x <= Ground_X_MAX && contact.normal.y >= Ground_Y_MIN && contact.normal.y <= Ground_Y_MAX)
            {
                int CountHappenings = 0;
                foreach (GameObject GroundedObject in GroundedToObjectsList)
                {
                    if (contact.collider.gameObject.GetInstanceID() == GroundedObject.GetInstanceID())
                    {
                        CountHappenings += 1;
                    }
                }

                //Is the platform already listed in GroundedObjects? If not Add it to the list.
                if (CountHappenings == 0)
                {
                    DJ_available = false;
                    GroundedToObjectsList.Add(contact.collider.gameObject);
                    //Move NinjaPlatformRoot to the new platform.
                    this.transform.parent = null;
                    NinjaPlatformRoot.transform.position = contact.collider.gameObject.transform.position;
                    NinjaPlatformRoot.RootedTo           = contact.collider.gameObject;
                    this.transform.parent = NinjaPlatformRoot.transform;

                    IsGrounded = true;

                    this.GetComponent <Rigidbody2D>().AddForce(contact.normal * (-300f));

                    if (WallTouch == true)
                    {
                        WallGripParticles.emissionRate = 0;
                        FixStateTimer = 0;
                    }
                }

                //If it wasnt a roof or a ground it must have been wall. No need for Normal check anymore.
            }
            else
            {
                //Ninja must be faling downwards to grab the wall.
                if (this.GetComponent <Rigidbody2D>().velocity.y < 0f && IsGrounded == false)
                {
                    this.GetComponent <Rigidbody2D>().velocity = Vector2.zero;
                    //is the Object already listed in WalledObjects?
                    int CountHappenings = 0;
                    foreach (GameObject WalledObject in WalledToObjectsList)
                    {
                        if (contact.collider.gameObject.GetInstanceID() == WalledObject.GetInstanceID())
                        {
                            CountHappenings += 1;
                        }
                    }
                    //if not. Lets list it.
                    if (CountHappenings == 0)
                    {
                        DJ_available = false;
                        WalledToObjectsList.Add(contact.collider.gameObject);
                        this.transform.parent = null;
                        NinjaPlatformRoot.transform.position = contact.collider.gameObject.transform.position;
                        NinjaPlatformRoot.RootedTo           = contact.collider.gameObject;
                        this.transform.parent = NinjaPlatformRoot.transform;

                        WallTouch = true;

                        //Check that the player is facing to the right direction
                        if (contact.normal.x > 0)
                        {
                            PlayerLooksRight = true;
                            MySpriteOBJ.transform.localScale = MySpriteOriginalScale;
                        }
                        else
                        {
                            PlayerLooksRight = false;
                            MySpriteOBJ.transform.localScale = new Vector3(-MySpriteOriginalScale.x, MySpriteOriginalScale.y, MySpriteOriginalScale.z);
                        }

                        //Start emiting smoke particles when touching the wall
                        WallGripParticles.emissionRate = WallGripEmissionRate;
                    }
                }
            }
        }
    }
    void OnCollisionEnter2D(Collision2D coll)
    {
        if (coll.gameObject.CompareTag("Enemy"))
        {
            return;
        }

        //This makes sure character doesn't slide from previous force when hitting platform. Unless player is holding Left or Right button.
        if (isGrounded == false && leftInputActive == false && rightInputActive == false)
        {
            this.rigidbody2D.velocity = new Vector2(this.rigidbody2D.velocity.x * 0.25f, -0.01f);
            //this.rigidbody2D.velocity = new Vector2 (this.rigidbody2D.velocity.x * 0.0f, -0.01f);
        }
        else if (isGrounded == false)
        {
            this.rigidbody2D.velocity = new Vector2(this.rigidbody2D.velocity.x, -0.01f);
        }

        OnCollisionStayCounter += 1;
        OnCollisionBugThreshold = 0;
        UpInTheAir_Counter      = 0;

        foreach (ContactPoint2D contact in coll.contacts)
        {
            //If character hits his head to the roof. Stop Jump Force.
            if (0.1f > contact.normal.y && ((contact.normal.x * contact.normal.x) < (0.85f * 0.85f)))
            {
                JumpForceCount = 0f;
            }

            //If it wasn't the roof. Was it a ground perhaps?
            else if (contact.normal.x >= Ground_X_MIN && contact.normal.x <= Ground_X_MAX && contact.normal.y >= Ground_Y_MIN && contact.normal.y <= Ground_Y_MAX)
            {
                int CountHappenings = 0;
                foreach (GameObject GroundedObject in GroundedToObjectsList)
                {
                    if (contact.collider.gameObject.GetInstanceID() == GroundedObject.GetInstanceID())
                    {
                        CountHappenings += 1;
                    }
                }

                //Is the platform already listed in GroundedObjects? If not Add it to the list.
                if (CountHappenings == 0)
                {
                    DJ_available = false;
                    GroundedToObjectsList.Add(contact.collider.gameObject);
                    //Move PlatformRoot to the new platform.
                    this.transform.parent           = null;
                    PlatformRoot.transform.position = contact.collider.gameObject.transform.position;
                    PlatformRoot.RootedTo           = contact.collider.gameObject;
                    this.transform.parent           = PlatformRoot.transform;

                    isGrounded = true;

                    this.rigidbody2D.AddForce(contact.normal * (-300f));

                    if (isWallSliding == true)
                    {
                        WallGripParticles.emissionRate = 0;
                        FixStateTimer = 0;
                    }
                }

                //If it wasnt a roof or a ground it must have been wall. No need for Normal check anymore.
            }
            else
            {
                //Character must be faling downwards to grab the wall.
                if (this.rigidbody2D.velocity.y < 0f && isGrounded == false)
                {
                    this.rigidbody2D.velocity = Vector2.zero;
                    //is the Object already listed in WalledObjects?
                    int CountHappenings = 0;
                    foreach (GameObject WalledObject in WalledToObjectsList)
                    {
                        if (contact.collider.gameObject.GetInstanceID() == WalledObject.GetInstanceID())
                        {
                            CountHappenings += 1;
                        }
                    }
                    //if not. Lets list it.
                    if (CountHappenings == 0)
                    {
                        //print (contact.collider.tag) ;
                        if (contact.collider.tag != "Box")
                        {
                            DJ_available = false;
                            WalledToObjectsList.Add(contact.collider.gameObject);
                            this.transform.parent           = null;
                            PlatformRoot.transform.position = contact.collider.gameObject.transform.position;
                            PlatformRoot.RootedTo           = contact.collider.gameObject;
                            this.transform.parent           = PlatformRoot.transform;

                            isWallSliding = true;

                            //Check that the player is facing to the right direction
                            if (contact.normal.x > 0)
                            {
                                FaceRight();
                            }
                            else
                            {
                                FaceLeft();
                            }

                            //Start emiting smoke particles when touching the wall
                            WallGripParticles.emissionRate = WallGripEmissionRate;
                        }
                    }
                }
            }
        }
    }
Exemplo n.º 4
0
    void OnCollisionEnter2D(Collision2D coll)
    {
        if (coll.gameObject.tag == "Enemy")
        {
            GetComponent <AudioSource>().Play();
            GameObject go = Instantiate(dieParticle, transform.position, new Quaternion(0, 0, 0, 0)) as GameObject;
            go.transform.Rotate(new Vector3(270, 0, 0));
            Destroy(gameObject, 1f);
            GameObject.Find("NinjaSprites").GetComponent <SpriteRenderer>().enabled = false;
            GlobalValues.isPlayerRunning = false;
            playerIsDie = true;
            loseGamePanel.SetActive(true);
            int high = PlayerPrefs.GetInt("HIGH_SCORE", 0);
            if (jScore > high)
            {
                PlayerPrefs.SetInt("HIGH_SCORE", jScore);
            }
            highJ.GetComponent <Text>().text    = "   " + PlayerPrefs.GetInt("HIGH_SCORE", 0).ToString();
            currentJ.GetComponent <Text>().text = "   " + jScore.ToString();
        }
        else
        {
            //This makes sure Ninja doesn't slide from previous force when hitting platform. Unless player is holding Left or Right button.
            if (IsGrounded == false && Btn_Left_bool == false && Btn_Right_bool == false)
            {
                this.GetComponent <Rigidbody2D>().velocity = new Vector2(this.GetComponent <Rigidbody2D>().velocity.x * 0.25f, -0.01f);
            }
            else if (IsGrounded == false)
            {
                this.GetComponent <Rigidbody2D>().velocity = new Vector2(this.GetComponent <Rigidbody2D>().velocity.x, -0.01f);
            }

            OnCollisionStayCounter += 1;
            OnCollisionBugThreshold = 0;
            UpInTheAir_Counter      = 0;

            foreach (ContactPoint2D contact in coll.contacts)
            {
                //If Ninja hits his head to the roof. Stop Jump Force.
                if (0.1f > contact.normal.y && ((contact.normal.x * contact.normal.x) < (0.85f * 0.85f)))
                {
                    JumpForceCount = 0f;
                }

                //If it wasn't the roof. Was it a ground perhaps?
                else if (contact.normal.x >= Ground_X_MIN && contact.normal.x <= Ground_X_MAX && contact.normal.y >= Ground_Y_MIN && contact.normal.y <= Ground_Y_MAX)
                {
                    int CountHappenings = 0;
                    foreach (GameObject GroundedObject in GroundedToObjectsList)
                    {
                        if (contact.collider.gameObject.GetInstanceID() == GroundedObject.GetInstanceID())
                        {
                            CountHappenings += 1;
                        }
                    }

                    //Is the platform already listed in GroundedObjects? If not Add it to the list.
                    if (CountHappenings == 0)
                    {
                        DJ_available = false;
                        GroundedToObjectsList.Add(contact.collider.gameObject);
                        //Move NinjaPlatformRoot to the new platform.
                        this.transform.parent = null;
                        NinjaPlatformRoot.transform.position = contact.collider.gameObject.transform.position;
                        NinjaPlatformRoot.RootedTo           = contact.collider.gameObject;
                        this.transform.parent = NinjaPlatformRoot.transform;

                        IsGrounded = true;

                        this.GetComponent <Rigidbody2D>().AddForce(contact.normal * (-300f));

                        if (WallTouch == true)
                        {
                            WallGripParticles.emissionRate = 0;
                            FixStateTimer = 0;
                        }
                    }
                    //If it wasnt a roof or a ground it must have been wall. No need for Normal check anymore.
                }
                else
                {
                    //Ninja must be faling downwards to grab the wall.
                    if (this.GetComponent <Rigidbody2D>().velocity.y < 0f && IsGrounded == false)
                    {
                        this.GetComponent <Rigidbody2D>().velocity = Vector2.zero;
                        //is the Object already listed in WalledObjects?
                        int CountHappenings = 0;
                        foreach (GameObject WalledObject in WalledToObjectsList)
                        {
                            if (contact.collider.gameObject.GetInstanceID() == WalledObject.GetInstanceID())
                            {
                                CountHappenings += 1;
                            }
                        }
                        //if not. Lets list it.
                        if (CountHappenings == 0)
                        {
                            DJ_available = false;
                            WalledToObjectsList.Add(contact.collider.gameObject);
                            this.transform.parent = null;
                            NinjaPlatformRoot.transform.position = contact.collider.gameObject.transform.position;
                            NinjaPlatformRoot.RootedTo           = contact.collider.gameObject;
                            this.transform.parent = NinjaPlatformRoot.transform;

                            WallTouch = true;

                            //Check that the player is facing to the right direction
                            if (contact.normal.x > 0)
                            {
                                PlayerLooksRight = true;
                                MySpriteOBJ.transform.localScale = MySpriteOriginalScale;
                            }
                            else
                            {
                                PlayerLooksRight = false;
                                MySpriteOBJ.transform.localScale = new Vector3(-MySpriteOriginalScale.x, MySpriteOriginalScale.y, MySpriteOriginalScale.z);
                            }

                            //Start emiting smoke particles when touching the wall
                            WallGripParticles.emissionRate = WallGripEmissionRate;
                        }
                    }
                }
            }
        }
    }