I’m pretty new to coding and making unity game and I code in c#. and I made this code:
using UnityEngine;
public class PlayerMovment : MonoBehaviour
// This is a reference to the Rigidbody component called "rb"
public Rigidbody rb;
public float forwardForce = 2000f;
public float sidewaysForce = 500f;
// Start is called before the first frame update
void FixedUpdate()
{
rb.AddForce(0, 200, 500);
}
// Add a forward force
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
if (Input.GetKey("d") )
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0);
}
if (Input.GetKey("a") )
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0);
}
and in the console i see LOTS of errors: cs1022, cs1003, cs1001, cs1031, cs1026, cs8124, cs0116… so can somone please help me and fix the code i wrote?
Everything after line 21 is outside a function, which it can’t be. Those lines of code need to be inside the { } brackets of the FixedUpdate() function.
I believe you are also missing a final } to close out the class at the end - make sure your brackets are always matched in pairs.
In programming 100% of all capitalization, spelling, and especially punctuation and ordering has to be correct.
Step back a moment to wherever you dug this code up and start from scratch because programming is not like typing text messages on your cel phone where the other person can figure out what you mean. You must be precise.
If you still have a specific problem, here is how to report your problem productively in the Unity3D forums:
On top to what the others said so far (all of which is correct!), you also removed the opening bracket for your class, as well as the closing bracket. So the compiler is just super confused about pretty much everything, because it has no idea what goes where, or relates to other stuff in what way.
As was already mentioned, it’s extremely important you pay attention to “small” details like that, since every symbol outside of comments (following //) is important, and not putting them there changes what the computer thinks you meant, or just outright makes it impossible for it to work with. Computers are dumb. They have no common sense, they have no idea what you might mean or not - they simply take what you tell them symbol by symbol and execute the exact instructions you gave them.