Unity 2D Character Controller for 4 direction movement

Dear You-who-read-this-message,

Hello and thank you for taking the time to read through this post.
I am all new to Unity and to the world of game-making in general, but anyway I’ve wanted to make a simple 2D top-down style of game to add for my application to University (I want to study Game Arts, as I love creating arts and playing games), but as I am a total newbie, I am really struggling with the coding.

As mentioned above, I really want to keep things as simple as possible: all I want to make is a simple level with my character walking through the scene by the control of the arrow keys, and I have placed some objects like tables and chairs in the scene, so of course I also want him to stop at this objects (and not just run right through them). I believe I can achieve this effect by the implement of RigidBody and Collision Boxes, but I am still at the beginning of creating my scene, and it is the Character Controller I face the biggest troubles recently.

I have succeeded in controlling my character as so far that he will face the direction (North,East,South or West) when I press the equal direction on the keyboard (controlling him with the arrow keys), but he is only facing that direction, he is not really moving, and that is what I want to achieve; I want him to walk in the direction I press on the keyboard!
I have searched for appliable scripts and tutorials on the internet, but didn’t have much success so far; most times I would find scripts/tutorials for 2D platformers or for games that are controlled with the mouse, so if there is anybody out there who knows about a script I might use for my Character Controller or has heared about any good tutorial on 2D top down games controlled with the arrow keys, I would more than appreciate your help.

Please also find my CharacterController Script at the end of this message.
Waiting eagerly for your responses and thank you very much already in advance for your support,
sincerely yours, CLOE


using UnityEngine;
using System.Collections;

public class playerController : MonoBehaviour
{
    public float speed;

    private Animator animator;
   
    // Use this for initialization
    void Start()
    {
        animator = this.GetComponent<Animator>();
    }
   
    // Update is called once per frame
    void Update()
    {
       
        var vertical = Input.GetAxis("Vertical");
        var horizontal = Input.GetAxis("Horizontal");
       
        if (horizontal > 0)
        {
            animator.SetInteger("Direction", 2);
        }
        else if (horizontal < 0)
        {
            animator.SetInteger("Direction", 0);
        }
        else if (vertical > 0)
        {
            animator.SetInteger("Direction", 1);
        }
        else if (vertical < 0)
        {
            animator.SetInteger("Direction", 3);
        }


    }  
}
1 Like

He Cloe at the moment you only playing Animations you have to choose: you can translate the gameObject to move it or you can move the gameObject by applying forces. For better control feeling go for translations :
Something like that… hope this helps :slight_smile:

using UnityEngine;
using System.Collections;
public class playerController : MonoBehaviour
{
    public float speed;
    private Animator animator;
 
    // Use this for initialization
    void Start()
    {
        animator = this.GetComponent<Animator>();
    }
 
    // Update is called once per frame
    void Update()
    {
       Vector2 moveDirection = Vector2.Zero;
        var vertical = Input.GetAxis("Vertical");
        var horizontal = Input.GetAxis("Horizontal");
     
        if (horizontal > 0)
        {
            moveDirection.x = 1;
            animator.SetInteger("Direction", 2);
        }
        else if (horizontal < 0)
        {
            moveDirection.x = -1;
            animator.SetInteger("Direction", 0);
        }
        else if (vertical > 0)
        {
            moveDirection.y = 1;
            animator.SetInteger("Direction", 1);
        }
        else if (vertical < 0)
        {
            moveDirection.y = -1;
            animator.SetInteger("Direction", 3);
        }
        transform.Translate(moveDirection * speed* Time.deltaTime, Space.World);
    }
}

you might want to look into using a blend tree for this, then just feed in the x & y values to control the animation frames.

Dear Sprotte, dear hawken,

Thank you so much for your support!
The script Sprotte provided to me already contained all the information I needed to control the character movements.
This is really a nice community, you helped me so much!

Thank you once more for the fast responses,
cloe

Well, this post certainly helped a lot, especially with the SetInteger part.
But I am having an issue: my character moves before the animation starts.
Is it because there’s a problem with the animation or is it a programming issue?
And thanks again for the solution.

Anyone have idea how to do ONLY 4-way movement? No up-right, up-left etc?

1 Like

The above code looks like it ought to handle that–it only runs the vertical checks if the horizontal checks fail, so that should convert any up-right- and up-left-type input into plain right and left.

SAYS “vector 2 does not define ZERO”

using UnityEngine;
using System.Collections;
public class playerController : MonoBehaviour
{
    public float speed;
    private Animator animator;
 
    // Use this for initialization
    void Start()
    {
        animator = this.GetComponent<Animator>();
    }
 
    // Update is called once per frame
    void Update()
    {
       Vector2 moveDirection = Vector2.Zero;
        var vertical = Input.GetAxis("Vertical");
        var horizontal = Input.GetAxis("Horizontal");
    
        if (horizontal > 0)
        {
            moveDirection.x = 1;
            animator.SetInteger("Direction", 2);
        }
        else if (horizontal < 0)
        {
            moveDirection.x = -1;
            animator.SetInteger("Direction", 0);
        }
        else if (vertical > 0)
        {
            moveDirection.y = 1;
            animator.SetInteger("Direction", 1);
        }
        else if (vertical < 0)
        {
            moveDirection.y = -1;
            animator.SetInteger("Direction", 3);
        }
        transform.Translate(moveDirection * speed* Time.deltaTime, Space.World);
    }
}

[/QU

…Are you trying to say that you’re getting that error message?

I get that error too: “vector 2 does not contain a definition to ZERO”

U have to use Vector2 moveDirection = Vector2.zero;
And not Vextor.Zero

Well as I see, it should be Vector2.zero instead of Vector2.Zero

A single wrong capital letter may lead your project into some errors.

1 Like

im having an issue where my sprite wont go from idle to animation when i move the character…

That’s really not the same as this thread’s question. Try asking in a new thread.

Hello,
Can you tell me a script for 2D Character Controller with jump and tell me how to add 2D colliders to sprites, please.

Lesson 3.1 - Jump ForceTutorial (part of Create with Code - Unity Learn course)

It isnt up-to-date