Exemple #1
0
    void Update()
    {
        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        if (x != 0f || z != 0f)
        {
            playerAnim.Move();
        }
        else
        {
            playerAnim.Idle();
        }

        Vector3 moveDirection = new Vector3(x, 0, z);

        moveDirection  = cameraTransform.TransformDirection(moveDirection);
        moveDirection *= moveSpeed;

        if (characterController.isGrounded == true)
        {
            yVelocity = 0.0f;
            jumpCount = 0;

            if (jumped == true)
            {
                playerAnim.Grounded();
            }
            jumped = false;
        }
        //Debug.Log(characterController.isGrounded);

        if (Input.GetButtonDown("Jump") && jumpCount < 2)
        {
            jumped = true;

            yVelocity = jumpSpeed;

            ++jumpCount;

            playerAnim.Jump();
        }

        yVelocity      += (gravity * Time.deltaTime);
        moveDirection.y = yVelocity;

        //Debug.Log(yVelocity);

        characterController.Move(moveDirection * Time.deltaTime);
    }
Exemple #2
0
 private void SetMovementAnimation()
 {
     // If the player is moving at all
     if (verticalInput > 0 || verticalInput < 0 ||
         horizontalInput > 0 || horizontalInput < 0)
     {
         // Set the animation to run
         playerAnim.Run();
     } // Otherwise
     else
     {
         // Set the animation to idle
         playerAnim.Idle();
     }
 }
Exemple #3
0
    IEnumerator Move()
    {
        while (canMove)
        {
            //calculate new transform.position
            Vector3 newPos = Vector3.Normalize(new Vector3(InputManager.instance.GetHorizontal(), 0, InputManager.instance.GetVertical())) * speed;
            //print (newPos);

            //rotate character
            if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
            {
                transform.rotation = Quaternion.LookRotation(newPos);
            }
            else
            {
                playerAnim.Idle();
            }

            //move to new position
            rigidbody.AddForce(newPos);

            if (rigidbody.velocity.magnitude > 0.1)
            {
                playerAnim.Walk();
            }
            if (speed > baseSpeed)
            {
                playerAnim.Run();
            }

            //Resets velocity to 0 each frame so character doesn't slide around
            rigidbody.velocity = new Vector3(0, rigidbody.velocity.y, 0);

            yield return(null);
        }
    }