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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

2D Characters [Super beginner, have only done 3 tutorials]

Discussion in '2D' started by Zippownz, Sep 28, 2018.

  1. Zippownz

    Zippownz

    Joined:
    Sep 5, 2017
    Posts:
    6
    Hello, im just starting with Unity and C#, have done some tutorials (Survival shooter, Roll-a-ball, and Endless runner on youtube) and now i wanted to see what i could do with the 2D options. I've downloaded some assets, but the character is broken down into pieces eg. head, hand, left leg etc. How can i "assemble" my character and have it be 1 body?

    Sorry if its a super easy question but on the 3D tutorials you either had the character already made, or used something simple like a sphere or a ball.

    Also, if someone would be so kind on explaining how to code the movement? I can do Input.GetKey("d"); or something like that but on the Survival shooter tutorial we used [..]("horizontal"); and the teacher said its better for having alternative keys etc.
     
  2. Zippownz

    Zippownz

    Joined:
    Sep 5, 2017
    Posts:
    6
    Ok so what I did was, create an empty object and add a sprite for every body part, is this the right way of doing it or not? I added a rigidbody component and it stays glued together so i think it works
     

    Attached Files:

  3. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    Moving without physic. Here the script will check the value from the Input. It will be between -1...0... +1 depended on the pressed keys. You can look at the keys in the Edit-Project Settings-Input (and expand "Horizontal"). And replace the object every frame depended on input value.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour {
    6.  
    7.     public float speed;
    8.  
    9.     void Update ()
    10.     {
    11.         float dirX = Input.GetAxis("Horizontal");
    12.         Vector2 movingVector = new Vector2(dirX, 0);
    13.         transform.Translate(movingVector * speed * Time.deltaTime);
    14.     }
    15. }
    16.  
    Moving with physic (rigidbody2D + Collider2D). Check the input and push the object with force.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour {
    6.  
    7.     public float speed;
    8.     Rigidbody2D rb;
    9.  
    10.     void Start ()
    11.     {
    12.         rb = GetComponent<Rigidbody2D>();
    13.     }
    14.  
    15.     void FixedUpdate ()
    16.     {
    17.         float dirX = Input.GetAxis("Horizontal");
    18.         Vector2 movingVector = new Vector2(dirX, 0);
    19.         rb.AddForce(movingVector);
    20.     }
    21. }
    Change the objects speed depended on input (rigidbody2D + Collider2D).

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour {
    6.  
    7.     public float speed;
    8.     Rigidbody2D rb;
    9.  
    10.     void Start ()
    11.     {
    12.         rb = GetComponent<Rigidbody2D>();
    13.     }
    14.  
    15.     void FixedUpdate ()
    16.     {
    17.         float dirX = Input.GetAxis("Horizontal");
    18.         Vector2 movingVector = new Vector2(dirX, 0);
    19.         rb.velocity = movingVector;
    20.     }
    21. }
     
    Last edited: Sep 28, 2018
    Zippownz likes this.
  4. RichardKain

    RichardKain

    Joined:
    Oct 1, 2012
    Posts:
    1,261
    It is a right-enough approach. The important thing is that it works for your purposes. I have used an empty object as a basis for a more complex object built out of sub-objects before. It is a valid way of solving the problem, and works within Unity's general structure.

    There is no one way to handle these things, there is only strengths and weaknesses to whatever approach you go with. If you changed up your character to only use a single sprite, instead of being made up of multiple parts, there would be strengths and weaknesses to that. The big weakness of that approach is that it severely limits your recycling, and further complicates your animations. (you would have to re-draw every frame of your character, no matter how small a change you were hoping to make) The big strength of that approach is that you would have unlimited freedom for drawing your character any way you wanted to. Your animation frames could be as intricate or unique as you wanted them to be.

    The approach you are describing limits the 2d illustration possibilities somewhat. (that's its weakness) But it is WAY better for recycling, and creating smoother in-engine motion animations. It all depends on the effect you want to achieve.
     
    Zippownz likes this.
  5. Zippownz

    Zippownz

    Joined:
    Sep 5, 2017
    Posts:
    6
    Well first of all thank you for the code, i wouldnt have been able to do it by myself :D and i can understand some parts but not all yet. At first it was giving me an error but it was because i wrote "playerMovement" and in the script the "P" was capitalized, but i figured it out, so no errors, still, my character wont move, tried 100 on speed aswell as 4e10 (tons of numbers) but it wont work. Can you maybe figure it out from the picture?
     

    Attached Files:

  6. Zippownz

    Zippownz

    Joined:
    Sep 5, 2017
    Posts:
    6
    Also a big thanks to you! I for some reason didnt see it the way you described my approach, i was trying to use a prefab animation when i could have done it myself. Without you telling me what you told me I would still be trying to figure out how to use the prefab animation on the whole body instead of only a body part. (Picture a character but the head is another character doing an Idle animation)
     
  7. Zippownz

    Zippownz

    Joined:
    Sep 5, 2017
    Posts:
    6
    Ok so i've been reading and looking for tutorials online and i found this code that works for moving but its giving me some errors for flipping the character:

    upload_2018-9-29_9-0-15.png

    now for some reason it wont let me write "||" for or and at the part for "facingRight" it says "; expected" if i add "&&" before that, that error goes away but the parenthesis after that gives one where it says "; expected" im confused...
    also in line 41 it says " Invalid expression term '=' " how does that work?

    its 9am now so maybe i dont see it cause im tired, but if someone figures it out please let me know and if you can explain it as if you were explaining it to a kid :D
     
  8. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    35 missed logical "and"
    41 delete space between * and =


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class playerMovement : MonoBehaviour {
    6.  
    7.     Rigidbody2D rb;
    8.     bool facingRight;
    9.     [SerializeField]
    10.     float speed;
    11.  
    12.     void Start ()
    13.     {
    14.         facingRight = true;
    15.         rb = GetComponent<Rigidbody2D>();
    16.     }
    17.  
    18.     void FixedUpdate ()
    19.     {
    20.         float h = Input.GetAxis("Horizontal");
    21.         HandleMovement(h);
    22.         Flip(h);
    23.     }
    24.  
    25.     void HandleMovement (float h)
    26.     {
    27.         rb.velocity = new Vector2(h * speed, rb.velocity.y);
    28.     }
    29.  
    30.     void Flip (float h)
    31.     {
    32.         if (h > 0 && !facingRight || h < 0 && facingRight)
    33.         {
    34.             facingRight = !facingRight;
    35.             Vector3 theScale = transform.localScale;
    36.             //theScale.x = theScale.x * -1;
    37.             theScale.x *= -1;
    38.             transform.localScale = theScale;
    39.         }
    40.     }
    41. }
    42.