I’ve been making an aRPG game, and just added in SpecialAttacks. All was working well, but now I can’t seem to attack the enemy. The animation resets for some reason, and I need some help because I can’t understand why
I’ll attach the scripts for reference:
My Player Combat Script:
using UnityEngine;
using System.Collections;
public class combat : MonoBehaviour
{
public GameObject opponent;
public AnimationClip attack;
public AnimationClip dieClip;
public float range;
public int damage;
public int health;
public int maxHealth;
private double impactLength;
public double impactTime;
public bool impacted;
public bool inAction;
bool started;
bool ended;
public float combatEscapeTime;
public float countDown;
public bool specialAttack;
// Use this for initialization
void Start ()
{
impactLength = (GetComponent<Animation>()[attack.name].length*impactTime);
}
// Update is called once per frame
void Update ()
{
if(Input.GetKeyDown(KeyCode.Space) && !specialAttack)
{
inAction = true;
}
if(inAction)
{
if(attackFunction (0, 1, KeyCode.Space))
{
}
else
{
inAction = false;
}
}
die ();
}
public bool attackFunction(int stunSeconds, double scaledDamage, KeyCode key)
{
if(Input.GetKey(key)&&inRange ())
{
GetComponent<Animation>().Play(attack.name);
ClickToMove.attack = true;
if(opponent!=null)
{
transform.LookAt(opponent.transform.position);
}
}
if(GetComponent<Animation>()[attack.name].time<0.9*GetComponent<Animation>()[attack.name].length)
{
ClickToMove.attack = false;
impacted = false;
if(specialAttack)
{
specialAttack = false;
}
return false;
}
impact (stunSeconds, scaledDamage);
return true;
}
public void resetAttackFunction()
{
ClickToMove.attack = false;
impacted = false;
GetComponent<Animation>().Stop(attack.name);
}
void impact(int stunSeconds, double scaledDamage)
{
if(opponent!=null&&GetComponent<Animation>().IsPlaying (attack.name)&&!impacted)
{
if((GetComponent<Animation>()[attack.name].time)>impactLength&&(GetComponent<Animation>()[attack.name].time<0.9*GetComponent<Animation>()[attack.name].length))
{
countDown = combatEscapeTime + 2;
CancelInvoke ();
InvokeRepeating ("combatEscapeCountDown", 0, 1);
opponent.GetComponent<mob>().GetHit(damage*scaledDamage);
opponent.GetComponent<mob>().getStun(stunSeconds);
impacted = true;
}
}
}
void combatEscapeCountDown()
{
countDown = countDown - 1;
if(countDown == 0)
{
CancelInvoke("combatEscapeCountDown");
}
}
public void GetHit(int damage)
{
health = health - damage;
if (health<0)
{
health = 0;
}
}
bool inRange()
{
if(Vector3.Distance (opponent.transform.position, transform.position) <= range)
{
return true;
}
else
{
return false;
}
}
//if dead returns true or alive turns false
public bool isDead()
{
if(health == 0)
{
return true;
}
else
{
return false;
}
}
void die()
{
if(isDead ()&&!ended)
{
if(!started)
{
ClickToMove.die = true;
GetComponent<Animation>().Play (dieClip.name);
started = true;
}
if(started&&!GetComponent<Animation>().IsPlaying(dieClip.name))
{
//what ever you want
maxHealth = 1000;
ended = true;
started = false;
ClickToMove.die = false;
}
}
}
}
And my Special Attacks script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpecialAttack : MonoBehaviour
{
public combat player;
public KeyCode key;
public double damagePercentage;
public int stunTime;
public bool inAction;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if(Input.GetKeyDown(key) && !player.specialAttack)
{
player.resetAttackFunction();
player.specialAttack = true;
inAction = true;
}
if(inAction)
{
if(player.attackFunction(stunTime, damagePercentage, key))
{
}
else
{
inAction = false;
}
}
}
}
The enemy can still attack and kill me though… Is anyone able to help me try and fix this? It was all working well until I messed around with the special attack script, now I can’t hit the enemy. I can give more information as well, thanks