Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Hi! I made this code (below) with tutorial and i have this error (below too). Can you help me?

Discussion in 'Scripting' started by Melonik9512, Jan 13, 2023.

  1. Melonik9512

    Melonik9512

    Joined:
    Dec 23, 2022
    Posts:
    4
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class mv : MonoBehaviour
    6. {
    7.     [Header("Movement")]
    8.     private float moveSpeed;
    9.     public float walkSpeed;
    10.     public float sprintSpeed;
    11.     public float dashSpeed;
    12.     public float dashSpeedChangeFactor;
    13.     public float slideSpeed;
    14.     public float wallrunSpeed;
    15.     public float climbSpeed;
    16.  
    17.     public float airMinSpeed;
    18.     public float speedIncreaseMultiplier;
    19.     public float slopeIncreaseMultiplier;
    20.  
    21.     public float groundDrag;
    22.  
    23.     [Header("Jumping")]
    24.     public float jumpForce;
    25.     public float jumpCooldown;
    26.     public float airMultiplier;
    27.     bool readyToJump;
    28.  
    29.     [Header("Crouching")]
    30.     public float crouchSpeed;
    31.     public float crouchYScale;
    32.     private float startYScale;
    33.  
    34.     [Header("Keybinds")]
    35.     public KeyCode jumpKey = KeyCode.Space;
    36.     public KeyCode sprintKey = KeyCode.LeftShift;
    37.     public KeyCode crouchKey = KeyCode.LeftControl;
    38.  
    39.     [Header("Ground Check")]
    40.     public float playerHeight;
    41.     public LayerMask whatIsGround;
    42.     bool grounded;
    43.  
    44.     [Header("Slope Handling")]
    45.     public float maxSlopeAngle;
    46.     private RaycastHit slopeHit;
    47.     private bool exitingSlope;
    48.  
    49.  
    50.     public Transform orientation;
    51.  
    52.     float horizontalInput;
    53.     float verticalInput;
    54.  
    55.     Vector3 moveDirection;
    56.  
    57.     Rigidbody rb;
    58.  
    59.    
    60.     public enum MovementState
    61.     {
    62.         walking,
    63.         sprinting,
    64.         wallrunning,
    65.         climbing,
    66.         crouching,
    67.         dashing,
    68.         sliding,
    69.         air
    70.     }
    71.  
    72.     public bool dashing;
    73.     public bool sliding;
    74.     public bool wallrunning;
    75.     public bool climbing;
    76.  
    77.     private void Start()
    78.     {
    79.         rb = GetComponent<Rigidbody>();
    80.         rb.freezeRotation = true;
    81.  
    82.         readyToJump = true;
    83.  
    84.         startYScale = transform.localScale.y;
    85.     }
    86.  
    87.     private void Update()
    88.     {
    89.         // ground check
    90.         grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.2f, whatIsGround);
    91.  
    92.         MyInput();
    93.         SpeedControl();
    94.         StateHandler();
    95.  
    96.         // handle drag
    97.         if (grounded)
    98.             rb.drag = groundDrag;
    99.         else
    100.             rb.drag = 0;
    101.     }
    102.  
    103.     private void FixedUpdate()
    104.     {
    105.         MovePlayer();
    106.     }
    107.  
    108.     private void MyInput()
    109.     {
    110.         horizontalInput = Input.GetAxisRaw("Horizontal");
    111.         verticalInput = Input.GetAxisRaw("Vertical");
    112.  
    113.         // when to jump
    114.         if (Input.GetKey(jumpKey) && readyToJump && grounded)
    115.         {
    116.             readyToJump = false;
    117.  
    118.             Jump();
    119.  
    120.             Invoke(nameof(ResetJump), jumpCooldown);
    121.         }
    122.  
    123.         // start crouch
    124.         if (Input.GetKeyDown(crouchKey))
    125.         {
    126.             transform.localScale = new Vector3(transform.localScale.x, crouchYScale, transform.localScale.z);
    127.             rb.AddForce(Vector3.down * 5f, ForceMode.Impulse);
    128.         }
    129.  
    130.         // stop crouch
    131.         if (Input.GetKeyUp(crouchKey))
    132.         {
    133.             transform.localScale = new Vector3(transform.localScale.x, startYScale, transform.localScale.z);
    134.         }
    135.     }
    136.  
    137.  
    138.  
    139.     private bool keepMomentum;
    140.     private float desiredMoveSpeed;
    141.     private float lastDesiredMoveSpeed;
    142.     private float MovementState; lastState
    143.  
    144.  
    145.  
    146.  
    147.     private void StateHandler()
    148.     {
    149.         //Mode - Climbing
    150.         if(climbing)
    151.         {
    152.             state = MovementState.climbing;
    153.             desiredMoveSpeed = climbSpeed;
    154.         }
    155.  
    156.  
    157.  
    158.         // Mode - Wallrunning
    159.         if(wallrunning)
    160.         {
    161.             state = MovementState.wallrunning;
    162.             desiredMoveSpeed = wallrunSpeed;
    163.         }
    164.  
    165.         // Mode - Sliding
    166.         if (sliding)
    167.         {
    168.             state = MovementState.sliding;
    169.  
    170.             if (OnSlope() && rb.velocity.y < 0.1f)
    171.                 desiredMoveSpeed = slideSpeed;
    172.  
    173.             else
    174.                 desiredMoveSpeed = sprintSpeed;
    175.         }
    176.         //Mode - Dashing
    177.         if(dashing)
    178.         {
    179.             state = MovementState.dashing;
    180.             desiredMoveSpeed = dashSpeed;
    181.             speedChangeFactor = dashSpeedChangeFactor;
    182.         }
    183.        
    184.  
    185.  
    186.  
    187.  
    188.         // Mode - Crouching
    189.         else if (Input.GetKey(crouchKey))
    190.         {
    191.             state = MovementState.crouching;
    192.             desiredMoveSpeed = crouchSpeed;
    193.         }
    194.  
    195.         // Mode - Sprinting
    196.         else if (grounded && Input.GetKey(sprintKey))
    197.         {
    198.             state = MovementState.sprinting;
    199.             desiredMoveSpeed = sprintSpeed;
    200.         }
    201.  
    202.         // Mode - Walking
    203.         else if (grounded)
    204.         {
    205.             state = MovementState.walking;
    206.             desiredMoveSpeed = walkSpeed;
    207.         }
    208.  
    209.         // Mode - Air
    210.         else
    211.         {
    212.             state = MovementState.air;
    213.  
    214.             if (desiredMoveSpeed < sprintSpeed)
    215.                 desiredMoveSpeed = walkSpeed;
    216.             else
    217.                 desiredMoveSpeed = sprintSpeed;
    218.  
    219.  
    220.         }
    221.      
    222.  
    223.         lastDesiredMoveSpeed = desiredMoveSpeed;
    224.         lastState = state;
    225.  
    226.  
    227.         // check if desiredMoveSpeed has changed drastically
    228.         if (Mathf.Abs(desiredMoveSpeed - lastDesiredMoveSpeed) > 4f && moveSpeed != 0)
    229.         {
    230.             StopAllCoroutines();
    231.             StartCoroutine(SmoothlyLerpMoveSpeed());
    232.         }
    233.         else
    234.         {
    235.             moveSpeed = desiredMoveSpeed;
    236.         }
    237.         bool desireMoveSpeddHasChanged = desiredMoveSpeed != lastDesiredMoveSpeed;
    238.         if (lastState == MovementState.dashing) keepMomentum = true;
    239.  
    240.         if (desireMoveSpeddHasChanged)
    241.         {
    242.             if (keepMomentum)
    243.             {
    244.                 StopAllCoroutines();
    245.                 StartCoroutine(SmoothlyLerpMoveSpeed());
    246.             }
    247.             else
    248.             {
    249.                 StopAllCoroutines();
    250.                 moveSpeed = desiredMoveSpeed;
    251.             }
    252.         }
    253.        
    254.        
    255.     }
    256.  
    257.     private IEnumerator SmoothlyLerpMoveSpeed()
    258.     {
    259.         // smoothly lerp movementSpeed to desired value
    260.         float time = 0;
    261.         float difference = Mathf.Abs(desiredMoveSpeed - moveSpeed);
    262.         float startValue = moveSpeed;
    263.  
    264.         float boostFactor = speedChangeFactor;
    265.  
    266.         while (time < difference)
    267.         {
    268.             moveSpeed = Mathf.Lerp(startValue, desiredMoveSpeed, time / difference);
    269.  
    270.             if (OnSlope())
    271.             {
    272.                 float slopeAngle = Vector3.Angle(Vector3.up, slopeHit.normal);
    273.                 float slopeAngleIncrease = 1 + (slopeAngle / 90f);
    274.  
    275.                 time += Time.deltaTime * speedIncreaseMultiplier * slopeIncreaseMultiplier * slopeAngleIncrease;
    276.             }
    277.             else
    278.                 time += Time.deltaTime * boostFactor;
    279.  
    280.             yield return null;
    281.         }
    282.  
    283.         moveSpeed = desiredMoveSpeed;
    284.     }
    285.  
    286.     private void MovePlayer()
    287.     {
    288.         // calculate movement direction
    289.         moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;
    290.  
    291.         // on slope
    292.         if (OnSlope() && !exitingSlope)
    293.         {
    294.             rb.AddForce(GetSlopeMoveDirection(moveDirection) * moveSpeed * 20f, ForceMode.Force);
    295.  
    296.             if (rb.velocity.y > 0)
    297.                 rb.AddForce(Vector3.down * 80f, ForceMode.Force);
    298.         }
    299.  
    300.         // on ground
    301.         else if (grounded)
    302.             rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);
    303.  
    304.         // in air
    305.         else if (!grounded)
    306.             rb.AddForce(moveDirection.normalized * moveSpeed * 10f * airMultiplier, ForceMode.Force);
    307.  
    308.         // turn gravity off while on slope
    309.         rb.useGravity = !OnSlope();
    310.     }
    311.  
    312.     private void SpeedControl()
    313.     {
    314.         // limiting speed on slope
    315.         if (OnSlope() && !exitingSlope)
    316.         {
    317.             if (rb.velocity.magnitude > moveSpeed)
    318.                 rb.velocity = rb.velocity.normalized * moveSpeed;
    319.         }
    320.  
    321.         // limiting speed on ground or in air
    322.         else
    323.         {
    324.             Vector3 flatVel = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
    325.  
    326.             // limit velocity if needed
    327.             if (flatVel.magnitude > moveSpeed)
    328.             {
    329.                 Vector3 limitedVel = flatVel.normalized * moveSpeed;
    330.                 rb.velocity = new Vector3(limitedVel.x, rb.velocity.y, limitedVel.z);
    331.             }
    332.         }
    333.     }
    334.  
    335.     private void Jump()
    336.     {
    337.         exitingSlope = true;
    338.  
    339.         // reset y velocity
    340.         rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
    341.  
    342.         rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
    343.     }
    344.     private void ResetJump()
    345.     {
    346.         readyToJump = true;
    347.  
    348.         exitingSlope = false;
    349.     }
    350.  
    351.     public bool OnSlope()
    352.     {
    353.         if (Physics.Raycast(transform.position, Vector3.down, out slopeHit, playerHeight * 0.5f + 0.3f))
    354.         {
    355.             float angle = Vector3.Angle(Vector3.up, slopeHit.normal);
    356.             return angle < maxSlopeAngle && angle != 0;
    357.         }
    358.  
    359.         return false;
    360.     }
    361.  
    362.     public Vector3 GetSlopeMoveDirection(Vector3 direction)
    363.     {
    364.         return Vector3.ProjectOnPlane(direction, slopeHit.normal).normalized;
    365.     }
    366. }
    obraz_2023-01-13_142636819.png
     
  2. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    If you look at the error, it will tell you it's finding a mistake in line 148, column 5.

    In this case that's not actually where the error is. The error is in line 142, where you're ending the line (with a ; ) then adding more text (the name of your variable).
    Thus, once it finds the next thing to parse, it fails on line 148, even though the error is before that:
    1. private float MovementState; lastState
     
    Melonik9512 likes this.
  3. Melonik9512

    Melonik9512

    Joined:
    Dec 23, 2022
    Posts:
    4
    so what i have to change?
     
  4. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    private float MovementState; lastState isn't a valid line.
    private float MovementState lastState; is a valid line.
     
    Melonik9512 likes this.
  5. Melonik9512

    Melonik9512

    Joined:
    Dec 23, 2022
    Posts:
    4
    Now i have this obraz_2023-01-13_151629397.png
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    You are rapidly swerving towards zero effort posting, explicitly prohibited on the forum Terms of Service.

    You're just making typing mistakes. We're not here to help you fix your typing skills.

    Please learn to read error messages. It's not hard.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.
     
  7. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    531
    Mr. FrankvHoof made an error when he corrected the line. But you should be able to discern this.

    the format is:
    scope datatype propertyName;

    nobody can guess what you intended lastState to be but possibly another float property.
     
    SF_FrankvHoof likes this.
  8. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    531
    And you are referencing something as state which (I'm not going to check) but my bet is it doesn't exist.

    Code (CSharp):
    1. state = MovementState.wallrunning;
    Learning to debug "your code" should be your primary goal at this point. Eventually people will see messages with these types of repeated errors and simply ignore them figuring if you can't be bothered to figure it out why should we?
     
    Kurt-Dekker likes this.