Search Unity

{ expected

Discussion in '2D' started by unity_wAtE4BQN8WIJTg, Apr 11, 2020.

  1. unity_wAtE4BQN8WIJTg

    unity_wAtE4BQN8WIJTg

    Joined:
    May 11, 2019
    Posts:
    1
    I have a problem unity says } expected, but I have no idea why this is happening
    it says problem is in line 153
    this is my code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     private float movementInputDirection;
    8.  
    9.     private int amountOfJumpsLeft;
    10.  
    11.     private bool isFacingRight = true;
    12.     private bool isWalking;
    13.     private bool isGrounded;
    14.     private bool isTouchingWall;
    15.     private bool canjump;
    16.     private bool isWallSliding;
    17.  
    18.     private Rigidbody2D rb;
    19.     private Animator anim;
    20.  
    21.     public int amountOfJumps = 1;
    22.     private int facingDirection = 1;
    23.  
    24.     public float movementSpeed = 10.0f;
    25.     public float jumpForce = 16.0f;
    26.     public float groundCheckRadius;
    27.     public float wallCheckDistance;
    28.     public float wallSlideSpeed;
    29.     public float movementForseOnAir;
    30.     public float airDragMulltiplier = 0.95f;
    31.     public float wallHopForce;
    32.     public float walljumpForce;
    33.  
    34.     public Vector2 wallHopDirection;
    35.     public Vector2 walljumpDirection;
    36.  
    37.  
    38.     public Transform groundCheck;
    39.     public Transform wallCheck;
    40.  
    41.     public LayerMask whatIsGround;
    42.  
    43.     void Start()
    44.     {
    45.         rb = GetComponent<Rigidbody2D>();
    46.         anim = GetComponent<Animator>();
    47.         amountOfJumpsLeft = amountOfJumps;
    48.         wallHopDirection.Normalize();
    49.         walljumpDirection.Normalize();
    50.     }
    51.  
    52.     // Update is called once per frame
    53.     void Update()
    54.     {
    55.         CheckInput();
    56.         CheckMovementDirection();
    57.         UpdateAnimations();
    58.         CheckIfCanJump();
    59.         CheckIfWallSliding();
    60.     }
    61.  
    62.     private void FixedUpdate()
    63.     {
    64.         ApplyMovement();
    65.         CheckSurroundings();
    66.     }
    67.  
    68.     private void CheckIfWallSliding()
    69.     {
    70.         if (isTouchingWall && !isGrounded && rb.velocity.y < 0)
    71.         {
    72.             isWallSliding = true;
    73.         }
    74.         else
    75.         {
    76.             isWallSliding = false;
    77.         }
    78.     }
    79.  
    80.     private void CheckSurroundings()
    81.     {
    82.         isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
    83.  
    84.         isTouchingWall = Physics2D.Raycast(wallCheck.position, transform.right, wallCheckDistance, whatIsGround);
    85.     }
    86.  
    87.     private void CheckIfCanJump()
    88.     {
    89.         if ((isGrounded && rb.velocity.y < 0.01f) || isWallSliding)
    90.         {
    91.             amountOfJumpsLeft = amountOfJumps;
    92.         }
    93.  
    94.         if (amountOfJumpsLeft <= 0)
    95.         {
    96.             canjump = false;
    97.         }
    98.         else
    99.         {
    100.             canjump = true;
    101.         }
    102.     }
    103.  
    104.     private void CheckMovementDirection()
    105.     {
    106.         if (isFacingRight && movementInputDirection < 0)
    107.         {
    108.             Flip();
    109.         }
    110.         else if (!isFacingRight && movementInputDirection > 0)
    111.         {
    112.             Flip();
    113.         }
    114.  
    115.         if (Mathf.Abs(rb.velocity.x) > 0.01f)
    116.         {
    117.             isWalking = true;
    118.         }
    119.         else
    120.         {
    121.             isWalking = false;
    122.         }
    123.     }
    124.  
    125.     private void UpdateAnimations()
    126.     {
    127.         anim.SetBool("isWalking", isWalking);
    128.         anim.SetBool("isGrounded", isGrounded);
    129.         anim.SetFloat("yVelocity", rb.velocity.y);
    130.         anim.SetBool("isWallSliding", isWallSliding);
    131.     }
    132.  
    133.     private void CheckInput()
    134.     {
    135.         movementInputDirection = Input.GetAxisRaw("Horizontal");
    136.  
    137.         if (Input.GetButtonDown("Jump"))
    138.         {
    139.             Jump();
    140.         }
    141.     }
    142.  
    143.     private void Jump()
    144.     {
    145.         if (canjump && !isWallSliding)
    146.         {
    147.             rb.velocity = new Vector2(rb.velocity.x, jumpForce);
    148.             amountOfJumpsLeft--;
    149.         }
    150.         else if (isWallSliding && movementInputDirection == 0 && canjump) ; //wall hop
    151.         {
    152.             isWallSliding = false;
    153.             amountOfJumpsLeft--;
    154.             Vector2 forceToAdd = new Vector2(wallHopForce * wallHopDirection.x * -facingDirection, wallHopForce * wallHopDirection.y);
    155.             rb.AddForce(forceToAdd, ForceMode2D.Impulse);
    156.         }
    157.         else if ((isWallSliding || isTouchingWall) && movementInputDirection != 0 && canjump)
    158.         {
    159.             isWallSliding = false;
    160.             amountOfJumpsLeft--;
    161.             Vector2 forceToAdd = new Vector2(walljumpForce * walljumpDirection.x * movementInputDirection, wallJumpForce * walljumpDirection.y);
    162.             rb.AddForce(forceToAdd, ForceMode2D.Impulse);
    163.         }
    164.     }
    165.  
    166.     private void ApplyMovement()
    167.     {
    168.         rb.velocity = new Vector2(movementSpeed * movementInputDirection, rb.velocity.y);
    169.         if (isGrounded)
    170.         {
    171.             rb.velocity = new Vector2(movementSpeed * movementInputDirection, rb.velocity.y);
    172.         }
    173.         else if (!isGrounded && !isWallSliding && movementInputDirection != 0)
    174.         {
    175.             Vector2 forceToAdd = new Vector2(movementForseOnAir * movementInputDirection, 0);
    176.             rb.AddForce(forceToAdd);
    177.  
    178.             if (Mathf.Abs(rb.velocity.x) > movementSpeed)
    179.             {
    180.                 rb.velocity = new Vector2(movementSpeed * movementInputDirection, rb.velocity.y);
    181.             }
    182.         }
    183.         else if (!isGrounded && !isWallSliding && movementInputDirection == 0) ;
    184.  
    185.  
    186.         if (isWallSliding)
    187.         {
    188.             if (rb.velocity.y < -wallSlideSpeed)
    189.             {
    190.                 rb.velocity = new Vector2(rb.velocity.x, -wallSlideSpeed);
    191.             }
    192.         }
    193.     }
    194.  
    195.     private void Flip()
    196.     {
    197.         if (!isWallSliding)
    198.         {
    199.             facingDirection *= -1;
    200.             isFacingRight = !isFacingRight;
    201.             transform.Rotate(0.0f, 180.0f, 0.0f);
    202.         }
    203.     }
    204.  
    205.     private void OnDrawGizmos()
    206.     {
    207.         Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
    208.  
    209.         Gizmos.DrawLine(wallCheck.position, new Vector3(wallCheck.position.x + wallCheckDistance, wallCheck.position.y, wallCheck.position.z));
    210.     }
    211. }
    212.  
     
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,514
    Line 183 seems to be lacking a statement.. the else if needs { }