Bool want return false

Hi again guys, so right now im having a little issue, i hv to scripts the BaseCharacter one and the MeleeEnemy that inherites from Enemy that inherits from BaseCharacter, in the BaseCharacter i define a bool called isMoving and in the MeleeEnemy Start function i set it to false, when enemy start moving it turn true (i can check in inspector) and if i go out of range it goes false, but when is in attack range i define it to go false and it dont returns to false, so this way enemy is attacking me but always move arround player instead of be stopeed (and in enemy attack method i call isMoving = false). Can anyone help me plz?

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public abstract class BaseCharacter : MonoBehaviour {

    public string characterName;

    public float attackRange;

    public float attackSpeed;

    public float maxHealth;

    public float curHealth;

    public int damage;

    public bool isMoving;

    // Use this for initialization
    void Start ()
    {

    }
   
    // Update is called once per frame
    void Update ()
    {
   
    }

    public void GetDamage(int attackDamage)
    {
        curHealth = curHealth - attackDamage;

        if (curHealth <= 0) {
            curHealth = 0;

            Destroy(gameObject, 0.5f);
        }
    }

    protected abstract void Attack();
}
using UnityEngine;
using System.Collections;

public class MeleeEnemy : Enemy
{
    Player player;

    bool blockEnemy;

       
    // Use this for initialization
    void Start ()
    {
        curHealth = maxHealth;

        player = Player.player;

        blockEnemy = false;

        isMoving = false;
    }
   
    // Update is called once per frame
    void Update ()
    {
        Attack();
       
        FollowPlayer();
    }
    void FollowPlayer()
    {
        if(IsInRange(chasingRange))
        {
            isMoving = true;

            navAgent.SetDestination(player.transform.position);
           
            navAgent.Resume();
        }
        else if (!IsInRange(chasingRange))
        {
            isMoving = false;

            navAgent.SetDestination(transform.position);
        }
    }
   
    void OnMouseOver()
    {
        Player.opponent = transform;
    }
   
    protected override void Attack()
    {
        if (IsInRange (attackRange))
        {
            if(!blockEnemy)
            {
                if(player.curHealth > 0)
                {
                    isMoving = false;

                    transform.LookAt(opponent);
                                       
                    player.GetDamage(damage);
                   
                    blockEnemy = true;
                   
                    Debug.Log("Player Damaged!");
                   
                    Invoke("UnBlock", attackSpeed);
                }
            }
        }
    }
   
    void UnBlock()
    {
        blockEnemy = false;
    }
   
    bool IsInRange(float range)
    {
        if (player != null)
        {
            if (Vector3.Distance (player.transform.position, transform.position) < range)
            {           
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
}

Starting at line 86 in MeleeEnemy, you wrote:

if (Vector3.Distance (player.transform.position, transform.position) < range)
{         
    return true;
}

so you’re telling it to return true if you are closer than the range. If you want it to return false when the enemy is in-range, change ‘<’ to ‘>’

if i do that the game go all strange and the melleenemy attack me from far… The IsInRange bool is diferente from isMivong bool, and the problem is in isMoving bool… Or im sleeping already or maybe i dont explaini myself very well… :slight_smile:

i figure it out ty m8… i need another else if in my follow player :slight_smile: ty again

My mistake… so starting on line 32, you wrote:

if(IsInRange(chasingRange))
{
    isMoving = true;
    navAgent.SetDestination(player.transform.position);
          
    navAgent.Resume();
}
else if (!IsInRange(chasingRange))
{
    isMoving = false;
    navAgent.SetDestination(transform.position);
}

looks like you’re saying “if the enemy is in range, isMoving is true and move towards the player. If the enemy isn’t in range, isMoving is false and don’t move”

Try using the ! operator on the first if instead of the “else if”

cool, good work ^^

i get it to work this way:

but now i hv another problem im using navmesh when when i attack the enemy with mouse button it push the enemy back lol… bouth have priority 50 at navmesh options :stuck_out_tongue:

if(IsInRange(chasingRange))
        {
            if (!IsInRange (attackRange))
            {
                transform.LookAt(opponent);
               
                isMoving = true;
               
                navAgent.SetDestination (player.transform.position);
               
                navAgent.Resume ();
            }
            else
            {
                transform.LookAt (opponent);
               
                isMoving = false;
               
                navAgent.Stop();
            }
        }

I’m not very experienced with unity nav meshes, but I know you have an agent capsule radius&height or something along those lines. I’m not sure how to make agents not push each other, but I’m sure you can do it somehow.

If your agents have rigidbodies attached, you should also check your physics settings and disable the collision between the player and enemy, then make an extra collider on the enemy that is a trigger. Triggers can’t push things, so you can allow your trigger to collide with the player and have the appropriate scripted reaction. (To summarize, the enemy has 2 colliders: one for physics and one for non-physics collision reactions)

1 Like