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

Question Action game 2D shooting projectiles

Discussion in '2D' started by VERDAMMT21, Sep 2, 2020.

  1. VERDAMMT21

    VERDAMMT21

    Joined:
    May 17, 2020
    Posts:
    11
    Hi All,
    I have a problem with Action game 2D. The character in action game 2d has ability to shoot projectiles which is an arrow. But after the character shoot the arrow, the character move slightly to the side. And also when the character shoot, the arrow is not instantiate on the middle of the character body, rather on the side(left) of the character body. And one more thing, my character cannot shoot arrow four direction depends on character idle facing, Vector 2 temp is not working, when the character shoot while idle, the character only Instantiate the arrow but not moving it. If the character shoot while walking, the character instantiate also shoot the arrow depends on the direction the character is moving. All i want is the character shoot arrow while idling, but not walking, and also shoot four direction depends on the idle facing direction



    Code (CSharp):
    1. public enum PlayerState {
    2.    idle,
    3.    walk,
    4.    attack
    5.  
    6. }
    7. public class Movement : MonoBehaviour
    8. {
    9.  
    10.     //Components
    11.     public Rigidbody2D rigidbody;
    12.     public Animator animator;
    13.     Vector2 movement;
    14.     public GameObject projectile;
    15.     public PlayerState currentPlayerState;
    16.     //Variables
    17.     [Range(0f, 50f)]
    18.     public float Speed = 0f;
    19.  
    20.  
    21.     // Start is called before the first frame update
    22.     void Start()
    23.     {
    24.         currentPlayerState = PlayerState.idle;
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.         movement = Vector2.zero;
    31.  
    32.  
    33.         movement.x = Input.GetAxisRaw("Horizontal");
    34.         movement.y = Input.GetAxisRaw("Vertical");
    35.         currentPlayerState = PlayerState.walk;
    36.  
    37.         if (currentPlayerState == PlayerState.idle) {
    38.             Speed = 0;
    39.    
    40.    
    41.    
    42.    
    43.         }
    44.         if (currentPlayerState == PlayerState.walk)
    45.         {
    46.  
    47.             if (movement != Vector2.zero)
    48.             {
    49.                 FixedUpdate();
    50.  
    51.  
    52.             }
    53.             animator.SetFloat("Horizontal", movement.x);
    54.             animator.SetFloat("Vertical", movement.y);
    55.  
    56.  
    57.  
    58.             if (Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1 || Input.GetAxisRaw("Vertical") == 1 || Input.GetAxisRaw("Vertical") == -1)
    59.             {
    60.                 animator.SetFloat("Idle Horizontal", Input.GetAxisRaw("Horizontal"));
    61.                 animator.SetFloat("Idle Vertical", Input.GetAxisRaw("Vertical"));
    62.                 currentPlayerState = PlayerState.idle;
    63.             }
    64.  
    65.  
    66.         }
    67.         if (Input.GetButtonDown("Fire1") &&  currentPlayerState != PlayerState.attack){
    68.        
    69.             StartCoroutine(StartAttackArrow());
    70.  
    71.         } else if (Input.GetButtonUp("Fire1"))
    72.         {
    73.        
    74.                 StartCoroutine(EndAttackArrow());
    75.            
    76.          
    77.        
    78.         }
    79.    
    80.     }
    81.     private IEnumerator StartAttackArrow() {
    82.         animator.SetBool("isAttacking", true);
    83.         currentPlayerState = PlayerState.attack;
    84.         Speed = 0;
    85.         yield return null;
    86.     }
    87.     private IEnumerator EndAttackArrow()
    88.     {
    89.         animator.SetBool("isAttacking", false);
    90.         currentPlayerState = PlayerState.idle;
    91.         Speed = 3;
    92.         yield return null;
    93.         MakeArrow();
    94.     }
    95.  
    96.  
    97.     void FixedUpdate()
    98.     {
    99.  
    100.         rigidbody.MovePosition(rigidbody.position + movement * Speed * Time.fixedDeltaTime);
    101.  
    102.     }
    103.     void MakeArrow()
    104.     {
    105.        Vector 2 temp = new Vector 2(animator.GetFloat("Horizontal"), animator.GetFloat("Vertical"));
    106.         Arrow arrow = Instantiate(projectile, transform.position, Quaternion.identity).GetComponent<Arrow>();
    107.         arrow.Setup(temp, Vector3.zero);
    108.     }
    109.  
    THANK YOU>>>>
     
    Last edited: Sep 2, 2020
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @denzelkwok

    Your code tags are not written correctly.
     
  3. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @denzelkwok

    "the character only Instantiate the arrow but now moving it.
    *now = not"


    You do realize you can edit your post using that "edit" button below your post?
     
  4. VERDAMMT21

    VERDAMMT21

    Joined:
    May 17, 2020
    Posts:
    11
    "Your code tags are not written correctly."

    Then please give me a correct code. THANKS>>
     
  5. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @denzelkwok

    I mean - your code formatting is not done properly like it should be; as you can see, your code block doesn't look like in the other posts... No one is going to read your badly formatted code, so better fix it! Luckily there is a tutorial how to do this:

    https://forum.unity.com/threads/using-code-tags-properly.143875/

    P.S. You already had the other code tag there, but for some reason, you edited it away, instead of fixing your code block.