Search Unity

Character Controller 2D

Discussion in '2D' started by Mish, Jul 8, 2014.

  1. Mish

    Mish

    Joined:
    Apr 23, 2010
    Posts:
    96
    Hi

    I have been wondering what is the best way to make a character controller in the new 2D system? Since there is no build in CharacterController component like for 3D. I am currently using rigidbodies and colliders, but the character controller had some nice features like grounded check and checking from which side you are colliding with something.

    Would it be a good idea to use a 3D character controller with the 2D system? And would it even work with it properly?
    So far the only examples I have seen for the new 2D system use the rigidbody approach. Is there any particalur reason for that?
     
  2. Vitor_r

    Vitor_r

    Joined:
    May 23, 2013
    Posts:
    93
    There is a CharacterController2D in this asset made by Unity Sample Assets (beta), it works fine for platformers and runners. There is a also a video tutorial of it.
     
  3. Mish

    Mish

    Joined:
    Apr 23, 2010
    Posts:
    96
    Thanks! However, this example is still using rigidbodies and colliders instead of a special CharacterController component.
     
  4. Dunkelheit

    Dunkelheit

    Joined:
    Sep 3, 2013
    Posts:
    81
    You could take a look at this tutorial on Youtube. This is brand new and cover the new features and fix on 4.5, so far one of the best 2D Platform turorial.
     
    Mish likes this.
  5. gruddlebug

    gruddlebug

    Joined:
    Mar 11, 2013
    Posts:
    65
  6. Dunkelheit

    Dunkelheit

    Joined:
    Sep 3, 2013
    Posts:
    81
    @gruddlebug

    Is this Character Controller code more similar to Mario Platform style or something else?
     
  7. gruddlebug

    gruddlebug

    Joined:
    Mar 11, 2013
    Posts:
    65
    It can do both physics and non-physics movement
     
  8. Mish

    Mish

    Joined:
    Apr 23, 2010
    Posts:
    96
    Would setting the velocity directly on a rigidbody without using AddForce for movement be a good way to do it?
    I am not trying to achieve a mario style gameplay but a more realistic one, but at the same time I dont want any unforeseen forces suddenly make the character jump/move differently in some cases. Since many people try to make their own controllers with raycasting and for instance unity use the rigidbody approach in their tutorial, I would like to know the cons and pros of both.
     
    Last edited: Jul 9, 2014
  9. Recon03

    Recon03

    Joined:
    Aug 5, 2013
    Posts:
    845
    Depends on your movement. I work with 2D games, I use my own controllers, and yes I use the raycast to check the ground and, I make use of it with Physics based games.

    This is using the old style before they added the 2D stuff...

    Also setting the velocity for movement is fine, if you use it properly. and again depends on what your making. I have made 20 different controllers and it all depends on the game and the movement required, jumping, running, etc.
     
  10. martin101

    martin101

    Joined:
    Dec 12, 2014
    Posts:
    16
    @Recon03 Can you please upload some of you charactrerController to github
    i will be happy to fork and use it :)
     
  11. Athos-Krul

    Athos-Krul

    Joined:
    Aug 12, 2014
    Posts:
    24
    Hi guys .How could i change the script so when i have a long press it will jump higher and vice versa
     
  12. Gamingbir

    Gamingbir

    Joined:
    Apr 1, 2014
    Posts:
    197
    Was wondering how to make a duck/sit animation with this controller. Or wait animation. Say if the player waits for 5 or more seconds he plays wait animation
     
  13. Guirao

    Guirao

    Joined:
    Nov 24, 2012
    Posts:
    68
    This has nothing to do with CharacterController2D. CharacterController2D Moves and makes collisions only.
     
  14. Mursuarts

    Mursuarts

    Joined:
    Jun 3, 2015
    Posts:
    1
    No Jump :D


    using UnityEngine;
    using System.Collections;

    public class PlayerController : MonoBehaviour {

    public float maxspeed = 4;
    public float jumpforce = 558;
    public Transform groundCheck;
    public LayerMask whatIsGround;
    [HideInInspector]
    public bool lookingRight = true;

    private Rigidbody2D rb2d;
    private Animator anim;
    private bool isGrounded = false;
    // Use this for initialization
    void Start () {
    rb2d = GetComponent<Rigidbody2D>();
    anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update () {

    }


    void FixedUpdate()
    {
    float hor = Input.GetAxis ("Horizontal");

    anim.SetFloat ("Speed", Mathf.Abs (hor));

    rb2d.velocity = new Vector2(hor * maxspeed, rb2d.velocity.y);

    isGrounded = Physics2D.OverlapCircle (groundCheck.position, 0.15F, whatIsGround);

    anim.SetBool("IsGrounded",isGrounded);

    if ((hor > 0 && !lookingRight) || (hor < 0 && lookingRight))
    Flip ();
    }

    public void Flip()
    {
    lookingRight = !lookingRight;
    Vector3 myScale = transform.localScale;
    myScale.x *= -1;
    transform.localScale = myScale;
    }
    }
     
    Millix likes this.
  15. Chrispins

    Chrispins

    Joined:
    Dec 9, 2014
    Posts:
    15
    I want to use a built-in character controller without the need for a rigidbody2D though. That way I can use Network Transform and Sync CharacterController to make smooth interpolation happen.
     
  16. Aggrojag

    Aggrojag

    Joined:
    Mar 24, 2014
    Posts:
    7
    There is no built in 2D CharacterController. To make one without using a rigidbody would be to use Translation or just directly changing the position of the transform. This is not advisable in comparison to using velocity. You would likely have collision issues unless handled properly.

    I haven't used the network components of Unity, but if some of the functions don't support 2D, then you will have to make your own, or purchase one off the asset store.

    Lastly, http://docs.unity3d.com/ScriptRefer...nsform.TransformSyncMode.SyncRigidbody2D.html I'm ignorant to the networking side of things, yet I have a feeling this may be what you're looking for.
     
  17. Mahmoud-AlSati

    Mahmoud-AlSati

    Joined:
    Aug 14, 2013
    Posts:
    4
    One of the options is to tween the position properly using the GoKit.
    Fast and efficient and low GC.
     
  18. LEGENDARY_L

    LEGENDARY_L

    Joined:
    Dec 31, 2015
    Posts:
    7
    Try this script

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Control : MonoBehaviour
    {
    private CharacterController controller;
    public float speed = 6.0f;
    public float jumpSpeed = 8.0F;
    private Vector3 moveDirection = Vector3.zero;
    public float gravity = 20.0f;

    void Awake()
    {
    controller = GetComponent<CharacterController>();
    }

    void Update()
    {
    if (controller.isGrounded)
    {
    moveDirection = transform.right * Input.GetAxis("Horizontal") * speed;
    if (Input.GetButton("Jump"))
    {
    moveDirection.y += jumpSpeed;
    }
    }
    controller.Move(moveDirection * Time.deltaTime);
    moveDirection.y -= gravity * Time.deltaTime;
    }
    }
     
  19. Millix

    Millix

    Joined:
    Jul 7, 2019
    Posts:
    1
    I know you posted this years ago, but I have been all over the web looking for a character controller that doesn't include a jump feature. Can you please give me some info on how you would go about making the character flip when pressing whatever button for the left key?
     
  20. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Use an IENumerator to count off 5 seconds and activate your "wait" animation.
     
  21. Wissard

    Wissard

    Joined:
    Aug 1, 2020
    Posts:
    3
    Hello,
    Can you tell me a script for 2D Character Controller with jump and tell me how to add 2D colliders to sprites, please.
     
  22. Rezrael

    Rezrael

    Joined:
    Nov 11, 2014
    Posts:
    7
  23. Jules6969

    Jules6969

    Joined:
    Mar 21, 2021
    Posts:
    1
    pls put jumping in the scripts
     
  24. itsazneefchowdhury

    itsazneefchowdhury

    Joined:
    Jan 14, 2021
    Posts:
    1
    Do you have the version with jump on it?
     
  25. darklift

    darklift

    Joined:
    Apr 11, 2016
    Posts:
    4
    Are there any disadvantages to using the 3D Character controller for 2D?
     
  26. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    What makes it 2D? The controller won't care about how you render stuff.

    Also, please create your own thread rather than hijack an existing one.

    Thanks.