The condition regarding the "Enemy Attacking does not work and i am very confused why.
Attack is a Trigger Parameter and yet it wont work when being called
Enemy CODE
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Enemy : character
{
Animator animator;
public float collisionOffset = 0.05f;
[SerializeField]
private float moveSpeed = 5f;
SpriteRenderer spriteRenderer;
private Vector3 startingPosition;
LevelSystem levelSystem = new LevelSystem();
Rigidbody2D rb;
private Transform target;
private IState currentState;
public float MyAttackRange { get; set; }
public float MyAttackTime { get; set; }
public Transform Target
{
get
{
return target;
}
set
{
target = value;
}
}
Vector2 moveDirection;
protected void Awake()
{
MyAttackRange = 1;
ChangeState(new IdleState());
rb = GetComponent<Rigidbody2D>();
spriteRenderer = GetComponent<SpriteRenderer>();
}
public void EnemyCollisions()
{
}
public float Health
{
set
{
// prints the amount of health set to the enemy
print(value);
health = value;
Debug.Log("This is the health of the enemy" + health); // this tells unity to paste the fact that this code is being called
if (health <= 0)
{
Defeated();
}
}
get
{
return health;
}
}
public float MoveSpeed { get => moveSpeed; set => moveSpeed = value; }
// allows the health to be changed in unity
public float health = 1;
private void Start()
{
startingPosition = transform.position;
// grabs the animator from the scene
animator = GetComponent<Animator>();
// no need for this due to the fact that we have a new follow target script :)
// target = GameObject.Find("Player").transform;
rb = GetComponent<Rigidbody2D>();
}
//private Vector3 GetRoamingPosition()
//{
//}
protected override void Update()
{
if (!IsAttacking)
{
MyAttackTime += Time.deltaTime;
}
currentState.Update();
}
public void Defeated()
{
// calls the animation that is set as an event in unity
ScoreManager.instance.AddPoint();
MMScoreManager.instance.AddPoint();
Debug.Log("Enemy : Defeated");
animator.SetTrigger("Defeated");
Destroy(gameObject);
Debug.Log(levelSystem);
levelSystem.AddExperience();
// add experience points on death
}
public void RemoveEnemy()
{
// d e a t h
Defeated();
// self explnanatory, destroys the game object this code is linked to, specifically the enemy
Destroy(gameObject);
Debug.Log("Yes this works : " + gameObject); // makes sure that this code is being called.
}
private IEnumerator Attack()
{
MyAnimator.SetBool("attack", true);
IsAttacking = true;
yield return new WaitForSeconds(3);
}
public void ChangeState(IState newState)
{
if (currentState != null)
{
currentState.Exit();
}
currentState = newState;
currentState.Enter(this);
}
}
AND ATTACK STATE CODE
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AttackState : IState
{
// reference to states parent
private Enemy parent;
private float attackCooldown = 3;
private float extraRange = .1f;
public void Enter(Enemy parent)
{
this.parent = parent;
}
public void Exit()
{
}
public void Update()
{
Debug.Log("The Enemy is attacking you...");
if (parent.MyAttackTime >= attackCooldown && !parent.IsAttacking)
{
parent.MyAttackTime = 0;
parent.StartCoroutine(Attack());
}
if(parent.Target != null)
{
float distance = Vector2.Distance(parent.Target.position, parent.transform.position);
if(distance >= parent.MyAttackRange+extraRange && !parent.IsAttacking)
{
parent.ChangeState(new FollowState());
}
//check range and then attack
}
else
{
parent.ChangeState(new IdleState());
}
}
public IEnumerator Attack()
{
parent.IsAttacking = true;
parent.MyAnimator.SetTrigger("attack");
yield return new WaitForSeconds(parent.MyAnimator.GetCurrentAnimatorStateInfo(1).length);
parent.IsAttacking = false;
}
}
AND THE CHARACTER INHERITANCE THEY ARE BOTH INHERITING FROM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class character : MonoBehaviour
{
protected float speed;
private Rigidbody2D myRigidbody;
public Animator MyAnimator { get; set; }
protected Vector2 direction;
private void Start()
{
MyAnimator = GetComponent<Animator>();
}
public bool IsAttacking { get; set; }
public bool isMoving
{
get
{
return direction.x != 0 || direction.y != 0;
}
}
protected virtual void Update()
{
HandleLayers();
}
public void Move()
{
myRigidbody.velocity = direction.normalized * speed;
}
public void HandleLayers()
{
if (isMoving)
{
}
else if (IsAttacking)
{
ActivateLayer("AttackLayer");
}
else
{
ActivateLayer("IdleLayer");
}
}
public void ActivateLayer(string layerName)
{
for (int i = 0; i < MyAnimator.layerCount; i++)
{
MyAnimator.SetLayerWeight(i, 0);
}
MyAnimator.SetLayerWeight(MyAnimator.GetLayerIndex(layerName), 1);
}
}
please help me fix this…
please and thank yous