Movement script error

StolasTheOwl

Member
Joined
Sep 9, 2021
Messages
5
Programming Experience
Beginner
Im new to coding and I copied this script to get a general idea of it, but my unity client is saying that it has an error. The Error specifically is CS1002: expected. How do I fix this?
C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovementScript : MonoBehaviour
{

    public CharacterController controller;
 
    public float speed = 12f;
    public float gravity = -9.81f;
    public float jumpHeight = 3f;

    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;

    Vector3 velocity;
    bool isGrounded;

    // Update is called once per frame
    void Update()
    {

      isGrounded = Physics.CheckSphere(groundCheck.Position, groundDistance, groundMask);

      If(isGrounded && velocity.y < 0)

            velocity.y = 2f;
      float x = Input.GetAxis("Horizontal");
      float z = Input.GetAxis("Vertical");

      Vector3 move = transform.right * x + transform.forward * z;

      controller.Move(move * speed * Time.deltaTime);

      If(Input.GetButtonDown("Jump") && isGrounded);

            velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);

      velocity.y += gravity * Time.deltaTime;

      controller.move(velocity * Time.deltaTime);
    }
}
 
Last edited by a moderator:
What line is that error pointing to?
 
it says that its pointing to (27,39) but im not sure whats actually wrong with the lines.
1631209069615.png
 
On line 27, you have a capital 'I' in what looks to be an if statement. C# is case sensitive, so it should lowercase 'i'. With the uppercase 'I', the C# compiler thinks that you are calling a function called If and trying to pass parameters to it, a function call needs to be separated from the next statement by a ';'. The compiler is saying that it is expecting to find that semicolon.
 
Also, on line 37, you again put a capital 'I' and looks like you accidentally put in a ';' at the end of the line. You'll need to change the 'I' to an 'i' and remove the semicolon.
 
They are both casing issues. I've never used Unity myself but, based on the documentation, property names start with lower-case letters and method names start with upper-case letters. That means that Position should be position and move should be Move.

You really ought to take notice of what Intellisense tells you as it would have shown the correct case. If you copied code from elsewhere rather than typing it, you can always put the cursor in the text of the code and press Ctrl+Space to bring up Intellisense, or just delete the offending part and type the dot again to bring up all options. You also should have read the same documentation I did, which I found with a simple web search. Like I said, I've never used Unity so, if I can find that information, you can too.
 
They are both casing issues. I've never used Unity myself but, based on the documentation, property names start with lower-case letters and method names start with upper-case letters. That means that Position should be position and move should be Move.

You really ought to take notice of what Intellisense tells you as it would have shown the correct case. If you copied code from elsewhere rather than typing it, you can always put the cursor in the text of the code and press Ctrl+Space to bring up Intellisense, or just delete the offending part and type the dot again to bring up all options. You also should have read the same documentation I did, which I found with a simple web search. Like I said, I've never used Unity so, if I can find that information, you can too.
Thank you so much for the suggestion! I looked at my code again and noticed the discrepancies.
 
Also, on line 37, you again put a capital 'I' and looks like you accidentally put in a ';' at the end of the line. You'll need to change the 'I' to an 'i' and remove the semicolon.
Thank you for the help Sky! Your suggestions helped me understand a little more on how to organize things. :)
 
Back
Top Bottom