Search Unity

Best method for movement in 2D?

Discussion in '2D' started by Lord_Scio, Sep 14, 2018.

  1. Lord_Scio

    Lord_Scio

    Joined:
    Sep 14, 2018
    Posts:
    4
    Hey, im new to Unity and would like to make my character move. However, i tried some tutorials and nothing worked at all.. i have no clue what im doing wrong since i dont even get errors or warnings.

    Oh, and i dont want platformer movement, more like from top down. Pretty much like the mobile game "Soul Knight" which im inspired from.
     
  2. chemimartinez77

    chemimartinez77

    Joined:
    Apr 8, 2016
    Posts:
    25
    First of all, welcome!

    Well, could you let us see some of your code?

    Do you want your character to be controlled with the keyboard?

    Give us some info!
     
  3. Lord_Scio

    Lord_Scio

    Joined:
    Sep 14, 2018
    Posts:
    4
    Well, i got a bit mad because in the vid everything worked just fine and i did exactly the same... but it didnt work at all. So i deleted the project.
    Yea, i want to control it with the keyboard, W A S D ^^

    This is the Tutorial i where i got the code from, i thought this looks okay so i tried
    www.youtube.com/watch?v=1WG791jyZ6M&index=2&list=WL&t=0s

    is it maybe because i didnt had a spritesheet? i just set the sprite mode on single
     
    Last edited: Sep 14, 2018
  4. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Code (CSharp):
    1.  if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) // Left movement
    2.         {
    3.            [code=CSharp]transform.Translate(Pspeed * Input.GetAxis("Horizontal") * Time.deltaTime, 0f, 0f);
    GetComponent<Renderer>().material = lftFace;
    Pshoot.gunPass.SetActive(false);
    Pshoot.gun1Pass.SetActive(true);
    [/code]

    Here's and example of how I did it. I had to specify which keypress in order to render materials and toggle gun facing side. However, all you need in 2D is
    Code (CSharp):
    1. transform.Translate(Pspeed * Input.GetAxis("Horizontal") * Time.deltaTime, 0f, 0f);
    Just be sure to create a float variable Pspeed and start off with about 5f. This is your player speed. If you want to be able to access it inside Unity for quick changes:

    Code (CSharp):
    1. [Header "Player Move attributes"]
    2. [SerializeField]
    3. private float Pspeed = 5f;
    4. //SerializeField makes private variables show up in the inspector
    The code I provided you with to Input.GetAxis("Horizontal") will make your player controller move Left / Right across the X-Axis with either "A", "Leftarrow", "D", RightArrow.

    NOTE - You DON'T have to use the GetKey(KeyCode) unless you want certain things to happen upon button press. Just know that GetKeyDown and GetKeyUp both work as well. Depending on your needs. Mine is raycasting so GetKey gives a non stop ray whilst walking.

    P.S.S. - When you copy/paste code as a beginner programming, take the time to look up the keywords. That will sometimes help you understand what's happening. The rest is just using code over and over and one day you just know the routine.

    Good luck to ya new programmer! :)
     
    TheReaperSAD likes this.
  5. Lord_Scio

    Lord_Scio

    Joined:
    Sep 14, 2018
    Posts:
    4
    Thanks for the quick response. However, if i would copy it i would get a bunch of errors whatsoever. For Example, Tupel isnt available in C# 4, use version 7.0 or higher. Does that mean im using a old version of C# ?? How do i update it if so?
     
  6. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    What version of Unity are you using? I don't think your C# would need to be updated. If anything, it would be your compiler which might be Monobehavior or Visual Studio. However, I am pretty sure that it will be up-to-date if you have the most recent version.

    If you are using Unity 4(or anything below 5.3.8) you may need to recode a lot of your scripts because it may be obsolete code in Unity 5+ versions.
     
  7. Lord_Scio

    Lord_Scio

    Joined:
    Sep 14, 2018
    Posts:
    4
    I have no clue what was wrong with the other codes that i tried, but this one works just perfect for me. I just hope its a good one which wont cause problems later

    Code (CSharp):
    1.  [SerializeField]
    2.     private float speed;
    3.  
    4.     private Rigidbody2D rb;
    5.     private Vector2 moveVelocity;
    6.  
    7.     private void Start()
    8.     {
    9.         rb = GetComponent<Rigidbody2D>();
    10.     }
    11.  
    12.     private void Update()
    13.     {
    14.         Vector2 moveInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
    15.         moveVelocity = moveInput.normalized * speed;
    16.     }
    17.  
    18.     void FixedUpdate()
    19.     {
    20.         rb.MovePosition(rb.position + moveVelocity * Time.fixedDeltaTime);
    21.     }
     
  8. moazzamcxv

    moazzamcxv

    Joined:
    Jan 1, 2021
    Posts:
    1
    whoa! im also working on a Soul Knight inspired game, and it would be helpful if u sent me a demo project :)
     
  9. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @moazzamcxv

    "...it would be helpful if u sent me a demo project"


    Code begging in 2 year old thread.

    There doesn't seem to be anything specific in Soul Knight movement. Any top down movement tutorial will do.

    So maybe start from https://learn.unity.com/ ? Or YouTube. Or some tutorial you can find with google.