I started unity today. I wanted to make a first person controller, so I went into the learn area to see if I could find anything. I found some movement code, so I copied it into a script and made some adjustments. I then, when testing, realized that I couldn’t move side to side. Can someone help?
It might be a bit of a large first step if you just started today but… try commenting out all the code in FixedUpdate(). Clearly nothing happens as one would expect. Then uncomment the moveVertical stuff. You should notice you named the Vector3 “movement” not differentiating it. No problem yet but it will turn into one.
Now comment that again and uncomment the moveHorizontalOne section. Hey what happened to the movement Vector3 and why is nothing using moveRight?
And don’t name things moveHorizontalOne and moveHorizontalTwo if they have something to do with left and right.
But if you wanna debug what you have, see below for debugging.
If you just want a functioning character controller, here’s a great starter:
That one has run, walk, jump, slide, crouch… it’s crazy-nutty!!
If you want to stick with what you have it just sounds like you wrote a bug… and that means… time to start debugging!
By debugging you can find out exactly what your program is doing so you can fix it.
Use the above techniques to get the information you need in order to reason about what the problem is.
You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.
Once you understand what the problem is, you may begin to reason about a solution to the problem.
Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.
Two steps to tutorials and / or example code:
do them perfectly, to the letter (zero typos, including punctuation and capitalization)
stop and understand each step to understand what is going on.
If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.
Check input in Update(), not FixedUpdate(). Though the code may have other issues too. I don’t see the need for the bools, just do
if (Input.GetKey) { }
Use ChatGPT to help you. It’s invaluable for stuff like this. Give it a prompt like
“create a unity script for basic player/rigidbody control using standard keyboard input, with speed and jumpSpeed parameters”
I can 99% guarantee the first thing it produces will work. Then read through it and ask questions about anything you don’t understand. You can also paste your code and ask it to tweak/correct it, which is another good approach.