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. Dismiss Notice

Question How do I resolve this?

Discussion in 'Scripting' started by TrexMatrix, Aug 29, 2023.

  1. TrexMatrix

    TrexMatrix

    Joined:
    Aug 8, 2023
    Posts:
    39
    Hey, how do I resolve this error?
    Assets\Scripts\Movement.cs(53,37): error CS0103: The name 'velocity' does not exist in the current context

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6.  
    7. public class Movement : MonoBehaviour
    8. {
    9.     public float walkSpeed = 4f;
    10.     public float sprintSpeed = 14f;
    11.     public float maxVelocityChange = 10f;
    12.     [Space]
    13.     public float jumpHeight = 30f;
    14.  
    15.     private Vector2 input;
    16.     private Rigidbody rb;
    17.  
    18.     private bool sprinting;
    19.     private bool jumping;
    20.  
    21.     private bool grounded = false;
    22.  
    23.     // Start is called before the first frame update
    24.     void Start()
    25.     {
    26.         rb = GetComponent<Rigidbody>();
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void Update()
    31.     {
    32.         input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
    33.         input.Normalize();
    34.  
    35.         sprinting = Input.GetButton("Sprint");
    36.         jumping = Input.GetButton("Jump");
    37.     }
    38.  
    39.  
    40.     void FixedUpdate()
    41.     {
    42.         if (grounded)
    43.         {
    44.             if  (jumping)
    45.             {
    46.                 rb.velocity = new Vector3(rb.velocity.x, jumpHeight, rb.velocity.z);
    47.             } else if (input.magnitude > 0.5f)
    48.         {
    49.             rb.AddForce(CalculateMovement(sprinting ? sprintSpeed : walkSpeed), ForceMode.VelocityChange);
    50.         }
    51.         else
    52.         {
    53.             var velocity1 = rb.velocity;
    54.             velocity1 = new Vector3(velocity.x * 0.2f * Time.fixedDeltaTime, velocity1.y, velocity1.z * 0.2f * Time.fixedDeltaTime);
    55.             rb.velocity = velocity1;
    56.         }
    57.         }
    58.     }
    59.  
    60.  
    61.     Vector3 CalculateMovement(float _speed)
    62.     {
    63.         Vector3 targetVelocity = new Vector3(input.x, 0, input.y);
    64.         targetVelocity = transform.TransformDirection(targetVelocity);
    65.  
    66.         targetVelocity *= _speed;
    67.  
    68.         Vector3 velocity = rb.velocity;
    69.  
    70.         if (input.magnitude > 0.5f)
    71.         {
    72.             Vector3 velocityChange = targetVelocity - velocity;
    73.  
    74.             velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
    75.             velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
    76.  
    77.             velocityChange.y = 0f;
    78.  
    79.  
    80.             return (velocityChange);
    81.         }
    82.         else
    83.         {
    84.             return new Vector3();
    85.         }
    86.     }
    87. }
    88.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    You're just making typos. You can fix those yourself by reading the error message. Here's how:


    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.

    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.




    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!


    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

    Finally, when you have errors, don't post here... just go fix your errors! Here's how:
     
  3. TrexMatrix

    TrexMatrix

    Joined:
    Aug 8, 2023
    Posts:
    39
    i check for typos already there is any that i see
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Then show me where the variable
    velocity
    is defined.

    I think you'll be surprised.
     
  5. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    711
    Then you need to look harder.
    It will give you line and character position.
    My guess line 54 of your above code. Because yes. You typod.
     
    Kurt-Dekker likes this.
  6. TrexMatrix

    TrexMatrix

    Joined:
    Aug 8, 2023
    Posts:
    39
    I'm either just gonna rewrite it or keep looking for the typo. I still can find anything though, I'm following a tutorial and I've looks back any forth between the video and my code and still nothing
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    I told you EXACTLY what is missing! Read my second reply!!!

    Go show me the variable
    velocity
    because I sure don't see it!

    I'm not sure what is going on with you but you're establishing a track record of not paying attention, such as in this post that got locked:

    https://forum.unity.com/threads/can-someone-tell-me-what-this-error-is.1471761/#post-9206943

    This isn't text messaging. This is software engineering. You have to pay attention to what you are doing here, pay attention to what the error messages are telling you.
     
    wideeyenow_unity and bugfinders like this.
  8. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    711

    Show that chunk of code as a screen print from the tutorial
     
  9. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    54. velocity1 = new Vector3(velocity.x * 0.2f * Time.fixedDeltaTime
     
  10. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    But, yeah man, I feel ya. I'm soon to put on my crusader outfit and lay siege on these "Code Editing" software makers, because I mean it should show a red squiggly line thing if I make a typo somewhere so I can see it. It's just becoming an epidemic! I mean, at the very least, if they could at least give me an error code, with a vague reasoning to why.. Not to mention a line number, geesh, it's probably asking for too much, I know...

    But never fear, I'm on the phone with corporates right now, I'll get this resolved. :cool:
     
    bugfinders likes this.
  11. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    711
    yes perhaps an error such as "Assets\Scripts\Movement.cs(53,37): error CS0103: The name 'velocity' does not exist in the current context" which says whichi word, what character and what line, can you suggest that?
     
    wideeyenow_unity likes this.