Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Enemy attack forced?

Discussion in 'Scripting' started by Resilo, Dec 23, 2016.

  1. Resilo

    Resilo

    Joined:
    Dec 8, 2016
    Posts:
    139
    so heres my enemy script but something is causing enemyattack to = true as soon as the player steps in range but i have no such programing that i have put in place initaly enemyattack is suppose to be false and the only way to start the battle sequence should be on my player right click but for some reason every time i step in the specified range it starts.


    any clues as to why this is happening?


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Enemy : Creature {
    5.     Player1 player;
    6.     NavMeshAgent navAgent;
    7.     bool activeattack;
    8.     public float chasingRange;
    9.     public static bool block;
    10.     public float chasingrange;
    11.     public float fpstargetdistance;
    12.     public float enemylookdistance;
    13.     public float attackdistance;
    14.     public float enemymovementspeed;
    15.     public float damping;
    16.     public Transform fpstarget;
    17.     Rigidbody therigidbody;
    18.     Renderer myrender;
    19.     // Use this for initialization
    20.     void  targetingsystem()
    21.     {
    22.         fpstargetdistance = Vector3.Distance(fpstarget.position, transform.position);
    23.     }
    24.     void Awake () {
    25.         player = Player1.player;
    26.         navAgent = GetComponent<NavMeshAgent>();
    27.         myrender = GetComponent<Renderer>();
    28.         therigidbody = GetComponent <Rigidbody>();
    29.         enemyattack = false;
    30.     }
    31.    
    32.     // Update is called once per frame
    33.     void Update () {
    34.         targetingsystem();
    35.         enemyisattacking();
    36.     }
    37.  
    38.  
    39.     private void OnMouseOver()
    40.     {
    41.         Player1.opponent = transform;
    42.     }
    43.  
    44.  
    45.  
    46.     protected override IEnumerator Autoaction()
    47.  
    48.     {
    49.             if (enemyattack == true && (fpstargetdistance < range))
    50.             {
    51.                     enemygo = false;
    52.                     Debug.Log("enemy is attacking");
    53.                     Playerattack = false;
    54.                     attackanimaton.SetBool("isAttacking", true);
    55.                     attackanimaton.SetFloat("Attack", 9.0f);
    56.                     player.GetComponent<Player1>().GetHit(damage);
    57.                     yield return new WaitForSecondsRealtime(idletime);
    58.                     attackanimaton.SetFloat("Attack", 4.0f);
    59.                     Debug.Log("waiting");
    60.                     yield return new WaitForSecondsRealtime(waittime);
    61.                     Playerattack = true;
    62.  
    63.  
    64.  
    65.                 }
    66.  
    67.        
    68.     }
    69.  
    70.  
    71.  
    72.     void enemyisattacking()
    73.     {
    74.         if (enemygo == true)
    75.             Debug.Log("enemy is attcking first");
    76.             enemyattack = true;
    77.             StartCoroutine(Autoaction());
    78.     }
    79.  
    80. }
    81.  


    only other script that might be relevent

    Code (CSharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public abstract class Creature : MonoBehaviour
    7. {
    8.   public float maxhealth;
    9.   public bool startattackrole;
    10.   private int d8;
    11.   public int attackstat;
    12.   public int waittime;
    13.   public int idletime;
    14.   public string name;
    15.   public float health;
    16.   public int damage;
    17.   public float range;
    18.   public bool enemyattack;
    19.   public bool Playerattack;
    20.   public int playerattackroleoutput;
    21.   public int enemyattackroleoutput;
    22.   public bool playergo;
    23.   public bool enemygo;
    24.   //1 attack per seond
    25.   public float attackSpeed = 2f;
    26.   public Animator attackanimaton;
    27.  
    28.   public void GetHit(int playerDamage)
    29.   {
    30.   health = health - playerDamage;
    31.  
    32.   }
    33.  
    34.   protected abstract IEnumerator Autoaction();
    35.  
    36.  
    37.   void attackisgo()
    38.   {
    39.   if (playerattackroleoutput > enemyattackroleoutput)
    40.   {
    41.   playergo = true;
    42.   }
    43.  
    44.   if (enemyattackroleoutput > playerattackroleoutput)
    45.   {
    46.   enemyattack = true;
    47.   }
    48.   }
    49.  
    50.   public void attackrolego()
    51.   {
    52.   Debug.Log("Attackrolego has activated");
    53.   d8output();
    54.   enemyattackroleoutputgo();
    55.   d8output();
    56.   playerattackroleoutputgo();
    57.   attackisgo();
    58.  
    59.   }
    60.  
    61.   void enemyattackroleoutputgo()
    62.   {
    63.  
    64.   enemyattackroleoutput = d8 + attackstat;
    65.  
    66.   }
    67.  
    68.   void playerattackroleoutputgo()
    69.   {
    70.   playerattackroleoutput = d8 + attackstat;
    71.  
    72.   }
    73.   void d8output()
    74.   {
    75.   d8 = Random.Range(1, 8);
    76.   }
    77.  
    78.   // Use this for initialization
    79.   void Awake() {
    80.   attackanimaton = GetComponent<Animator>();
    81.   Playerattack = false;
    82.   enemyattack = false;
    83.   d8 = Random.Range(1, 8);
    84.   playergo = false;
    85.   enemygo = false;
    86.   startattackrole = false;
    87.    
    88.   }
    89.  
    90.    
    91.  
    92.   // Update is called once per frame
    93.   void Update() {
    94.  
    95.   }
    96. }
    97.  
     
  2. Defero

    Defero

    Joined:
    Jul 9, 2012
    Posts:
    200
    Didn't follow it through till the end, but maybe there are parenthesis missing in the "if" below?

    Code (CSharp):
    1. void enemyisattacking()
    2. {
    3.         if (enemygo == true)
    4.             Debug.Log("enemy is attcking first");
    5.         enemyattack = true;
    6.         StartCoroutine(Autoaction());
    7. }