Search Unity

Input on player controller not responsive

Discussion in 'Scripting' started by cbrown117, Aug 3, 2020.

  1. cbrown117

    cbrown117

    Joined:
    Nov 9, 2019
    Posts:
    12
    So I am setting up a third person player controller and everything works well except the jumping. If my player is idle jumping works fine but when I am moving the input is not being read and the player can't jump. I figure this is because of the if(direction.magnitude >= 0.1f) is being read and because its being read every frame. I am curious how to get around this. do I just set up a Jump() method inside that if and outside it making sure its read regardless? I have tried moving the code around but it does not work. Any help would be awesome, thanks.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PlayerMove_Attack : MonoBehaviour
    7. {
    8.     //player movement variables
    9.     public CharacterController controller;
    10.     public Transform cam;
    11.     public float speed = 6f;
    12.     public float gravity = -9.8f;
    13.     public float jumpHeight = 8f;
    14.     public float turnSmoothTime = 0f;
    15.     float turnSmoothVeloctiy;
    16.     Vector3 velocity;
    17.  
    18.     //player animation variables
    19.     public Animator animator;
    20.     bool isIdle, isRunning, isJumping, isBacking, isAttacking, canDoubleJump, canAttack;
    21.  
    22.     //attack cooldown so the player cant spam attack animation
    23.     float attackCooldown;
    24.  
    25.  
    26.     void Start()
    27.     {
    28.              isIdle = true;
    29.              isRunning = false;
    30.              isBacking = false;
    31.              isJumping = false;
    32.              isAttacking = false;
    33.              canDoubleJump = false;
    34.              canAttack = true;
    35.     }
    36.  
    37.     void Update()
    38.     {
    39.         float horizontal = Input.GetAxisRaw("Horizontal");
    40.         float vertical = Input.GetAxisRaw("Vertical");
    41.         Vector3 direction = new Vector3(horizontal, 0, vertical).normalized;
    42.  
    43.         //velocity with gravity allows the player to fall
    44.         //this resets velocity so it is not always adding up in the background
    45.         if(controller.isGrounded)
    46.         {
    47.             velocity.y = -2f;
    48.             isJumping = false;
    49.             canDoubleJump = false;
    50.         }
    51.          
    52.  
    53.         if(direction.magnitude >= 0.1f)
    54.         {
    55.             isRunning = true;
    56.             isIdle = false;
    57.  
    58.             //target angle takes the x and z axis to get a y angle of movement
    59.             //this is how we move diagonally
    60.             float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
    61.             float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVeloctiy, turnSmoothTime);
    62.             transform.rotation = Quaternion.Euler(0f, angle, 0f);
    63.  
    64.             Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
    65.             controller.Move(moveDir * speed * Time.deltaTime);
    66.         }
    67.         else
    68.         {
    69.             isIdle = true;
    70.             isRunning = false;
    71.         }
    72.  
    73.         //check to see if the jump key is pressed (space bar in this case)
    74.         if(controller.isGrounded)
    75.         {
    76.             if(Input.GetKeyDown(KeyCode.Space) && controller.isGrounded)
    77.             {
    78.                 isRunning = false;
    79.                 isJumping = true;
    80.                 canDoubleJump = true;
    81.  
    82.                 velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    83.             }
    84.         }
    85.  
    86.         else if(canDoubleJump && Input.GetKeyDown(KeyCode.Space))
    87.         {
    88.             velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    89.  
    90.             isRunning = false;
    91.             canDoubleJump = false;
    92.         }
    93.  
    94.         if(Input.GetKeyDown(KeyCode.Mouse0) && canAttack == true)
    95.         {
    96.             isAttacking = true;
    97.             isRunning = false;
    98.             isIdle = false;
    99.             isJumping = false;
    100.             canAttack = false;
    101.  
    102.             controller.Move(Vector3.zero);
    103.  
    104.             StartCoroutine(WaitToAttack()); //this helps not spam the attack animation
    105.         }
    106.  
    107.         if(animator.GetBool("isAttacking"))
    108.             isAttacking = false;
    109.  
    110.         //this is our gravity
    111.         //note needs to be after jump
    112.         velocity.y += gravity * Time.deltaTime;
    113.         controller.Move(velocity * Time.deltaTime);
    114.  
    115.         animator.SetBool("isRunning", isRunning);
    116.         animator.SetBool("isBacking", isBacking);
    117.         animator.SetBool("isJumping", isJumping);
    118.         animator.SetBool("canDoubleJump", canDoubleJump);
    119.         animator.SetBool("isAttacking", isAttacking);
    120.         animator.SetBool("isIdle", isIdle);
    121.     }
    122.  
    123.     private IEnumerator WaitToAttack()
    124.     {
    125.         yield return new WaitForSeconds(.6f);
    126.         canAttack = true;
    127.     }
    128. }
    129.  
    130.  
     
    Last edited: Aug 3, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    eisenpony likes this.
  3. cbrown117

    cbrown117

    Joined:
    Nov 9, 2019
    Posts:
    12
    It is reaching the first if and passing over everything in there
     
  4. cbrown117

    cbrown117

    Joined:
    Nov 9, 2019
    Posts:
    12
    I have it working, its the GetKeyDown method. just needs to be GetKey
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Try to restructure things to keep the if/then tests simpler, testing only one condition at a time. For instance in line 74 and line 76 you superfluously check isGrounded, which is just visual noise when you are trying to reason about the logic.

    Print the values in real time, see what they are. If they are wrong, find out where they are coming from and so on up the trail of data. Follow the data.
     
    eisenpony likes this.