void Update()
    {
        if (isDead)
        {
            Death();
        }

        #region Health/Hunger/Stamina

        healthBar.fillAmount  = health / maxHealth;
        hungerBar.fillAmount  = hunger / maxHunger;
        staminaBar.fillAmount = stamina / maxStamina;

        if (health > maxHealth)
        {
            health = 10;
        }

        if (hunger == 0)
        {
            isDead = true;
        }

        #endregion


        #region Movement
        CharacterController controller = GetComponent <CharacterController>();
        if (controller.isGrounded)
        {
            moveDirection  = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
            moveDirection  = transform.TransformDirection(moveDirection);
            moveDirection *= speed;

            if (Input.GetButton("Jump"))
            {
                moveDirection.y = jumpSpeed;
            }


            if (Input.GetKey(KeyCode.LeftShift))
            {
                speed = sprintSpeed;
            }

            if (Input.GetKeyUp(KeyCode.LeftShift))
            {
                speed = 7.0f;
            }
        }
        #endregion

        if (Input.GetKeyDown(KeyCode.KeypadEnter))
        {
            if (Cursor.lockState == CursorLockMode.None)
            {
                Cursor.lockState = CursorLockMode.Locked;
            }

            else if (Cursor.lockState == CursorLockMode.Locked)
            {
                Cursor.lockState = CursorLockMode.None;
            }
        }

        RaycastHit hit;
        float      range = 4.5f;

        #region Controls
        //What to do when the player press the left mouse button.
        if (Input.GetMouseButtonDown(0))
        {
            //If the player left clicks while having their mouse unlocked then lock their mouse
            if (Cursor.lockState == CursorLockMode.None)
            {
                Cursor.lockState = CursorLockMode.Locked;
            }
            //When The Player Presses Mouse 0 (Left Mouse Button) The Script Will Check If he hit something and if so what it hit.
            if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, range))
            {
                //If the player clicks on a tree...
                if (hit.transform.gameObject.tag == "Tree")
                {
                    //Get the script for the tree that has just been hit
                    TreeScript Target = hit.transform.GetComponent <TreeScript>();
                    //@@REMINDER@@ Replace attack number with Attack variable
                    //Run the function that handles what to do when the tree is punched.
                    Target.Punch(1);
                    //Spawns a particle system wherever the player clicked.
                    GameObject treeFX = Instantiate(timpactFX, hit.point, Quaternion.LookRotation(hit.normal));
                    //Then Destroys said particle system.
                    Destroy(treeFX, 1f);
                }

                if (hit.transform.gameObject.tag == "Item")
                {
                    ItemBehavior Target = hit.transform.gameObject.GetComponent <ItemBehavior>();
                }
            }
        }
        #endregion
        //Movement Stuff
        moveDirection.y -= gravity * Time.deltaTime;
        controller.Move(moveDirection * Time.deltaTime);
    }
Beispiel #2
0
    void Update()
    {
        //All The Code For The Player Controls Moving, Attacking, etc..
        #region Controls
        //Update This...
        //Movement Controls...
        var x = Input.GetAxis("Horizontal") * Time.deltaTime * xMod;
        var z = Input.GetAxis("Vertical") * Time.deltaTime * zMod;

        transform.Translate(x, 0, 0);
        transform.Translate(0, 0, z);

        //Sprinting, When Shift is held down the player moves faster
        if (Input.GetKey(KeyCode.LeftShift))
        {
            xMod = 60f;
            zMod = 60f;

            isRunning = true;
        }

        //When The Player lets go of shift their speed returns to normal
        else if (Input.GetKeyUp(KeyCode.LeftShift))
        {
            xMod = 15f;
            zMod = 15f;

            isRunning = false;
        }

        //If your mouse is locked this will let it "Escape"
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            if (Cursor.lockState == CursorLockMode.Locked)
            {
                Cursor.lockState = CursorLockMode.None;
            }
        }
        #endregion

        RaycastHit hit;
        float      range = 4.5f;

        if (Input.GetMouseButtonDown(0))
        {
            //If the player left clicks while having their mouse unlocked then lock their mouse
            if (Cursor.lockState == CursorLockMode.None)
            {
                Cursor.lockState = CursorLockMode.Locked;
            }
            //When The Player Presses Mouse 0 (Left Mouse Button) The Script Will Check If He's Looking At A Tree Then Figure Out What Tree It Is And "Attack it"
            if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, range))
            {
                TreeScript Target = hit.transform.GetComponent <TreeScript>();
                if (Target != null)
                {
                    Target.Punch(1);
                }

                else
                {
                }
                GameObject treeFX = Instantiate(treeImpactFX, hit.point, Quaternion.LookRotation(hit.normal));
                Destroy(treeFX, 2f);
            }
        }
    }