Exemple #1
0
    public void Update()
    {
        if (!LevelManager.Level.CanPlay)
        {
            return;
        }

        float horizontal = Input.GetAxis("Horizontal");
        float vertical   = Input.GetAxis("Vertical");

        // Rotate character
        transform.Rotate(0, horizontal * turnSpeed * Time.deltaTime, 0);

        // Set speed
        float moveSpeed = speed;

        // Start Crouching
        if (Input.GetKeyDown("space"))
        {
            crouching = !crouching;
            animCharacter.SetBool("Crouch", crouching);
        }

        // Crouching
        if (crouching)
        {
            moveSpeed = crouchSpeed;
        }

        // Sprinting if not crouching
        if (!crouching && Input.GetKey(KeyCode.LeftShift))
        {
            Sprint = true;
            animCharacter.SetBool("Sprinting", true);
            moveSpeed = sprintingSpeed;
        }
        else
        {
            Sprint = false;
            animCharacter.SetBool("Sprinting", false);
        }

        // Calculate movement
        Vector3 vel = transform.forward * vertical * moveSpeed;

        if (controller.isGrounded)
        {
            vSpeed = 0;
        }
        vSpeed -= gravity * Time.deltaTime;
        vel.y   = vSpeed;

        // Move character
        controller.Move(vel * Time.deltaTime);

        // Walking animation
        if (vertical == 0)
        {
            animCharacter.SetBool("Walking", false);
        }
        else
        {
            animCharacter.SetBool("Walking", true);
        }

        // If player is near an item
        if (item != null)
        {
            if (Input.GetButtonDown("Pickup"))
            {
                // Use item when player uses the right input
                item.ItemAction();
                item = null;
            }
        }
        else if (Input.GetButtonDown("ThrowRock"))
        {
            Debug.Log("Throw rock");
        }

        if (Input.GetButtonDown("OpenInventory"))
        {
            Debug.Log("Open inventory");
        }

        if (guards.Count > 0)
        {
            guardsUITextTotal.text = "Guards " + guards.Count;

            bool  filled         = false;
            float lowestDistance = 0;
            foreach (Guard grd in guards)
            {
                float distance = Vector3.Distance(grd.transform.position, transform.position);
                if (filled)
                {
                    if (distance < lowestDistance)
                    {
                        lowestDistance = distance;
                    }
                }
                else
                {
                    lowestDistance = distance;
                    filled         = true;
                }
            }
            string txtDistance = "Distance " + Mathf.Round(lowestDistance);
            guardsUITextDistance.text = txtDistance;
        }

        if (crouching || Sprint)
        {
            suspicious = true;
        }
        else
        {
            suspicious = false;
        }
    }