In my game, I have a player and an enemy. the player can walk, jump and slash a sword. The enemy walks towards the player. if the enemy gets hit by the sword, it blinks red and gets knocked back. The problem is, sometimes when I hit the enemy it damages the enemy, waits a second, then plays the animation. This doesn’t happen when I’m not hitting the enemy.
I have exit time checked off. the slash animation is composed of 3 frames (pretty simple). The slash waits .25 seconds before registering the attack, but triggers the anim beforehand. I have 2.2gig processor and 8 gigs ram. Could this just be my computer lagging?
here’s the code:
The player attack script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Attack : MonoBehaviour {
Animator anim;
public float slashDistance;
public float slashWait;
float startSlashWait;
public bool slashing;
public int damage;
public LayerMask hitAble;
// Use this for initialization
void Start() {
anim = GetComponentInParent<Animator>();
startSlashWait = slashWait;
}
// Update is called once per frame
void Update() {
if (Input.GetMouseButtonDown(0) && !slashing)
{
slashing = true;
anim.SetTrigger("Slash");
}
if (slashing)
{
slashWait -= Time.deltaTime;
RaycastHit2D hit = Physics2D.Raycast(this.transform.position, transform.TransformDirection(Vector2.left), slashDistance, hitAble);
if (slashWait <= 0 )
{
slashing = false;
slashWait = startSlashWait;
if(hit.transform.tag == ("Enemy"))
{
print(hit.transform.gameObject);
if (hit.transform.gameObject.GetComponent<Econtrol>() != null)
{
hit.transform.gameObject.GetComponent<Econtrol>().TakeDamage(damage);
}
}
}
}
}
}
The enemy react script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Econtrol : MonoBehaviour {
public bool damagedWait;
public float DamagedEffectTime;
float StartDamagedEffectTime;
int damageTaken;
public float health;
Rigidbody2D RB;
public int Knockback;
public GameObject prefab;
SpriteRenderer render;
Movement playerMovement;
// Use this for initialization
void Start () {
RB = GetComponent<Rigidbody2D>();
playerMovement = GameObject.FindGameObjectWithTag("player").GetComponent<Movement>();
render = gameObject.GetComponent<SpriteRenderer>();
StartDamagedEffectTime = DamagedEffectTime;
}
// Update is called once per frame
void Update () {
if (damagedWait)
{
DamagedEffectTime -= Time.deltaTime;
}
if(DamagedEffectTime <= 0)
{
FinishTakeDamage();
DamagedEffectTime = StartDamagedEffectTime;
}
}
public void TakeDamage(int damage)
{
DamagedEffectTime = StartDamagedEffectTime;
if (playerMovement.turnedRight)
{
RB.velocity = new Vector2(RB.velocity.x - Knockback, 0);
} else
{
RB.velocity = new Vector2(RB.velocity.x + Knockback, 0);
}
RB.velocity *= -1;
damageTaken = damage;
render.material.color = Color.red;
damagedWait = true;
}
void FinishTakeDamage()
{
print("effect finished");
health -= damageTaken;
render.material.color = Color.white;
damageTaken = 0;
if (health <= 0)
{
Instantiate(prefab, new Vector2(15, 0), Quaternion.identity);
Destroy(this.gameObject);
}
}
}
If you need any more info tell me. Thanks!