Search Unity

Assets\Scripts\PlayerMovement.cs(44,82): error CS1002: ; expected

Discussion in 'Scripting' started by Metaggod, Jan 14, 2021.

Thread Status:
Not open for further replies.
  1. Metaggod

    Metaggod

    Joined:
    Jan 14, 2021
    Posts:
    7
    Please I'm just starting out on Unity and am getting this error message - Assets\Scripts\PlayerMovement.cs(44,82): error CS1002: ; expected, I need help sorting it out...

    This is my Code... Thanks

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.  
    8.     // This is a reference to the Rigidbody component called "rb"
    9.     public Rigidbody rb;
    10.  
    11.     public float forwardForce = 2000f;
    12.     public float sidewaysForce = 500f;
    13.     private Vector2 lastMousePos;
    14.  
    15.     // We marked this as "Fixed"Update because we
    16.     // are using it to mess with physics.
    17.     void FixedUpdate()
    18.     {
    19.         // Add a forward force
    20.         rb.AddForce(0, 0, forwardForce * Time.deltaTime);
    21.  
    22.         if (Input.GetKey("d"))
    23.         {
    24.             rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
    25.         }
    26.  
    27.         if (Input.GetKey("a"))
    28.         {
    29.             rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
    30.         }
    31.  
    32.         Vector2 deltaPos = Vector2.zero;
    33.  
    34.         if (Input.GetMouseButton(0))
    35.         {
    36.             Vector2 currentMousePos = Input.mousePosition;
    37.  
    38.             if (lastMousePos == Vector2.zero)
    39.                 lastMousePos = currentMousePos;
    40.  
    41.             deltaPos = currentMousePos = lastMousePos;
    42.             lastMousePos = currentMousePos;
    43.  
    44.             Vector3 force = new Vector3(deltaPos.x, 0, deltaPos.y) + forwardForce
    45.             rb.AddForce(force);  
    46.         }
    47.         else
    48.         {
    49.             lastMousePos = Vector2.zero;
    50.         }
    51.     }
    52. }
    53.  
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    It's exactly as the error says... you're missing a semicolon
    ;
    at the end of line 44. Note that the line number of the problem is in your error message.
     
  3. Metaggod

    Metaggod

    Joined:
    Jan 14, 2021
    Posts:
    7
    Thanks, it cleared that error but changed to this...
    --- Assets\Scripts\PlayerMovement.cs(44,29): error CS0019: Operator '+' cannot be applied to operands of type 'Vector3' and 'float'
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Indeed, you cannot add a Vector3 and a float as you are trying to do. Maybe you meant to scale the Vector3 by forwardForce? Replace + with *. That will clear your compile error but I have no idea if it's what you want your code to do.
     
    Metaggod likes this.
  5. Metaggod

    Metaggod

    Joined:
    Jan 14, 2021
    Posts:
    7
    Thanks it worked, I am trying to make a game like "Color Bumb 3d", but I've been having issues with the code
     
Thread Status:
Not open for further replies.