public void CheckForObjects()
    {
        LayerMask[] mascaras = new LayerMask[3];
        mascaras[0] = LayerMask.GetMask("Platform");
        mascaras[1] = LayerMask.GetMask("Enemy");
        mascaras[2] = LayerMask.GetMask("Platform");


        RaycastHit2D hit2D = PlayerUtilsStatic.RayCastArrayMask(transform.position - new Vector3(0, 0.5f, 0), Vector3.down, 0.05f, mascaras);

        if (hit2D)
        {
            Debug.Log("Trying to grab" + hit2D.collider.gameObject);
            GrabObject(hit2D.collider.gameObject);
            Debug.DrawRay(transform.position - new Vector3(0f, 0.5f, 0f), Vector2.down);
        }
        else
        {
            hit2D = Physics2D.Raycast(transform.position - new Vector3(0, 0.5f, 0), Vector3.down, 0.05f, LayerMask.GetMask("Walkable"));
            if (hit2D)
            {
                print("Bruh");
                if (GetComponentInParent <CanBeGrabbed>() != null)
                {
                    GrabObject(hit2D.collider.gameObject);
                }
            }
        }
    }
    public override void Update(Agent a, float dt) {

        Trampler agentScript = a.GetComponent<Trampler>();


        RaycastHit2D hitWallRight = PlayerUtilsStatic.RayCastArrayMask(a.transform.position+ new Vector3(0.5f, 0, 0), Vector2.right,.1f,a.GetComponent<Trampler>().masks);
        RaycastHit2D hitWallLeft = PlayerUtilsStatic.RayCastArrayMask(a.transform.position+new Vector3(-0.5f,0,0), Vector2.left, .1f, a.GetComponent<Trampler>().masks);

        //Debug.DrawRay(a.transform.position + new Vector3(0.5f, 0, 0), Vector2.right* .1f);

        if ((hitWallRight && agentScript.currentSpeed>0)|| (hitWallLeft&& agentScript.currentSpeed < 0)) {
            a.SwitchState(0, new TramplerStunnedState());
            Debug.Log("HITWALL");
        }

        if (a.touchedByPlayer) {
            a.touchedByPlayer = false;
            GameLogic.instance.KillPlayer();
        }

        if (a.GetComponent<Trampler>().whereTo == 0) {
            //La distancia entre el punto A y el objeto (en X) es superior a Threshold, por lo que debe seguir moviendose
            //Debug.Log(Mathf.Abs((a.transform.position.x - a.GetComponent<Trampler>().pointA.x)));
            if (Mathf.Abs((a.transform.position.x - a.GetComponent<Trampler>().pointA.x)) > threshold && a.GetComponent<Trampler>().mustStop == false) {
                // a.GetComponent<Rigidbody2D>().velocity = new Vector2(Mathf.Clamp(a.GetComponent<Rigidbody2D>().velocity.x - accelerationRate * Time.deltaTime, -a.GetComponent<Trampler>().maxSpeed, 0),0);

                agentScript.currentSpeed -= (accelerationRate * Time.deltaTime);
                agentScript.currentSpeed = Mathf.Clamp(agentScript.currentSpeed, -agentScript.maxSpeed, 0);

                a.GetComponent<Rigidbody2D>().velocity = new Vector2(agentScript.currentSpeed, 0);//new Vector2(a.GetComponent<Rigidbody2D>().velocity.x - accelerationRate * Time.deltaTime, 0);

                //Vector2 velocity = a.GetComponent<Rigidbody2D>().velocity;
                //Debug.Log(accelerationRate * Time.deltaTime);


            } else {
                agentScript.mustStop = true;

                //Debug.Log("arrived");
                //Llega a A, se cansa 
                agentScript.currentSpeed -= agentScript.currentSpeed * Time.deltaTime*1.5f;

                a.GetComponent<Rigidbody2D>().velocity = new Vector2(agentScript.currentSpeed,0);


                if (a.GetComponent<Rigidbody2D>().velocity.x>=-0.2f) {
                    a.SwitchState(0, new TramplerStunnedState());
                }

            }

        }else if (agentScript.whereTo == 1){

            //La distancia entre el punto B y el objeto (en X) es superior a Threshold, por lo que debe seguir moviendose
            if (Mathf.Abs((a.transform.position.x - a.GetComponent<Trampler>().pointB.x)) > threshold&& a.GetComponent<Trampler>().mustStop == false) {

                agentScript.currentSpeed += (accelerationRate * Time.deltaTime);
                agentScript.currentSpeed = Mathf.Clamp(agentScript.currentSpeed, 0, agentScript.maxSpeed);

                a.GetComponent<Rigidbody2D>().velocity = new Vector2(agentScript.currentSpeed, 0);//new Vector2(a.GetComponent<Rigidbody2D>().velocity.x - accelerationRate * Time.deltaTime, 0);
            } else {
                agentScript.mustStop = true;

                //Llega a B, se cansa 

                agentScript.currentSpeed -= agentScript.currentSpeed * Time.deltaTime*1.5f;

                a.GetComponent<Rigidbody2D>().velocity = new Vector2(agentScript.currentSpeed, 0);

                if (a.GetComponent<Rigidbody2D>().velocity.x <= 0.2f) {
                    a.SwitchState(0, new TramplerStunnedState());
                }

            }
        } else {
            //a.SwitchState(0, new TramplerIdleState());
        }


    }
Beispiel #3
0
    //Método que comprueba si la velocidad y del personaje es 0 o aprox. y actualiza el booleano grounded en consecuencia
    public void CheckGrounded(PlayerController player)
    {
        LayerMask[] mascaras = new LayerMask[1];
        mascaras[0] = LayerMask.GetMask("Walkable");
        //mascaras[1] = LayerMask.GetMask("Ground");
        //mascaras[2] = LayerMask.GetMask("Enemy");


        RaycastHit2D hit2D      = PlayerUtilsStatic.RayCastArrayMask(transform.position, Vector3.down, 0.05f, mascaras);
        RaycastHit2D hit2DLeft  = PlayerUtilsStatic.RayCastArrayMask(transform.position + new Vector3(-player.distanciaBordeSprite, 0, 0), Vector3.down, 0.05f, mascaras);
        RaycastHit2D hit2DRight = PlayerUtilsStatic.RayCastArrayMask(transform.position + new Vector3(player.distanciaBordeSprite, 0, 0), Vector3.down, 0.05f, mascaras);

        Debug.DrawLine(transform.position, Vector3.down * 0.05f);
        Debug.DrawLine(transform.position + new Vector3(-player.distanciaBordeSprite, 0, 0), Vector3.down * 0.05f);
        Debug.DrawLine(transform.position + new Vector3(player.distanciaBordeSprite, 0, 0), Vector3.down * 0.05f);


        //if (hit2D.collider != null)
        //Debug.Log(hit2D.collider);

        //RaycastHit2D hit2D = Physics2D.Raycast(rb.position - new Vector2(0f, 0.5f), Vector2.down, 0.1f, groundMask);
        //RaycastHit2D hit2DLeft = Physics2D.Raycast(rb.position - new Vector2(0f, 0.5f) + new Vector2(-distanciaBordeSprite, 0), Vector2.down, 0.1f, groundMask);
        //RaycastHit2D hit2DRight = Physics2D.Raycast(rb.position - new Vector2(0f, 0.5f) + new Vector2(distanciaBordeSprite, 0), Vector2.down, 0.1f, groundMask);
        // If the raycast hit something
        if (hit2D || hit2DLeft || hit2DRight)
        {
            //if (player.GetComponent<Rigidbody2D>().velocity.y == 0) {

            if (!player.grounded)
            {
                player.grounded = true;
                //player.dashing = false;
                player.SetCanDash(true);
            }
            //}
        }
        else
        {
            player.grounded = false;
        }
        RaycastHit2D hit2DLeftO  = Physics2D.Raycast(player.rb.position + new Vector2(-player.distanciaBordeSprite, 0), Vector2.down, 0.1f, player.groundMask);
        RaycastHit2D hit2DRightO = Physics2D.Raycast(player.rb.position + new Vector2(player.distanciaBordeSprite, 0), Vector2.down, 0.1f, player.groundMask);

        if (!hit2DLeftO && !hit2DRightO)
        {
            bool right = true;

            //SLIDE PA LA DERECHA
            hit2DRightO = Physics2D.Raycast(player.rb.position + new Vector2(player.distanciaBordeSprite, 0), Vector2.down, 3.0f, player.slideMask);
            hit2DLeftO  = Physics2D.Raycast(player.rb.position + new Vector2(-player.distanciaBordeSprite, 0), Vector2.down, 0.2f, player.slideMask);


            if (!(hit2DLeftO && hit2DRightO))
            {
                right = false;
                //SLIDE PA LA IZQUIERDA
                hit2DRightO = Physics2D.Raycast(player.rb.position + new Vector2(player.distanciaBordeSprite, 0), Vector2.down, 0.2f, player.slideMask);
                hit2DLeftO  = Physics2D.Raycast(player.rb.position + new Vector2(-player.distanciaBordeSprite, 0), Vector2.down, 3.0f, player.slideMask);
            }
            if (hit2DLeftO && hit2DRightO)
            {
                if (!player.facingRight && right)
                {
                    player.Flip();
                    player.prevHorizontalMov = 1.0f;
                }
                else if (player.facingRight && !right)
                {
                    player.Flip();
                    player.prevHorizontalMov = -1.0f;
                }
                //Debug.Log(hit2DLeftO.collider.gameObject.transform.rotation.z);
                if (player.timeNotSliding > 0.3f)
                {
                    player.sliding        = true;
                    player.rb.velocity    = new Vector2(player.rb.velocity.x, -10);
                    player.timeNotSliding = 0;
                }
                //Debug.Log("BothHit");
            }
            else
            {
                player.sliding = false;
            }
        }
        else
        {
            player.sliding = false;
        }
    }