Search Unity

2D character controller

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

  1. A_Person_Named_Dart

    A_Person_Named_Dart

    Joined:
    Apr 14, 2018
    Posts:
    330
    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:
    8,209
    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. A_Person_Named_Dart

    A_Person_Named_Dart

    Joined:
    Apr 14, 2018
    Posts:
    330
    Where is the example project?
     
  4. A_Person_Named_Dart

    A_Person_Named_Dart

    Joined:
    Apr 14, 2018
    Posts:
    330
    Oh nevermind I found it
     
  5. A_Person_Named_Dart

    A_Person_Named_Dart

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

    Nanior

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

    A_Person_Named_Dart

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

    Nanior

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

    Nanior

    Joined:
    May 4, 2019
    Posts:
    99
    Say yes if you want.
     
  10. A_Person_Named_Dart

    A_Person_Named_Dart

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

    Nanior

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

    Nanior

    Joined:
    May 4, 2019
    Posts:
    99
    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:
    99
    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:
    99
    Please name your script "Player"
     
  15. A_Person_Named_Dart

    A_Person_Named_Dart

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

    Nanior

    Joined:
    May 4, 2019
    Posts:
    99
    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:
    99
    Oh, yes, there is a bug, you can clim up the wall.
     
  18. A_Person_Named_Dart

    A_Person_Named_Dart

    Joined:
    Apr 14, 2018
    Posts:
    330
    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:
    99
    No rigidbody?!
     
  20. A_Person_Named_Dart

    A_Person_Named_Dart

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

    Nanior

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

    Nanior

    Joined:
    May 4, 2019
    Posts:
    99
    Why no rigidbody?
     
  23. A_Person_Named_Dart

    A_Person_Named_Dart

    Joined:
    Apr 14, 2018
    Posts:
    330
    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:
    99
    Well, I will try to find a way to fix it.