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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Need help replicating the Zelda Gameboy games sword swinging

Discussion in '2D' started by NoahMewes, Apr 6, 2019.

Thread Status:
Not open for further replies.
  1. NoahMewes

    NoahMewes

    Joined:
    May 9, 2017
    Posts:
    6
    Hello,

    As said in the title I currently am trying to replicate the way that the Zelda gameboy games handle sword swinging. The thing I am specifically struggling with is that you are able to spam the attack button to swing rapidly. This is shown in the beginning of this video:



    This is currently what my PlayerController currently looks like:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public enum PlayerState {
    6.     walk,
    7.     attack,
    8.     interact
    9. }
    10.  
    11. public class PlayerMovement : MonoBehaviour
    12. {
    13.     public PlayerState currentState;
    14.     public float speed;
    15.     private Rigidbody2D playerRigidbody;
    16.     private Vector3 change;
    17.     private Animator anim;
    18.  
    19.     // Start is called before the first frame update
    20.     void Start()
    21.     {
    22.         anim = GetComponent<Animator>();
    23.         playerRigidbody = GetComponent<Rigidbody2D>();
    24.         currentState = PlayerState.walk;
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.         change = Vector3.zero;
    31.         change.x = Input.GetAxisRaw("Horizontal");
    32.         change.y = Input.GetAxisRaw("Vertical");
    33.      
    34.  
    35.         if (Input.GetButtonDown("attack") && currentState != PlayerState.attack)
    36.         {
    37.             StartCoroutine(AttackCo());
    38.         }
    39.         else if (currentState == PlayerState.walk)
    40.         {
    41.             UpdateAnimationsAndMove();
    42.         }
    43.        
    44.     }
    45.     private IEnumerator AttackCo()
    46.     {
    47.         anim.SetBool("Attacking", true);
    48.         currentState = PlayerState.attack;
    49.         yield return null;
    50.         anim.SetBool("Attacking", false);
    51.         yield return new WaitForSeconds(0.25f);
    52.         currentState = PlayerState.walk;
    53.     }
    54.     void UpdateAnimationsAndMove() {
    55.         if (change != Vector3.zero)
    56.         {
    57.             MovePlayer();
    58.             anim.SetFloat("movex", change.x);
    59.             anim.SetFloat("movey", change.y);
    60.             anim.SetBool("moving", true);
    61.         }
    62.         else
    63.         {
    64.             anim.SetBool("moving", false);
    65.         }
    66.  
    67.     }
    68.     void MovePlayer() {
    69.         playerRigidbody.MovePosition(transform.position + change * speed * Time.deltaTime);
    70.     }
    71. }
    72.  
    This code recreates the base movement and sword swinging pretty closely, however you can't attack quickly like shown in the video. Any suggestions on how I can implement this?

    Thanks!
     
  2. Gilbasaurus

    Gilbasaurus

    Joined:
    Oct 26, 2017
    Posts:
    1
    Sounds like it's waiting for your animation to finish completely before playing it again? Does the animation take longer than 0.25s to play?
     
  3. NoahMewes

    NoahMewes

    Joined:
    May 9, 2017
    Posts:
    6
    Yeah it does wait for it to finish completely. The animation is exactly 0.25s.
     
  4. AZRetro

    AZRetro

    Joined:
    Aug 29, 2017
    Posts:
    3
    Just reset the animation from the start each time the user presses the swing button.

    Also keep in mind the game you are referencing has a 2 - 3 frame swing animation. I'm guessing yours is a lot more frames.
     
Thread Status:
Not open for further replies.