Skip to main content
 using UnityEngine;
 using System.Collections;   

public class
  PlayerDirectionAndShooting : MonoBehaviour {
  {
     Rigidbody2D rbody;
     Animator anim;   
 
 // Use this for initialization
         void Start () 
     {
         rbody = GetComponent<Rigidbody2D> ();
         anim = GetComponent<Animator> ();
        }   

 // Update is called oncevoid perUpdate frame() 
        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);
         };
        }
     }
 using UnityEngine;
 using System.Collections;   

public class
  PlayerDirectionAndShooting : MonoBehaviour {
  
     Rigidbody2D rbody;
     Animator anim;   
 
 // Use this for initialization
         void Start () {
         rbody = GetComponent<Rigidbody2D> ();
         anim = GetComponent<Animator> ();
        }   

 // Update is called once per frame 
        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);
         };
        }
     }
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);
        }
    }
}
edited tags
Link
user1430
user1430

Need Help With My Script And Animator.I I am encountering a few bugs with it

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

when I unpress an arrow key,the the character always returns to his default direction and when I press space when no arrow key is pressed,the the right bow attack animation is played and also when i press the up arrow key and the space bar,the 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 iI can't seem to find anything wrong.the The animation's assigned are okay and the code is okay too. Don't know whatswhat's wrong. I have been stuck at this problem all day.

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

 using UnityEngine;
 using System.Collections;   

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

 // Use this for initialization
         void Start () {
         rbody = GetComponent<Rigidbody2D> ();
         anim = GetComponent<Animator> ();
        }   

 // Update is called once per frame 
        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);
         };
        }
     }

Need Help With My Script And Animator.I am encountering a few bugs with it

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.

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 whats wrong. I have been stuck at this problem all day.

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

 using UnityEngine;
 using System.Collections;   

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

 // Use this for initialization
         void Start () {
         rbody = GetComponent<Rigidbody2D> ();
         anim = GetComponent<Animator> ();
        }   

 // Update is called once per frame 
        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);
         };
        }
     }

Need Help With My Script And Animator. I am encountering a few bugs with it

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.

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.

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

 using UnityEngine;
 using System.Collections;   

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

 // Use this for initialization
         void Start () {
         rbody = GetComponent<Rigidbody2D> ();
         anim = GetComponent<Animator> ();
        }   

 // Update is called once per frame 
        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);
         };
        }
     }
fix code beautifully
Source Link
Loading
added 4 characters in body
Source Link
Loading
Source Link
Loading