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 error says missing semicolon even though i have one?

Discussion in 'Scripting' started by Sekudo, Jan 12, 2023.

  1. Sekudo

    Sekudo

    Joined:
    Dec 4, 2022
    Posts:
    36
    so this might be really noob thing, but im new to c#. in the effected line, line 56, the error i got was 56, 31, error cs1002: ; expected, which i learned means that you need a semicolon. but, i have one at the end? if anyone is able to help i'd appreciate it a ton. thank u for reading, heres my code

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using System.Collections;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     public CharacterController controller;
    8.     public Transform cam;
    9.  
    10.     public float speed = 6f;
    11.     public float gravity = -9.81f;
    12.     public float jumpHeight = 3f;
    13.  
    14.     public Transform groundCheck;
    15.     public float groundDistance = 0.4f;
    16.     public LayerMask groundMask;
    17.  
    18.     public Rigidbody rb;
    19.  
    20.     bool isGrounded;
    21.     bool jump;
    22.     public float turnSmoothTime = 0.1f;
    23.     float turnSmoothVelocity;
    24.  
    25.     Vector3 ve;
    26.  
    27.     private void Start()
    28.     {
    29.         rb = GetComponent<Rigidbody>();
    30.     }
    31.  
    32.     // Update is called once per frame
    33.     void Update()
    34.     {
    35.         if (Input.GetKeyDown("Jump"))
    36.         {
    37.             jump = true;
    38.         }
    39.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    40.  
    41.         if (Input.GetButtonDown("Jump"))
    42.         {
    43.             isGrounded = false;
    44.         }
    45.  
    46.         float horizontal = Input.GetAxisRaw("Horizontal");
    47.         float vertical = Input.GetAxisRaw("Vertical");
    48.         Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
    49.  
    50.     }
    51.  
    52.     void FixedUpdate()
    53.     {
    54.         Vector3 ve = rb.velocity;
    55.         ve.y += gravity * Time.deltaTime;
    56.         rb.velocity = Vector3 ve;
    57.  
    58.         if (jump && isGrounded)
    59.         {
    60.             jump = false;
    61.             isGrounded = false;
    62.             velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    63.         }
    64.         if (rotation.magnitude >= 0.1f)
    65.         {
    66.             float targetAngle = Mathf.Atan2(transform.rotation.x, transform.rotation.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
    67.             float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
    68.             rb.MoveRotation(Quaternion.Euler(0f, angle, 0f));
    69.  
    70.             Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
    71.             rb.MovePosition(moveDir.normalized * speed * Time.deltaTime + transform.position);
    72.         }
    73.     }
    74. }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    This line just doesn't make sense and the compiler is confused:
    rb.velocity = Vector3 ve;

    it should be:
    rb.velocity = ve;
     
  3. Sekudo

    Sekudo

    Joined:
    Dec 4, 2022
    Posts:
    36
    thank you, but when i tried it it gives me these two errors. if you know how to fix, that will be really amazing and i'd appreciate it so much
     

    Attached Files:

  4. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    524
    Trust the compiler... when it tells you that a name doesn't exist then it doesn't exist. You have no variable named velocity or rotation right? So that's it. Maybe rb.velocity?
     
    angrypenguin likes this.
  5. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,509
    This!

    Compiler errors can't tell you what to fix, they can only tell you where and why they got confused. In this case, you need to look at line 56 and figure out why it might be expecting a ';'.

    A semi-colon is used to mark the end of a statement. So why might it think that there's a second, un-ended statement there? It's because what's written doesn't make sense as one statement, which in turn is because there's a type name where it doesn't belong.

    This all seems confusing and opaque to begin with. Thankfully there are indeed clear rules around all of this stuff (there have to be, otherwise compilers couldn't work at all), and they'll stick with practice.
     
  6. Sekudo

    Sekudo

    Joined:
    Dec 4, 2022
    Posts:
    36
    thank you so much, but it didn't work. this is the new error that comes up. i changed them to rb.velocity and rb.rotation. i also heard that transform.rotation might work but it doesnt. i'm trying to figure out why, since in the documentation rigidbody does contain a velocity and rotation property?
     

    Attached Files:

  7. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    524
    These are new errors. I'm going to give you a hint. You code isn't close and we aren't exactly sure of the goal but it seems to involve jumping.

    For instance I don't need to know but you need to ask yourself what does this do? Why instantiate a local variable here?

    Code (CSharp):
    1. Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
     
    Sekudo likes this.
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    I see you have disregarded what I wrote you here;

    https://forum.unity.com/threads/how...-in-the-current-context.1385724/#post-8725872

    Go back and read the second sentence carefully.

    Unless you have a math degree, DO NOT manipulate those quantities, they will NEVER be useful to you. Seriously.

    This shouldn't be such a struggle. You're not listening to what we are saying.

    If you just need a CC script, start with this one:

    https://forum.unity.com/threads/a-basic-first-person-character-controller-for-prototyping.1169491/

    That one has run, walk, jump, slide, crouch... it's crazy-nutty!!
     
  9. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    524
    Not only did he ignore it everybody ignored it. Nobody is going to read 60 lines (if we count the blanks) of bloat. I just clicked on the link and I didn't read it I just estimated the number of lines.

    I'll bet that he will not go back and read any part of it carefully due in part to your holier-than-thou attitude about EVERYTHING. "Unless you have a math degree"? How about if someone is 3 years into obtaining a degree?

    Unless you have an English degree DO NOT attempt to write meaningful replies.
     
  10. Sekudo

    Sekudo

    Joined:
    Dec 4, 2022
    Posts:
    36
    so it basically is a combination of 3d brackeys platformer (with cinema machine) and a jump script, but the player got stuck in the wall so i made a thread asking about it and someone tried to help but they stopped responding and im stuck at a bunch of errors. that script actually already exists in the code, the one that you put. (line 48) i tried changing it to that but it isn't receiving the vector3 float or whatever its called, and it just stays at regular direction instead of a colored version, which i think marks that its the vector3 float. if i already called it, i have no idea whats going on,but again thank you for helping (also the thing kurt-dekker sent was just a 620-word essay telling me that i need to read the error message, which i already did but i knew both were in the rigidbody, so i thought they were already called and didn't need to be called with vector3 or anything)
     
  11. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    Just use a CharacterController! It handles collisions with walls for you.

    But, anyway...

    A quaternion is like a complex number (you know, the square root of -1), but with four terms instead of two.

    The x, y, z, (and w) terms are not angles, and you cannot interpret them as angles. You should not be doing anything with them. They're only named that because those are the traditional names for the four elements of a Vector4.

    You need to get a handle on the terminology! It's going to be very hard to work (and even harder to communicate) if you've got all this vocabulary jumbled up.

    Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;


    Line 48 is a declaration statement. A declaration looks like this:

    TypeName variableName;
    .

    It declares a new variable called "direction". The variable stores a Vector3.

    It also assigns a value to that variable. It does so by constructing a new Vector3 (hence the new keyword). The Vector3 constructor takes three parameters: the x, y, and z values. In your case, those values are the horizontal variable, a constant value of 0, and the vertical variable. You then use the normalized property of Vector3, which gives you a vector that points in the same direction, but has a length of 1.

    So, the result of this blob of code...

    new Vector3(horizontal, 0f, vertical).normalized


    ...is a Vector3, which you can then store into a variable. In fact, you MUST store it into something: C# does not allow you to compute a value without storing it!

    Since this is a declaration, it's creating a variable. If nothing else uses that variable, then it's pretty much pointless: you say "I have this new thing!" and then immediately discard it.

    Specifically, the variable goes away once the variable goes out of scope. C# scope is determined by curly braces (the fancy term here is "lexical scope").

    So, you declare a thing called "direction", and then it instantly goes out of scope.

    If you remove the Vector3 from the left side of that statement, you'd have this:

    direction = new Vector3(horizontal, 0f, vertical).normalized;


    This is an expression statement. It does not declare a new variable. It simply stores a new value into an existing variable.

    If the variable does not exist, then that's an error. I see no other declaration of a Vector3 called "direction", so that would indeed happen.

    Is that what you meant by it not "receiving" the value? I presume that "it just stays at regular direction instead of a colored version" is talking about code highlighting, which could also happen if the variable didn't exist.
     
  12. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    I'm not sure I understand your life strategy here... Why would you ignore the correct answer? It was right at the very top of the post.

    Accessing the .x, .y, .z and .w terms of a Quaternion simply are not useful in gamedev, especially for a beginner.
     
  13. Sekudo

    Sekudo

    Joined:
    Dec 4, 2022
    Posts:
    36

    i don't see why it doesn't show up, since you said

    "
    new Vector3(horizontal, 0f, vertical).normalized


    ...is a Vector3, which you can then store into a variable. In fact, you MUST store it into something: C# does not allow you to compute a value without storing it!"

    so, if the variable is created with that line of code (line 48), and i used the variable (line 64) why is there an error, if you said the variable needs to be created then used? also, heres the code again



    Code (CSharp):
    1. using System.Collections.Generic;
    2. using System.Collections;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     public CharacterController controller;
    8.     public Transform cam;
    9.  
    10.     public float speed = 6f;
    11.     public float gravity = -9.81f;
    12.     public float jumpHeight = 3f;
    13.  
    14.     public Transform groundCheck;
    15.     public float groundDistance = 0.4f;
    16.     public LayerMask groundMask;
    17.  
    18.     public Rigidbody rb;
    19.  
    20.     bool isGrounded;
    21.     bool jump;
    22.     public float turnSmoothTime = 0.1f;
    23.     float turnSmoothVelocity;
    24.  
    25.     Vector3 ve;
    26.  
    27.     private void Start()
    28.     {
    29.         rb = GetComponent<Rigidbody>();
    30.     }
    31.  
    32.     // Update is called once per frame
    33.     void Update()
    34.     {
    35.         if (Input.GetKeyDown("Jump"))
    36.         {
    37.             jump = true;
    38.         }
    39.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    40.  
    41.         if (Input.GetButtonDown("Jump"))
    42.         {
    43.             isGrounded = false;
    44.         }
    45.  
    46.         float horizontal = Input.GetAxisRaw("Horizontal");
    47.         float vertical = Input.GetAxisRaw("Vertical");
    48.         Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
    49.  
    50.     }
    51.  
    52.     void FixedUpdate()
    53.     {
    54.         Vector3 ve = rb.velocity;
    55.         ve.y += gravity * Time.deltaTime;
    56.         rb.velocity = ve;
    57.  
    58.         if (jump && isGrounded)
    59.         {
    60.             jump = false;
    61.             isGrounded = false;
    62.             velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    63.         }
    64.         if (direction.magnitude >= 0.1f)
    65.         {
    66.             float targetAngle = Mathf.Atan2(transform.rotation.x, transform.rotation.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
    67.             float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
    68.             rb.MoveRotation(Quaternion.Euler(0f, angle, 0f));
    69.  
    70.             Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
    71.             rb.MovePosition(moveDir.normalized * speed * Time.deltaTime + transform.position);
    72.         }
    73.     }
    74. }
     
  14. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    Because the variable goes out of scope.

    Code (CSharp):
    1. if (whatever) {
    2.   float myVar = 3;
    3. }
    4.  
    5. myVar += 1;
    myVar
    stops existing once its block ends. Therefore, you will get an error.

    You need to declare
    direction
    in a more appropriate place -- like with the rest of your fields, at the top.

    A field is a variable that is declared in the definition of a class. They're variables attached to each instance of the class (e.g. each PlayerMovement component in your scene).

    You've already got plenty of fields, like "isGrounded" and "jump". Declare "direction" as one, and you'll be able to set it in one function and read it from another function.
     
  15. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    524
    Most humans recognize that how we say something is nearly as important as what we say. Nobody digs through 60+line replies especially those by someone who can't "communicate" an opts instead to imply they are the teacher and you are the lowly student. Or better yet that they are the school Principal and imply they know more than any of the other teachers.

    Lately you've told people to not try to write a clock routine and now you suggest someone needs a degree in mathematics not to mention your favorite topic of expertise "how nobody should use object pooling". Your first reply to me explained how putting all your code in the Update method was a good idea and how events were suspect.

    You type and type and type with little regard to the topic. Try to "interact". Before you post 5 video links consider asking the poster if they would like some suggestions for video links. Rather than type "don't touch those algorithms" try the simpler "trajectory formulas can be extremely tricky".

    You aren't here to tell people what to do. You put people off by your attitude and the "if you had just listened to me" sort of replies. I for instance scan your replies pretty much knowing few supported facts will be presented and it will be 98% your opinion alone.

    If you lighten up more people might listen.
     
  16. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    This is fascinating... There is so much mind-reading in your posts!

    and

    and

    and

    Not only that but you're getting pretty much all of the mind-reading wrong.

    Scott Adams has a great treatise on the dangers of mind reading and the mental prison it creates.

    Screen Shot 2023-01-13 at 8.19.09 AM.png
    Full text:

    https://talkwithjon.com/thoughts-on-loserthink-by-scott-adams/

    I'm just trying to help here and I get plenty of "Thanks, that worked!" posts.

    I suppose we'll just have to agree to disagree.
     
  17. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    524
    It is time to take a short step back. I'm assuming that you didn't write this code from scratch and it has turned into the sort of thing that should simply be rewritten rather than refactored.

    The code presented here is quite similar. The beauty of this approach being that you could get it working (probably) and then customize it from there. If it breaks you only need to look at what you changed since the last time it worked.

    https://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html

    Notice that your variable names are inconsistent. This doesn't mean it won't work but it adds to confusion (for you) when you have to read it. You have isGrounded but then you call something jump. It is plainly isJumping right?
     
  18. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    524
    @Kurt-Dekker you are quoting Scott Adams? That pretty much explains everything.

    Let me find out how "ignore" works on this forum.
     
    Kurt-Dekker likes this.
  19. Sekudo

    Sekudo

    Joined:
    Dec 4, 2022
    Posts:
    36
    youre right, do i just replace all jump with isGrounded and then remove the bool create thing for it at the top?
     
  20. Sekudo

    Sekudo

    Joined:
    Dec 4, 2022
    Posts:
    36
    thank you! that helped, and now that error is gone. but, it created two new errors. they are both this: (same line, character placements are 37 and 53)

    Assets/Scripts/PlayerMovement.cs(27,37): error CS0236: A field initializer cannot reference the non-static field, method, or property 'PlayerMovement.horizontal'

    [BTW NO IDEA IF ANY OF THE FOLLOWING IS CORRECT]

    i'm guessing its because it doesn't know what horizontal and vertical are, (lines 49 and 50) which are used so that the player moves in the direction of the camera when you press 'w', to the sides when you press 'a' and 'd', and away from it when you press 's', like roblox or minecraft movement without including sprinting

    also, since it's in the top instead of update(), won't it not track the direction anymore, and only on the first frame?


    NEW CODE
    Code (CSharp):
    1. using System.Collections.Generic;
    2. using System.Collections;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     public CharacterController controller;
    8.     public Transform cam;
    9.  
    10.     public float speed = 6f;
    11.     public float gravity = -9.81f;
    12.     public float jumpHeight = 3f;
    13.  
    14.     public Transform groundCheck;
    15.     public float groundDistance = 0.4f;
    16.     public LayerMask groundMask;
    17.  
    18.     public Rigidbody rb;
    19.  
    20.     bool isGrounded;
    21.     bool jump;
    22.     public float turnSmoothTime = 0.1f;
    23.     float turnSmoothVelocity;
    24.  
    25.     Vector3 ve;
    26.  
    27.     Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
    28.  
    29.  
    30.     private void Start()
    31.     {
    32.         rb = GetComponent<Rigidbody>();
    33.     }
    34.  
    35.     // Update is called once per frame
    36.     void Update()
    37.     {
    38.         if (Input.GetKeyDown("Jump"))
    39.         {
    40.             jump = true;
    41.         }
    42.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    43.  
    44.         if (Input.GetButtonDown("Jump"))
    45.         {
    46.             isGrounded = false;
    47.         }
    48.  
    49.         float horizontal = Input.GetAxisRaw("Horizontal");
    50.         float vertical = Input.GetAxisRaw("Vertical");
    51.     }
    52.  
    53.     void FixedUpdate()
    54.     {
    55.         Vector3 ve = rb.velocity;
    56.         ve.y += gravity * Time.deltaTime;
    57.         rb.velocity = ve;
    58.  
    59.         if (jump && isGrounded)
    60.         {
    61.             jump = false;
    62.             isGrounded = false;
    63.             velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    64.         }
    65.         if (direction.magnitude >= 0.1f)
    66.         {
    67.             float targetAngle = Mathf.Atan2(transform.rotation.x, transform.rotation.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
    68.             float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
    69.             rb.MoveRotation(Quaternion.Euler(0f, angle, 0f));
    70.  
    71.             Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
    72.             rb.MovePosition(moveDir.normalized * speed * Time.deltaTime + transform.position);
    73.         }
    74.     }
    75. }
     
  21. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    524
    I've capitalized your question for a particular reason. I've reviewed the code you have posted several times and recognize that I get nothing out of this. Nope you don't "just replace" anything. The code is "wrong" and is irrecoverably wrong. Seriously it isn't a few changes away from working.

    Ask yourself, do I understand what I am reading. That must be answer no right? I don't understand it and I've been a software developer for 40+ years.

    Look at the inconsistencies which are demonstrative of "I don't get it". You have a public Transform named cam and you reference cam once. No problem but that indicates that you must have assigned cam in the Unity inspector. You've done something similar with the groundCheck Transform. Now look at the public Rigidbody named rb. You GetComponent that reference in Start. Why? Not why do you reference the Rigidbody (I know why) but why didn't you assign this in the same way in the inspector? What have you gained by not being consistent in these cases?

    You have defined a Vector3 named ve as part of the class. Is it ever referenced? I don't see it. I see a local definition named ve in the FixedUpdate method.

    It doesn't matter than perhaps 25% of the code makes no sense or isn't needed but that is when I typically asked the developer to stop trying to fix it and told them I would rewrite it. Too many hours have been spent already and even if it works you should see it remains nearly broken.

    Ask yourself... I have added the following lines so that...? And see if you can come up with an answer.

    Code (CSharp):
    1.         float horizontal = Input.GetAxisRaw("Horizontal");
    2.         float vertical = Input.GetAxisRaw("Vertical");
    3.         Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
    4.  
    Do work on the problem by all means it is a good challenge but look at the doc that Unity provided (or a short tutorial) and understand the concepts.
     
  22. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    524
    You're not improving it you are changing it. You can guess things but you need to verify your guess is correct and importantly your guess is incorrect in this case.

    Try generating some C# code to experiment with and don't try to have it do anything important. You need to understand what a class is, scope, data types, etc. If you get this working you will go through all the same guesses the next time you try and make something happen. The project will never be finished and you could get discouraged and quit. Put in some up-front learn the basics effort instead.