Above.
I managed to create a character in GIMP, and import it as a sprite to my own game, but how do I write the script to get it to move left and right and jump in a platform game scenario?
Sorry if it’s a daft question but I’m a total noob.
If you’re presenting code to me, remember to use code tags.
I wrote an article about this a few years ago, but it all still applies: 2D Animation Methods in Unity.
Note that Gamasutra (now Game Developer) doesn’t format code perfectly, even losing some punctuation here and there, so the best way to study it would be to download the project and look at the code at the same time you read the article.
Thanks Joe but I wanted someone to actually POST the code.
Break it down! As a first step, can you detect the left and right arrow keys? In the posted code above, it looks like they are using both GetButton and GetButtonDown in Update(). At that point after you’ve greatly simplified the code, just output Debug.Log. Now you have a working project that detects the left and right arrow keys! Very exciting. Now move onto the next step…
3 Likes
There are many, many tutorials and examples of this on you tube. Some of them even have links to the project with all code and other assets.
1 Like
I know but how hard is it to get someone to actually post the damn code without having to be told to watch YouTube footage and keep pausing at the right time to get the correct code?!
Do you have the left-right arrow keys working yet? Please show your code that you’ve tried so far. The code posted above wasn’t in a video, and you only need a tiny bit of it, as mentioned.
There’s several different kinds of movement in Unity.
You can move using the transform, such as changing the value of transform.position directly, or using a method like transform.Translate.
You can use the physics system for movement. You do this by adding forces, setting velocity directly, or using MovePosition.
For 3D there’s the CharacterController. I don’t know if there is a 2D equivalent or if you can just use the regular CharacterController for 2D.
Each kind of movement has its pros and cons. They also are pretty complicated where understanding the details matter, so I’d recommend going through the documentation or video tutorials rather than just picking one and taking random example code without first understanding what exactly it is doing and not doing.
1 Like
Google Search, “Unity 2d Movement Code” , second result, 2d movement unity Code Example (codegrepper.com), all kinds of movement code for you to cut and paste to your delight.