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

Problems with Diagonal animation en game 2d top down shooter style

Discussion in 'Scripting' started by cyberlarry, Jun 6, 2014.

  1. cyberlarry

    cyberlarry

    Joined:
    May 14, 2014
    Posts:
    12
    Hi i am new in the forum, i am developing a top down shooter 2d game. I have the next problem, i have gotten move the player(include diagonal) and i have gotten the animation in 4 classic directions, but now i dont get the diagonal animation. This is the code in csharp to get the animation in the 4 directions, but i dont know how can to get the 4 diagonal positions in the code. Thank you very much and sorry for my english.

    using UnityEngine;
    using System.Collections;


    public class PlayerController : MonoBehaviour

    {
    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 (vertical > 0)
    {
    animator.SetInteger("run", 2);
    }
    else if (vertical <0)
    {
    animator.SetInteger("run", 0);
    }
    else if (horizontal > 0)
    {
    animator.SetInteger("run",1);
    }
    else if (horizontal < 0)
    {
    animator.SetInteger("run", 3);
    }
    }
    }
     
  2. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157
    I'm new to C# but IMO you'll have to tell it to run in both the horizontal and vertical at the same time at the same rate for each possible diagonal direction.

    I would simply name them NW,NE,SW,SE and the other directions N,S,E,W but that's just me. Again, newbie as well, just some thoughts for you to bounce off of.
     
  3. cyberlarry

    cyberlarry

    Joined:
    May 14, 2014
    Posts:
    12
    Thanks troas for the answer but it doesnt work because i need give 1 number to the nw, ne, sw,se, to make the transition.

    :(
     
  4. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157
    Well you can affect 2 variables at the same time with one function right? Try that.

    Basically make a function that tells the character to use the North and West command at the same time if both buttons are pressed. So an if statement should work, or something like that.
     
  5. Ereous

    Ereous

    Joined:
    Aug 29, 2012
    Posts:
    163
    Troas is correct. You need to take out the else if logic and only use if logic. Otherwise you can only update the one direction every frame.
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  7. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157
    An easier solution (as I spent some time figuring this out) is you should setup your own custom inputs. This way you can set a button for positive and negative direction as well as alternative input buttons for both and make it under one button.

    Ex: You want a button that tells the character to go right/left that takes inputs of D for right and A for left, as well as right arrow for right and left arrow for left

    Solution:

    1) Go to Edit -> Project Settings -> Input
    2) Create a new input button called Horizontal or whatever suites you
    3) Fill in the pos/neg button inputs as well as the alt. pos/neg inputs (pos in this case should be to the right)
    4) No script should be needed for the classic for directions
    5) Now test if pressing up and down at once updates in both directions in 1 frame if not go to step 6
    6) Create a script (as discussed before) that uses if functions to update in 2 directions simultaneously in one frame


    The advantage to this method is a lot of optimization. You wont have all of those if statements just to have 4 directions.
     
  8. cyberlarry

    cyberlarry

    Joined:
    May 14, 2014
    Posts:
    12
    Thanks everybody,this is the solution: Is a mix of code and blend tree. LEARN Blend tree if you want that your player move in 8 directions.


    This for the code:


    using UnityEngine;
    using System.Collections;

    public class PlayerScript : MonoBehaviour {

    private Animator anim;
    public Vector2 speed = new Vector2(4, 4);

    private Vector2 movement;

    void Start ()
    {
    anim = GetComponent<Animator>();
    }

    void FixedUpdate()
    {
    {
    rigidbody2D.velocity = movement;
    }

    float lastInputX = Input.GetAxis ("Horizontal");
    float lastInputY = Input.GetAxis ("Vertical");

    if (lastInputX != 0 || lastInputY != 0) {
    anim.SetBool ("walking", true);


    if (lastInputX > 0) {
    anim.SetFloat ("LastMoveX", 1f);
    } else if (lastInputX < 0) {
    anim.SetFloat ("LastMoveX", -1f);
    } else {
    anim.SetFloat ("LastMoveX", 0f);
    }

    if (lastInputY > 0) {
    anim.SetFloat ("LastMoveY", 1f);
    } else if (lastInputY < 0) {
    anim.SetFloat ("LastMoveY", -1f);
    } else {
    anim.SetFloat ("LastMoveY", 0f);
    }


    } else {
    anim.SetBool ("walking", false);
    }
    }

    void Update ()

    {

    float inputX = Input.GetAxis("Horizontal");
    float inputY = Input.GetAxis("Vertical");


    movement = new Vector2(
    speed.x * inputX,
    speed.y * inputY);


    anim.SetFloat ("SpeedX", inputX);
    anim.SetFloat ("SpeedY", inputY);

    }
    }

    This for the blend tree



    ;)
     
    Troas likes this.
  9. cyberlarry

    cyberlarry

    Joined:
    May 14, 2014
    Posts:
    12

    Yes of course, for 4 directions, there isnt problem. Is very easy. For 8 directions is more complicated. Thank you for the answer. I have posted the solution, i hope that it help too much people :D
     
  10. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157

    Nice, I like how you went through the solution with a video.

    Though I still think there may be a more optimal way to do it this seems to work just fine.
     
  11. cyberlarry

    cyberlarry

    Joined:
    May 14, 2014
    Posts:
    12
    I still rather to read, but there are only 1 year of unity 2d information.