Search Unity

Resolved my character keeps jumping without an input

Discussion in 'Scripting' started by Shiury, May 19, 2020.

  1. Shiury

    Shiury

    Joined:
    Mar 28, 2020
    Posts:
    5
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMove : MonoBehaviour
    6. {
    7.  
    8.     public float moveSpeed;
    9.     public float jumpForce = 14f;
    10.     public SpriteRenderer sr;
    11.     public int amountOfJump = 1;
    12.     public float wallSlideSpeed;
    13.     public float wallJumpForce;
    14.     public float jumpTimerSet = 0.15f;
    15.  
    16.     private Rigidbody2D rb;
    17.     private Animator anim;
    18.     private int facingDirection = 1;
    19.     private float moveInput;
    20.     private int amountOfJumpLeft;
    21.     private float jumpTimer;
    22.  
    23.     private bool isGrounded;
    24.     private bool isWalking;
    25.     private bool isTouchingWall;
    26.     private bool isWallSliding;
    27.     private bool isFacingRight = true;
    28.     private bool canNormalJump;
    29.     private bool canWallJump;
    30.     private bool isAttemptingToJump;
    31.  
    32.     public float wallCheckDistance;
    33.     public Transform groundCheck;
    34.     public Transform wallCheck;
    35.     public float checkRadius;
    36.     public LayerMask Ground;
    37.     public Vector2 wallJumpDirection;
    38.  
    39.  
    40.  
    41.     void Start()
    42.     {
    43.         rb = GetComponent<Rigidbody2D>();
    44.         anim = GetComponent<Animator>();
    45.         amountOfJumpLeft = amountOfJump;
    46.         wallJumpDirection.Normalize();
    47.  
    48.     }
    49.  
    50.     void Update()
    51.     {
    52.        
    53.         checkInput();
    54.         checkifcanjump();
    55.         checkIfWallSliding();
    56.         checkJump();
    57.         FlipPlayer();
    58.         animationTrigger();
    59.         checkDirection();
    60.  
    61.        
    62.        
    63.  
    64.     }
    65.  
    66.     void FixedUpdate()
    67.     {
    68.        
    69.         applyMove();
    70.         checkingSurroundings();
    71.  
    72.     }
    73.  
    74.     private void checkInput(){
    75.  
    76.         moveInput = Input.GetAxis("Horizontal");
    77.  
    78.         if (Input.GetButtonDown("Jump")){
    79.             if (isGrounded || (amountOfJumpLeft > 0 && isTouchingWall))
    80.             {
    81.                 normalJump();
    82.             }
    83.                
    84.            
    85.         }
    86.         else
    87.         {
    88.             jumpTimer = jumpTimerSet;
    89.             isAttemptingToJump = true;
    90.         }
    91.            
    92.        
    93.     }
    94.      
    95.  
    96.     private void checkJump(){
    97.         if (jumpTimer > 0)
    98.         {
    99.             if (!isGrounded && isTouchingWall && moveInput != 0 && moveInput != facingDirection)
    100.             {
    101.                 wallJump();
    102.             }
    103.             else if (isGrounded)
    104.             {
    105.                 normalJump();
    106.             }
    107.         }
    108.            
    109.        
    110.  
    111.         if (isAttemptingToJump)
    112.         {
    113.             jumpTimer -= Time.deltaTime;
    114.         }
    115.            
    116.  
    117.     }
    118.  
    119.     private void normalJump()
    120.     {
    121.  
    122.         if (canNormalJump)
    123.         {
    124.             rb.velocity = new Vector2(rb.velocity.x, jumpForce);
    125.             amountOfJumpLeft--;
    126.             jumpTimer = 0.0f;
    127.             isAttemptingToJump = false;
    128.         }
    129.     }
    130.  
    131.  
    132.  
    133.     private void wallJump()
    134.     {
    135.         if (canWallJump)
    136.         {
    137.             rb.velocity = new Vector2(rb.velocity.x, 0.0f);
    138.             isWallSliding = false;
    139.             amountOfJumpLeft = amountOfJump;
    140.             amountOfJumpLeft--;
    141.             Vector2 forceToAdd = new Vector2(wallJumpForce * wallJumpDirection.x * moveInput, wallJumpForce * wallJumpDirection.y);
    142.             rb.AddForce(forceToAdd, ForceMode2D.Impulse);
    143.             jumpTimer = 0.0f;
    144.             isAttemptingToJump = false;
    145.         }
    146.     }
    147.  
    148.        
    149.            
    150.  
    151.        
    152.    
    153.  
    154.     private void checkifcanjump(){
    155.         if (isGrounded && rb.velocity.y <= 0.01f){
    156.             amountOfJumpLeft = amountOfJump;
    157.         }
    158.         if (isTouchingWall){
    159.             canWallJump = true;
    160.         }
    161.         if (amountOfJumpLeft <= 0){
    162.             canNormalJump = false;
    163.         }
    164.         else {
    165.             canNormalJump = true;
    166.         }
    167.     }
    168.  
    169.     private void checkingSurroundings(){
    170.         isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, Ground);
    171.         isTouchingWall = Physics2D.Raycast(wallCheck.position, transform.right, wallCheckDistance, Ground);
    172.  
    173.     }
    174.  
    175.  
    176.  
    177.  
    178.     private void FlipPlayer()
    179.     {
    180.         if (moveInput > 0 && !isFacingRight || moveInput < 0 && isFacingRight )
    181.         {
    182.             facingDirection *= -1;
    183.             isFacingRight = !isFacingRight;
    184.             transform.Rotate(new Vector3(0, 180, 0));
    185.         }
    186.     }
    187.  
    188.     private void OnDrawGizmos()
    189.     {
    190.         Gizmos.DrawLine(wallCheck.position, new Vector3(wallCheck.position.x + wallCheckDistance, wallCheck.position.y, wallCheck.position.z));
    191.     }
    192.  
    193.     private void checkIfWallSliding()
    194.     {
    195.         if (isTouchingWall && moveInput == facingDirection)
    196.         {
    197.             isWallSliding = true;
    198.         }
    199.         else
    200.         {
    201.             isWallSliding = false;
    202.         }
    203.     }
    204.  
    205.     private void animationTrigger(){
    206.  
    207.         if (moveInput == 0)
    208.         {
    209.             anim.SetBool("isWalking", false);
    210.         }
    211.         else
    212.         {
    213.             anim.SetBool("isWalking", true);
    214.         }
    215.  
    216.         if (isWallSliding){
    217.             anim.SetBool("isSliding", true);
    218.         }
    219.         else {
    220.             anim.SetBool("isSliding", false);
    221.         }
    222.  
    223.  
    224.         if (isGrounded != true)
    225.         {
    226.             anim.SetBool("isJumping", true);
    227.         }
    228.         else
    229.         {
    230.             anim.SetBool("isJumping", false);
    231.         }
    232.     }
    233.  
    234.     private void checkDirection(){
    235.         if (moveInput < 0)
    236.         {
    237.             isFacingRight = false;
    238.         }
    239.         else if (moveInput > 0)
    240.         {
    241.             isFacingRight = true;
    242.         }
    243.  
    244.         // checking is walking
    245.         if (rb.velocity.x != 0){
    246.             isWalking = true;
    247.         }
    248.         else {
    249.             isWalking = false;
    250.         }
    251.     }      
    252.  
    253.  
    254.  
    255.     private void applyMove(){
    256.         rb.velocity = new Vector2(moveInput * moveSpeed, rb.velocity.y);
    257.            
    258.  
    259.         if (isWallSliding){
    260.             if (rb.velocity.y < -wallSlideSpeed){
    261.                 rb.velocity = new Vector2(rb.velocity.x, -wallSlideSpeed);
    262.             }
    263.         }
    264.     }
    265.  
    266. }
    267.  
    268.    
    269.    
    270.    
    271.    
    272.  
    273.  
    274.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
  3. Shiury

    Shiury

    Joined:
    Mar 28, 2020
    Posts:
    5
    My character keep jumping without any input and when i remove the checkJump function it just works i think there s a problem in that function
     
  4. Shiury

    Shiury

    Joined:
    Mar 28, 2020
    Posts:
    5
    Update : i fixed the problem it was because the else needed to be with second if