so i have an issue with the enemy attack range being not set to an instance of an object
when i hover my mouse over the enemy it should be set as the opponent and the battle system should fire but for some reason the range of the enemy vs the range of the player is throwing it off and im not too sure why at this point
player script
using UnityEngine;
using System.Collections;
using System;
public class Player1 : Creature
{
public static Player1 player;
bool activeattack;
public static bool isAttacking;
public static bool playerattack;
public static Transform opponent;
// Use this for initialization
void Awake()
{
player = this;
isAttacking = false;
}
// Update is called once per frame
void Update()
{
playerisattacking();
attacktrue();
isAttacking = false;
}
protected override IEnumerator Autoaction()
{
{if (playerattack ==true)
if (opponent != null && Vector3.Distance(opponent.position, transform.position) < range)
{
playergo = false;
enemyattack = false;
Debug.Log("is attacking");
attackanimaton.SetBool("isAttacking", true);
attackanimaton.SetFloat("Attack", 9.0f);
opponent.GetComponent<Enemy>().GetHit(damage);
yield return new WaitForSecondsRealtime(idletime);
attackanimaton.SetFloat("Attack", 4.0f);
Debug.Log("player waiting");
yield return new WaitForSecondsRealtime(waittime);
enemyattack = true;
}
}
}
void playerisattacking()
{
if (playergo == true)
StartCoroutine(Autoaction());
}
void attacktrue()
{
if (Input.GetMouseButtonDown(1))
{
startattackrole = true;
}
}
}
Enemy script
using UnityEngine;
using System.Collections;
public class Enemy : Creature {
Player1 player;
NavMeshAgent navAgent;
bool activeattack;
public float chasingRange;
public static bool block;
public float chasingrange;
// Use this for initialization
void Awake () {
player = Player1.player;
navAgent = GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update () {
FollowPlayer();
enemyisattacking();
}
private void OnMouseOver()
{
Player1.opponent = transform;
}
void FollowPlayer()
{
if (IsInRange(chasingRange))
{
navAgent.SetDestination(player.transform.position);
}
else
{
navAgent.SetDestination(transform.position);
}
}
protected override IEnumerator Autoaction()
{
if (IsInRange(range))
{
if (enemyattack == true)
{
if (Vector3.Distance(player.transform.position, transform.position) < range)
{
enemygo = false;
Debug.Log("enemy is attacking");
Playerattack = false;
attackanimaton.SetBool("isAttacking", true);
attackanimaton.SetFloat("Attack", 9.0f);
player.GetComponent<Player1>().GetHit(damage);
yield return new WaitForSecondsRealtime(idletime);
attackanimaton.SetFloat("Attack", 4.0f);
Debug.Log("waiting");
yield return new WaitForSecondsRealtime(waittime);
Playerattack = true;
}
}
}
}
void unBlock()
{
block = false;
}
//Returns true if player is in range with the enemy in the given range
bool IsInRange(float range)
{
if (Vector3.Distance(player.transform.position, transform.position) < range)
{
return true;
}
else
{
return false;
}
}
void enemyisattacking()
{
if (enemygo == true)
StartCoroutine(Autoaction());
}
}
variables script
using UnityEngine;
using System.Collections;
public abstract class Creature : MonoBehaviour
{
public bool startattackrole;
public int d8;
public int attackstat;
public int waittime;
public int idletime;
public string name;
public int health;
public int damage;
public float range;
public bool enemyattack;
public bool Playerattack;
public int playerattackroleoutput;
public int enemyattackroleoutput;
public bool playergo;
public bool enemygo;
//1 attack per seond
public float attackSpeed = 2f;
public Animator attackanimaton;
public void GetHit(int playerDamage)
{
health = health - playerDamage;
}
protected abstract IEnumerator Autoaction();
void attackisgo()
{
if (playerattackroleoutput > enemyattackroleoutput)
{
playergo = true;
}
if (enemyattackroleoutput > playerattackroleoutput)
{
enemyattack = true;
}
}
void attackrolego()
{
if (startattackrole == true)
{
attackisgo();
}
}
// Use this for initialization
void Awake() {
attackanimaton = GetComponent<Animator>();
Playerattack = false;
enemyattack = false;
d8 = Random.Range(1, 8);
playergo = false;
enemygo = false;
startattackrole = false;
enemyattackroleoutput = d8 + attackstat;
playerattackroleoutput = d8 + attackstat;
}
// Update is called once per frame
void Update() {
attackrolego();
}
}