0
\$\begingroup\$

Okay so I have created a player archer character which when he using the arrow keys, faces those directions and upon hitting space when in that direction, the bow attack animation is played.

The code works fine but there are some nerve-wrecking problems:

when I unpress an arrow key, the character always returns to his default direction and when I press space when no arrow key is pressed, the right bow attack animation is played and also when i press the up arrow key and the space bar, the up bow attack animation does not play and instead again,the right bow attack animation is played.

I have looked at everything again and again but I can't seem to find anything wrong. The animation's assigned are okay and the code is okay too. Don't know what's wrong. I have been stuck at this problem all day.

I have attached some screenshots so you can see how I have achieved this:

my animator here

my blend tree in the idle state is here and so far, this is my script:

public class PlayerDirectionAndShooting : MonoBehaviour 
{
     Rigidbody2D rbody;
     Animator anim;   

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

     void Update () 
     {
         Vector2 direction_vector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
         anim.SetFloat("input_x", direction_vector.x);
         anim.SetFloat("input_y", direction_vector.y);

         if (Input.GetKeyDown("space"))
         {
            if (anim.GetFloat("input_x") <= -1)
            {
                anim.SetBool("is_shooting_left", true);
            }
         }
        else
        {
            anim.SetBool("is_shooting_left", false);
        }

        if (Input.GetKeyDown("space"))
        {
            if (anim.GetFloat("input_y") <= -1)
            {
                anim.SetBool("is_shooting_down", true);
            }
        }
        else
        {
          anim.SetBool("is_shooting_down", false);
        }


        if (Input.GetKeyDown("space"))
        {
            if (anim.GetFloat("input_y") <= 1)
            {
               anim.SetBool("is_shooting_up", true);
            }
        }
        else
        {
            anim.SetBool("is_shooting_up", false);
        }

        if (Input.GetKeyDown("space"))
        { 
            if (anim.GetFloat("input_x") <= 1)
            {
                anim.SetBool("is_shooting_right", true);
            }
        }
        else
        {
            anim.SetBool("is_shooting_right",false);
        }
    }
}
\$\endgroup\$
4
  • \$\begingroup\$ Can you please format your code? It's very hard to read it \$\endgroup\$
    – Bálint
    Commented Jul 19, 2016 at 5:35
  • \$\begingroup\$ Okay,sorry!!!!Let me fix it. \$\endgroup\$ Commented Jul 19, 2016 at 5:45
  • \$\begingroup\$ Is it okay now?(Thanks for the edit smkplus!) \$\endgroup\$ Commented Jul 19, 2016 at 5:53
  • \$\begingroup\$ "Here is my code, fix the bugs" type of questions are not good. Try to ask "how to do", not "what did I do wrong". Even If you ask a bug solving question, try to shrink your question into readable and easily understandable boundaries. \$\endgroup\$ Commented Jul 27, 2016 at 16:59

1 Answer 1

0
\$\begingroup\$

I rearrange code here(for the jump and attack part):

         if (Input.GetKeyDown("space"))
         {
            if (anim.GetFloat("input_x") < 0)
             {
                 anim.SetBool("is_shooting_left", true);
                  anim.SetBool("is_shooting_right", false);
             }
              if (anim.getFloat("input_x") > 0)
             {
                  anim.SetBool("is_shooting_left", false);
                  anim.SetBool("is_shooting_right", true);
             }
             if (anim.GetFloat("input_y") < 0)
             {
                  anim.SetBool("is_shooting_down", true);
                  anim.SetBool("is_shooting_up", false);
             }
              if (anim.GetFloat("input_y") > 0)
              {
                  anim.SetBool("is_shooting_down", false);
                  anim.SetBool("is_shooting_up", true);
             }
         }

it should work.

  • having multiple if (GetKeyDown("space")) is wrong.
  • it's better to compare anim.GetFloat(input_x"or y") with 0. not 1 and -1 ( i don't know why you did that)
\$\endgroup\$
7
  • \$\begingroup\$ But now the shooting animation keeps looping. \$\endgroup\$ Commented Jul 19, 2016 at 18:28
  • \$\begingroup\$ Oh nevermind.I fixed that with this: else { anim.SetBool("is_shooting_down", false); anim.SetBool("is_shooting_left", false); anim.SetBool("is_shooting_right", false); anim.SetBool("is_shooting_up", false); } \$\endgroup\$ Commented Jul 19, 2016 at 18:32
  • \$\begingroup\$ @MuhammadMahad if you found the answer correct, please check it as correct answer. \$\endgroup\$
    – klaymen
    Commented Jul 19, 2016 at 18:51
  • \$\begingroup\$ I am still having a problem.I want my player to permanently face a direction when i press an arrow key and remain there until i press any other arrow key.It just keeps returning to its default position before initialization right now. \$\endgroup\$ Commented Jul 19, 2016 at 19:03
  • \$\begingroup\$ Actually you have asked multiple questions. Better separate and clarify them. For that problem you can have above conditions without space part, Or in the else block that you added yourself. \$\endgroup\$
    – klaymen
    Commented Jul 19, 2016 at 19:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .