Search Unity

Bug Code error

Discussion in 'Scripting' started by jasonasaad2, Jan 12, 2021.

  1. jasonasaad2

    jasonasaad2

    Joined:
    Nov 30, 2020
    Posts:
    51
    What do I do to fix CS1513: } expected
    Code:
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class HookShot : MonoBehaviour
    7. {
    8.  
    9.     private const float NORMAL_FOV = 60f;
    10.     private const float HOOKSHOT_FOV = 100f;
    11.  
    12.     private CharacterController characterController;
    13.     private float cameraVerticalAngle;
    14.     private float characterVelocityY;
    15.     private Vector3 characterVelocityMomentum;
    16.     private Camera playerCamera;
    17.     private CameraFov cameraFov;
    18.     private ParticleSystem speedLinesParticleSystem;
    19.     private State state;
    20.     private Vector3 hookshotPosition;
    21.     private float hookshotSize;
    22.  
    23.     private enum State
    24.     {
    25.         Normal,
    26.         HookshotThrown,
    27.         HookshotFlyingPlayer,
    28.     }
    29.  
    30.     private void Awake()
    31.     {
    32.         characterController = GetComponent<CharacterController>();
    33.         playerCamera = transform.Find("Camera").GetComponent<Camera>();
    34.         cameraFov = playerCamera.GetComponent<CameraFov>();
    35.         speedLinesParticleSystem = transform.Find("Camera").Find("SpeedLinesParticleSystem").GetComponent<ParticleSystem>();
    36.         Cursor.lockState = CursorLockMode.Locked;
    37.         state = State.Normal;
    38.         hookshotTransform.gameObject.SetActive(false);
    39.     }
    40.  
    41.     private void ResetGravityEffect()
    42.     {
    43.         characterVelocityY = 0f;
    44.     }
    45.  
    46.     private void HandleHookshotStart()
    47.     {
    48.         if (TestInputDownHookshot())
    49.         {
    50.             if (Physics.Raycast(playerCamera.transform.position, playerCamera.transform.forward, out RaycastHit raycastHit))
    51.             {
    52.                 // Hit something
    53.                 debugHitPointTransform.position = raycastHit.point;
    54.                 hookshotPosition = raycastHit.point;
    55.                 hookshotSize = 0f;
    56.                 hookshotTransform.gameObject.SetActive(true);
    57.                 hookshotTransform.localScale = Vector3.zero;
    58.                 state = State.HookshotThrown;
    59.             }
    60.         }
    61.     }
    62.  
    63.     private void HandleHookshotThrow()
    64.     {
    65.         hookshotTransform.LookAt(hookshotPosition);
    66.  
    67.         float hookshotThrowSpeed = 500f;
    68.         hookshotSize += hookshotThrowSpeed * Time.deltaTime;
    69.         hookshotTransform.localScale = new Vector3(1, 1, hookshotSize);
    70.  
    71.         if (hookshotSize >= Vector3.Distance(transform.position, hookshotPosition))
    72.         {
    73.             state = State.HookshotFlyingPlayer;
    74.             cameraFov.SetCameraFov(HOOKSHOT_FOV);
    75.             speedLinesParticleSystem.Play();
    76.         }
    77.     }
    78.  
    79.     private void HandleHookshotMovement()
    80.     {
    81.         hookshotTransform.LookAt(hookshotPosition);
    82.  
    83.         Vector3 hookshotDir = (hookshotPosition - transform.position).normalized;
    84.  
    85.         float hookshotSpeedMin = 10f;
    86.         float hookshotSpeedMax = 40f;
    87.         float hookshotSpeed = Mathf.Clamp(Vector3.Distance(transform.position, hookshotPosition), hookshotSpeedMin, hookshotSpeedMax);
    88.         float hookshotSpeedMultiplier = 5f;
    89.  
    90.         // Move Character Controller
    91.         characterController.Move(hookshotDir * hookshotSpeed * hookshotSpeedMultiplier * Time.deltaTime);
    92.  
    93.         float reachedHookshotPositionDistance = 1f;
    94.         if (Vector3.Distance(transform.position, hookshotPosition) < reachedHookshotPositionDistance)
    95.         {
    96.             // Reached Hookshot Position
    97.             StopHookshot();
    98.         }
    99.  
    100.         if (TestInputDownHookshot())
    101.         {
    102.             // Cancel Hookshot
    103.             StopHookshot();
    104.         }
    105.  
    106.         if (TestInputJump())
    107.         {
    108.             // Cancelled with Jump
    109.             float momentumExtraSpeed = 7f;
    110.             characterVelocityMomentum = hookshotDir * hookshotSpeed * momentumExtraSpeed;
    111.             float jumpSpeed = 40f;
    112.             characterVelocityMomentum += Vector3.up * jumpSpeed;
    113.             StopHookshot();
    114.         }
    115.     }
    116.  
    117.     private void StopHookshot()
    118.     {
    119.         state = State.Normal;
    120.         ResetGravityEffect();
    121.         hookshotTransform.gameObject.SetActive(false);
    122.         cameraFov.SetCameraFov(NORMAL_FOV);
    123.         speedLinesParticleSystem.Stop();
    124.     }
    125.  
    126.     private bool TestInputDownHookshot()
    127.     {
    128.         return Input.GetKeyDown(KeyCode.F);
    129.  
     
    Last edited: Jan 12, 2021
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,914
    Please use regular "[ CODE ]" tags rather than "[ ICODE ]".

    Is this your entire file? If this is really all the code, is that you're missing closing brackets for both "TestInputDownHookshot()" and the class itself at the end of the file.
     
    Last edited: Jan 12, 2021
  3. jasonasaad2

    jasonasaad2

    Joined:
    Nov 30, 2020
    Posts:
    51
    This is all the code, I fixed the ICODE, but what do I do to fix this "(126,44): error CS1513: } expected" Also that is the only compiler error
     
  4. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    798
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class HookShot : MonoBehaviour
    5. {
    6.     private const float NORMAL_FOV = 60f;
    7.     private const float HOOKSHOT_FOV = 100f;
    8.     private CharacterController characterController;
    9.     private float cameraVerticalAngle;
    10.     private float characterVelocityY;
    11.     private Vector3 characterVelocityMomentum;
    12.     private Camera playerCamera;
    13.     private CameraFov cameraFov;
    14.     private ParticleSystem speedLinesParticleSystem;
    15.     private State state;
    16.     private Vector3 hookshotPosition;
    17.     private float hookshotSize;
    18.     private enum State
    19.     {
    20.         Normal,
    21.         HookshotThrown,
    22.         HookshotFlyingPlayer,
    23.     }
    24.     private void Awake()
    25.     {
    26.         characterController = GetComponent<CharacterController>();
    27.         playerCamera = transform.Find("Camera").GetComponent<Camera>();
    28.         cameraFov = playerCamera.GetComponent<CameraFov>();
    29.         speedLinesParticleSystem = transform.Find("Camera").Find("SpeedLinesParticleSystem").GetComponent<ParticleSystem>();
    30.         Cursor.lockState = CursorLockMode.Locked;
    31.         state = State.Normal;
    32.         hookshotTransform.gameObject.SetActive(false);
    33.     }
    34.     private void ResetGravityEffect()
    35.     {
    36.         characterVelocityY = 0f;
    37.     }
    38.     private void HandleHookshotStart()
    39.     {
    40.         if (TestInputDownHookshot())
    41.         {
    42.             if (Physics.Raycast(playerCamera.transform.position, playerCamera.transform.forward, out RaycastHit raycastHit))
    43.             {
    44.                 // Hit something
    45.                 debugHitPointTransform.position = raycastHit.point;
    46.                 hookshotPosition = raycastHit.point;
    47.                 hookshotSize = 0f;
    48.                 hookshotTransform.gameObject.SetActive(true);
    49.                 hookshotTransform.localScale = Vector3.zero;
    50.                 state = State.HookshotThrown;
    51.             }
    52.         }
    53.     }
    54.     private void HandleHookshotThrow()
    55.     {
    56.         hookshotTransform.LookAt(hookshotPosition);
    57.         float hookshotThrowSpeed = 500f;
    58.         hookshotSize += hookshotThrowSpeed * Time.deltaTime;
    59.         hookshotTransform.localScale = new Vector3(1, 1, hookshotSize);
    60.         if (hookshotSize >= Vector3.Distance(transform.position, hookshotPosition))
    61.         {
    62.             state = State.HookshotFlyingPlayer;
    63.             cameraFov.SetCameraFov(HOOKSHOT_FOV);
    64.             speedLinesParticleSystem.Play();
    65.         }
    66.     }
    67.     private void HandleHookshotMovement()
    68.     {
    69.         hookshotTransform.LookAt(hookshotPosition);
    70.         Vector3 hookshotDir = (hookshotPosition - transform.position).normalized;
    71.         float hookshotSpeedMin = 10f;
    72.         float hookshotSpeedMax = 40f;
    73.         float hookshotSpeed = Mathf.Clamp(Vector3.Distance(transform.position, hookshotPosition), hookshotSpeedMin, hookshotSpeedMax);
    74.         float hookshotSpeedMultiplier = 5f;
    75.         // Move Character Controller
    76.         characterController.Move(hookshotDir * hookshotSpeed * hookshotSpeedMultiplier * Time.deltaTime);
    77.         float reachedHookshotPositionDistance = 1f;
    78.         if (Vector3.Distance(transform.position, hookshotPosition) < reachedHookshotPositionDistance)
    79.         {
    80.             // Reached Hookshot Position
    81.             StopHookshot();
    82.         }
    83.         if (TestInputDownHookshot())
    84.         {
    85.             // Cancel Hookshot
    86.             StopHookshot();
    87.         }
    88.         if (TestInputJump())
    89.         {
    90.             // Cancelled with Jump
    91.             float momentumExtraSpeed = 7f;
    92.             characterVelocityMomentum = hookshotDir * hookshotSpeed * momentumExtraSpeed;
    93.             float jumpSpeed = 40f;
    94.             characterVelocityMomentum += Vector3.up * jumpSpeed;
    95.             StopHookshot();
    96.         }
    97.     }
    98.     private void StopHookshot()
    99.     {
    100.         state = State.Normal;
    101.         ResetGravityEffect();
    102.         hookshotTransform.gameObject.SetActive(false);
    103.         cameraFov.SetCameraFov(NORMAL_FOV);
    104.         speedLinesParticleSystem.Stop();
    105.     }
    106.     private bool TestInputDownHookshot()
    107.     {
    108.         return Input.GetKeyDown(KeyCode.F);
    109.     }
    110. }
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    This seems dubious. To me it looks chopped off midway through the
    TestInputDownHookshot()
    function.

    I suggest you return to wherever you found this and make sure you got it all.
     
    PraetorBlue and flashframe like this.