Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unity 2D Character Controller for 4 direction movement

Discussion in '2D' started by cloe, Feb 15, 2015.

  1. cloe

    cloe

    Joined:
    Feb 15, 2015
    Posts:
    3
    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

    -------------------------------------------------------------------

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class playerController : MonoBehaviour
    5. {
    6.     public float speed;
    7.  
    8.     private Animator animator;
    9.    
    10.     // Use this for initialization
    11.     void Start()
    12.     {
    13.         animator = this.GetComponent<Animator>();
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.        
    20.         var vertical = Input.GetAxis("Vertical");
    21.         var horizontal = Input.GetAxis("Horizontal");
    22.        
    23.         if (horizontal > 0)
    24.         {
    25.             animator.SetInteger("Direction", 2);
    26.         }
    27.         else if (horizontal < 0)
    28.         {
    29.             animator.SetInteger("Direction", 0);
    30.         }
    31.         else if (vertical > 0)
    32.         {
    33.             animator.SetInteger("Direction", 1);
    34.         }
    35.         else if (vertical < 0)
    36.         {
    37.             animator.SetInteger("Direction", 3);
    38.         }
    39.  
    40.  
    41.     }  
    42. }
     
    beardevelopment likes this.
  2. Sprotte

    Sprotte

    Joined:
    Jun 14, 2013
    Posts:
    2
    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 :)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class playerController : MonoBehaviour
    4. {
    5.     public float speed;
    6.     private Animator animator;
    7.  
    8.     // Use this for initialization
    9.     void Start()
    10.     {
    11.         animator = this.GetComponent<Animator>();
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.        Vector2 moveDirection = Vector2.Zero;
    18.         var vertical = Input.GetAxis("Vertical");
    19.         var horizontal = Input.GetAxis("Horizontal");
    20.      
    21.         if (horizontal > 0)
    22.         {
    23.             moveDirection.x = 1;
    24.             animator.SetInteger("Direction", 2);
    25.         }
    26.         else if (horizontal < 0)
    27.         {
    28.             moveDirection.x = -1;
    29.             animator.SetInteger("Direction", 0);
    30.         }
    31.         else if (vertical > 0)
    32.         {
    33.             moveDirection.y = 1;
    34.             animator.SetInteger("Direction", 1);
    35.         }
    36.         else if (vertical < 0)
    37.         {
    38.             moveDirection.y = -1;
    39.             animator.SetInteger("Direction", 3);
    40.         }
    41.         transform.Translate(moveDirection * speed* Time.deltaTime, Space.World);
    42.     }
    43. }
     
  3. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    832
    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.

     
  4. cloe

    cloe

    Joined:
    Feb 15, 2015
    Posts:
    3
    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
     
  5. Matheus_Aguilera

    Matheus_Aguilera

    Joined:
    Mar 17, 2017
    Posts:
    1
    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.
     
  6. yanooshiiik

    yanooshiiik

    Joined:
    Oct 11, 2013
    Posts:
    1
    Anyone have idea how to do ONLY 4-way movement? No up-right, up-left etc?
     
    RNG-Development likes this.
  7. MegamaDev

    MegamaDev

    Joined:
    Jul 17, 2017
    Posts:
    77
    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.
     
  8. bobbymoynahan333

    bobbymoynahan333

    Joined:
    Feb 1, 2019
    Posts:
    1
    SAYS "vector 2 does not define ZERO"

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class playerController : MonoBehaviour
    4. {
    5.     public float speed;
    6.     private Animator animator;
    7.  
    8.     // Use this for initialization
    9.     void Start()
    10.     {
    11.         animator = this.GetComponent<Animator>();
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.        Vector2 moveDirection = Vector2.Zero;
    18.         var vertical = Input.GetAxis("Vertical");
    19.         var horizontal = Input.GetAxis("Horizontal");
    20.    
    21.         if (horizontal > 0)
    22.         {
    23.             moveDirection.x = 1;
    24.             animator.SetInteger("Direction", 2);
    25.         }
    26.         else if (horizontal < 0)
    27.         {
    28.             moveDirection.x = -1;
    29.             animator.SetInteger("Direction", 0);
    30.         }
    31.         else if (vertical > 0)
    32.         {
    33.             moveDirection.y = 1;
    34.             animator.SetInteger("Direction", 1);
    35.         }
    36.         else if (vertical < 0)
    37.         {
    38.             moveDirection.y = -1;
    39.             animator.SetInteger("Direction", 3);
    40.         }
    41.         transform.Translate(moveDirection * speed* Time.deltaTime, Space.World);
    42.     }
    43. }
    [/QU
     
  9. MegamaDev

    MegamaDev

    Joined:
    Jul 17, 2017
    Posts:
    77
    ...Are you trying to say that you're getting that error message?
     
    Last edited: Feb 12, 2019
  10. Gileno

    Gileno

    Joined:
    Nov 23, 2018
    Posts:
    29
    I get that error too: "vector 2 does not contain a definition to ZERO"
     
  11. Gileno

    Gileno

    Joined:
    Nov 23, 2018
    Posts:
    29
    U have to use Vector2 moveDirection = Vector2.zero;
    And not Vextor.Zero
     
  12. AchordionVLY

    AchordionVLY

    Joined:
    Oct 20, 2019
    Posts:
    2
    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.
     
    MegamaDev likes this.
  13. ollie_floren

    ollie_floren

    Joined:
    Mar 28, 2020
    Posts:
    2
    im having an issue where my sprite wont go from idle to animation when i move the character...
     
  14. MegamaDev

    MegamaDev

    Joined:
    Jul 17, 2017
    Posts:
    77
    That's really not the same as this thread's question. Try asking in a new thread.
     
  15. 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.
     
  16. Valjuin

    Valjuin

    Joined:
    May 22, 2019
    Posts:
    481
  17. Wissard

    Wissard

    Joined:
    Aug 1, 2020
    Posts:
    3