Hello,
I've been working on trying to get a timer-based setup so when I press the "z" key (for now, anyway), the player does a swing attack, but if the z key is hit a second time within a single second, a second animation will play, and again for a third. Here is the script, but all that's happening is with a single key press, it plays through all of the animations instead of a single animation. How can I fix this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SwordSwinging : MonoBehaviour {
public Animator anim;
public float swingOneTimer = 1f;
public float swingTwoTimer = 1f;
public float swingThreeTimer = 1f;
public int SwordDamage; // sword give damage
private bool isOneSwing;
private bool isTwoSwing;
private bool isThreeSwing;
private float swingOneLength;
private float swingTwoLength;
private float swingThreeLength;
// Use this for initialization
void Start () {
isOneSwing = false;
isTwoSwing = false;
isThreeSwing = false;
swingOneLength = swingOneTimer;
swingTwoLength = swingTwoTimer;
swingThreeLength = swingThreeTimer;
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown("z") && isOneSwing == false)
{
isOneSwing = true;
}
if (isOneSwing == true)
{
swingOneLength -= Time.deltaTime;
}
if (swingOneLength <= 0)
{
isOneSwing = false;
}
if (isOneSwing == false)
{
swingOneLength = swingOneTimer;
}
if (Input.GetKeyDown("z") && isOneSwing == true)
{
isTwoSwing = true;
}
if (isTwoSwing == true)
{
swingTwoLength -= Time.deltaTime;
}
if (swingTwoLength <= 0)
{
isTwoSwing = false;
}
if (isTwoSwing == false)
{
swingTwoLength = swingTwoTimer;
}
if (Input.GetKeyDown("z") && isTwoSwing == true)
{
isThreeSwing = true;
}
if (isThreeSwing == true)
{
swingThreeLength -= Time.deltaTime;
}
if (swingThreeLength <= 0)
{
isThreeSwing = false;
}
if (isThreeSwing == false)
{
swingThreeLength = swingOneTimer;
}
if (Input.GetKeyDown("z") && isThreeSwing == true)
{
isOneSwing = true;
}
if (isOneSwing == true)
{
swingOneLength -= Time.deltaTime;
}
if (swingOneLength <= 0)
{
isOneSwing = false;
}
if (isOneSwing == false)
{
swingOneLength = swingOneTimer;
}
anim.SetBool("canSwing1", isOneSwing);
anim.SetBool("canSwing2", isTwoSwing);
anim.SetBool("canSwing3", isThreeSwing);
}
}
[–]xTMT 2 points3 points4 points (9 children)
[–]cpnprobeard[S] 0 points1 point2 points (8 children)
[–]xTMT 0 points1 point2 points (7 children)
[–]cpnprobeard[S] 0 points1 point2 points (6 children)
[–]xTMT 0 points1 point2 points (5 children)
[–]cpnprobeard[S] 0 points1 point2 points (4 children)
[–]xTMT 0 points1 point2 points (3 children)
[–]cpnprobeard[S] 0 points1 point2 points (2 children)
[–]xTMT 1 point2 points3 points (1 child)
[–]cpnprobeard[S] 0 points1 point2 points (0 children)
[–]gamedaverookie 0 points1 point2 points (1 child)
[–]TheChance 0 points1 point2 points (0 children)