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?
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.
Thanks! However, this example is still using rigidbodies and colliders instead of a special CharacterController component.
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.
@gruddlebug Is this Character Controller code more similar to Mario Platform style or something else?
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.
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.
@Recon03 Can you please upload some of you charactrerController to github i will be happy to fork and use it
Hi guys .How could i change the script so when i have a long press it will jump higher and vice versa
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
This has nothing to do with CharacterController2D. CharacterController2D Moves and makes collisions only.
No Jump 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; } }
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.
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.
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; } }
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?
Hello, Can you tell me a script for 2D Character Controller with jump and tell me how to add 2D colliders to sprites, please.
This is a decent one you can learn from. https://github.com/cjddmut/Unity-2D-Platformer-Controller I would gut it and use the unity character controller. Here is a simple understanding of why in the beginning of this tutorial. https://www.roystan.net/articles/character-controller-2d.html
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.