Ejemplo n.º 1
0
    private void sprintTiredness()
    {
        ExhaustionBar tired = GameObject.FindObjectOfType <ExhaustionBar>();

        tired.ReduceExhaustion(5f * Time.deltaTime);
        //print(4f * Time.deltaTime);
    }
Ejemplo n.º 2
0
 private void Awake()
 {
     instance = this;
     myExhaustion.Initialize();
 }
Ejemplo n.º 3
0
    void FixedUpdate()
    {
        if (GameObject.FindObjectOfType <DummyHealthBar>().GetHealth() <= 0)
        {
            rb.velocity = new Vector3(0, 0, 0);
            if (alive == true)
            {
                StartCoroutine(FallDown());
                alive = false;
            }
        }
        if (!Input.GetKey(KeyCode.LeftShift))
        {
            anim.SetBool("Shift", false);
        }
        else
        {
            anim.SetBool("Shift", true);
        }

        if (grounded)
        {
            // Calculate how fast we should be moving
            Vector3 targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
            targetVelocity  = transform.TransformDirection(targetVelocity);
            targetVelocity *= speed;

            // Apply a force that attempts to reach our target velocity
            Vector3 velocity       = rb.velocity;
            Vector3 velocityChange = (targetVelocity - velocity);
            velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
            velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
            velocityChange.y = 0;
            if (!anim.GetBool("Shift"))
            {
                velocityChange /= 15;
            }
            if ((targetVelocity.x != 0 || targetVelocity.y != 0) && anim.GetBool("Shift"))
            {
                sprintTiredness();
            }
            rb.AddForce(velocityChange, ForceMode.VelocityChange);

            // Jump
            if (canJump && Input.GetButton("Jump"))
            {
                ExhaustionBar tired = GameObject.FindObjectOfType <ExhaustionBar>();
                tired.ReduceExhaustion(2f);
                ac.Jump();
                rb.velocity = new Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
                grounded    = false;
                anim.SetTrigger("Jump");
                anim.ResetTrigger("Land");
            }
        }
        else
        {
            anim.SetTrigger("Jump");
        }
        rb.AddForce(new Vector3(0, -gravity * rb.mass, 0));

        moveSpeed = transform.InverseTransformDirection(rb.velocity).z;
        anim.SetFloat("MoveSpeed", moveSpeed);


        if (collisions <= 0)
        {
            if (frames > 120)
            {
                anim.SetTrigger("Jump");
                anim.ResetTrigger("Land");
                frames = 0;
            }
            else
            {
                frames++;
            }
        }

        if (Input.GetKeyDown("f"))
        {
            GameObject[] chests;
            chests = GameObject.FindGameObjectsWithTag("Chest");
            GameObject closestChest     = null;
            float      smallestDistance = Mathf.Infinity;
            foreach (GameObject chest in chests)
            {
                float distance = Vector3.Distance(transform.position, chest.transform.position);
                if (distance < smallestDistance)
                {
                    closestChest     = chest;
                    smallestDistance = distance;
                }
            }
            if (closestChest != null && smallestDistance <= 5f)
            {
                closestChest.GetComponent <Chest>().Open();
            }
        }
    }