Search Unity

2D character controller

Discussion in 'Getting Started' started by Green11001, Aug 2, 2019.

  1. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    I am trying to create a character controller for a 2d platformer (I mean without a rigidbody). I have looked online but could not find any good tutorials. Does anyone know where a good tutorial for this is? Or is it better to just try to find things that are related to it, and piece information together? And if so, can I get some pointers on where to start? Thanks.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I have exactly such a thing here: 2D Animation Methods in Unity. Be sure to download the demo project, since Gamasutra mangles the code a bit.
     
  3. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    Where is the example project?
     
  4. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    Oh nevermind I found it
     
  5. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    How would one implement collisions and walls into this?
     
  6. Nanior

    Nanior

    Joined:
    May 4, 2019
    Posts:
    101
    Will I show a C# script for 2D character controller?
     
  7. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
  8. Nanior

    Nanior

    Joined:
    May 4, 2019
    Posts:
    101
    Will I show the script?
     
  9. Nanior

    Nanior

    Joined:
    May 4, 2019
    Posts:
    101
    Say yes if you want.
     
  10. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
  11. Nanior

    Nanior

    Joined:
    May 4, 2019
    Posts:
    101
    And, it will be with a rigidbody2D
     
  12. Nanior

    Nanior

    Joined:
    May 4, 2019
    Posts:
    101
    And to move you need to use WAD.
    W to jump, A to move left, D to move right
     
  13. Nanior

    Nanior

    Joined:
    May 4, 2019
    Posts:
    101
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Player : MonoBehaviour
    4. {
    5.     Rigidbody2D Rig2D;
    6.     public float Speed = 5f;
    7.     public float JumpForce;
    8.  
    9.     void Start()
    10.     {
    11.         Rig2D = GetComponent<Rigidbody2D>();
    12.     }
    13.  
    14.     void FixedUpdate()
    15.     {
    16.         if (Input.GetKey(KeyCode.A))
    17.         {
    18.             transform.position -= transform.right * Speed *
    19.                 Time.deltaTime;
    20.         }
    21.         if (Input.GetKey(KeyCode.D))
    22.         {
    23.             transform.position += transform.right * Speed *
    24.                 Time.deltaTime;
    25.         }
    26.     }
    27.     private void OnCollisionStay2D(Collision2D Collision2D)
    28.     {
    29.         if (Input.GetKeyDown(KeyCode.W))
    30.         {
    31.             Rig2D.AddForce(new Vector2(0, JumpForce * 50));
    32.         }
    33.     }
    34. }
    sorry, still quite Buggy...
     
  14. Nanior

    Nanior

    Joined:
    May 4, 2019
    Posts:
    101
    Please name your script "Player"
     
  15. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    Hmmm not quite what Im looking for
     
  16. Nanior

    Nanior

    Joined:
    May 4, 2019
    Posts:
    101
    And what do you want?
    What game do you want to make?
    Do you make your terrain with tilemap?
     
  17. Nanior

    Nanior

    Joined:
    May 4, 2019
    Posts:
    101
    Oh, yes, there is a bug, you can clim up the wall.
     
  18. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    A controller that doesnt use rigidbody mainly because it has difficulty handling slopes and isnt quite as easy to fine tune as a character controller
     
  19. Nanior

    Nanior

    Joined:
    May 4, 2019
    Posts:
    101
    No rigidbody?!
     
  20. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    Yes.... a character controller that functions without a rigidbody
     
  21. Nanior

    Nanior

    Joined:
    May 4, 2019
    Posts:
    101
    than i don't know how to do it
     
  22. Nanior

    Nanior

    Joined:
    May 4, 2019
    Posts:
    101
    Why no rigidbody?
     
  23. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    Because, if there is a slope then the rigidbody may slide down/get stuck when moving up it which is not ideal for some platformers
     
  24. Nanior

    Nanior

    Joined:
    May 4, 2019
    Posts:
    101
    Well, I will try to find a way to fix it.
     
  25. Dudibro09

    Dudibro09

    Joined:
    Oct 23, 2020
    Posts:
    2
    using UnityEngine;

    public class CharacterController2D : MonoBehaviour
    {
    public float WalkSpeed = 10;
    public float JumpForce = 5;

    void Update()
    {
    var Move = Input.GetAxis("Horizontal");
    transform.position += new Vector3(Move, 0, 0) * Time.deltaTime * WalkSpeed;

    if (Input.GetButtonDown("Jump") && GetComponent<Rigidbody2D>().velocity.y == 0)
    {
    GetComponent<Rigidbody2D>().AddForce(new Vector2(0, JumpForce), ForceMode2D.Impulse);
    }
    }
    }


    here is my 2d characterController. If you want to copy this make shore that you have a rigidbody on your player and freeze position x and freeze rotation z on contraints in your rigidbody component and change your scripts name to CharacterController2D or else its not gonna work.
     
  26. Dudibro09

    Dudibro09

    Joined:
    Oct 23, 2020
    Posts:
    2
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CharacterController2D : MonoBehaviour
    4. {
    5. public float WalkSpeed = 10;
    6. public float JumpForce = 5;
    7.  
    8. void Update()
    9. {
    10. var Move = Input.GetAxis("Horizontal");
    11. transform.position += new Vector3(Move, 0, 0) * Time.deltaTime * WalkSpeed;
    12.  
    13. if (Input.GetButtonDown("Jump") && GetComponent<Rigidbody2D>().velocity.y == 0)
    14. {
    15. GetComponent<Rigidbody2D>().AddForce(new Vector2(0, JumpForce), ForceMode2D.Impulse);
    16. }
    17. }
    18. }