Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Question What's the basic code to get a character to move left/right and jump?

Discussion in 'Getting Started' started by RichAllen2021, Oct 23, 2021.

  1. RichAllen2021

    RichAllen2021

    Joined:
    Jul 19, 2016
    Posts:
    651
    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.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,796
    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.
     
  3. RichAllen2021

    RichAllen2021

    Joined:
    Jul 19, 2016
    Posts:
    651
    Thanks Joe but I wanted someone to actually POST the code.
     
  4. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    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...
     
    Joe-Censored and JoeStrout like this.
  5. jbnlwilliams1

    jbnlwilliams1

    Joined:
    May 21, 2019
    Posts:
    246
    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.
     
    Joe-Censored likes this.
  6. RichAllen2021

    RichAllen2021

    Joined:
    Jul 19, 2016
    Posts:
    651
    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?!
     
  7. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    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.
     
  8. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,825
    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.
    https://docs.unity3d.com/ScriptReference/Transform.Translate.html

    You can use the physics system for movement. You do this by adding forces, setting velocity directly, or using MovePosition.
    https://docs.unity3d.com/ScriptReference/Rigidbody2D.MovePosition.html

    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.
     
    mattrav likes this.
  9. Valjuin

    Valjuin

    Joined:
    May 22, 2019
    Posts:
    481
  10. jbnlwilliams1

    jbnlwilliams1

    Joined:
    May 21, 2019
    Posts:
    246
unityunity