The enemy keeps chasing, even when not in range

I have a problem with the enemy path finding script I tried it myself then I fixed any issue I had by watching ben . But the enemy is still chasing me with out getting close to it I set both character and enemy atleast 100 meters apart and the enemy is still coming to me with out me moving this is what I have .

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.ThirdPerson;

public class Enemy : MonoBehaviour {

    [SerializeField] float maxHealthPoints = 100f;
	[SerializeField] float attackRadius = 4f;

    float currentHealthpoint = 100f;
	AICharacterControl aiCharacterControl = null;
	GameObject player = null;

    public float healthAsPercentage
    {
        get
        {
            return currentHealthpoint / (float) maxHealthPoints;
        }
    }
	void start()
	{
		player = GameObject.FindGameObjectWithTag ("Player");
		aiCharacterControl = GetComponent<AICharacterControl> ();
     }
	void update ()
	{
		float distanceToPlayer = Vector3.Distance (player.transform.position, transform.position);
		if (distanceToPlayer <= attackRadius) {
			aiCharacterControl.SetTarget (player.transform);
		} else {
			aiCharacterControl.SetTarget (transform);
		}
	}
}

Hi,

I was going to ask you to check what values you had set in the Inspector, but then I spotted both your Start and Update methods are all lower-case, as such Unity will not be calling these, C# is case-sensitive.

Correct the names and then let me know if you are still experiencing the same problems. :slight_smile:

[SOlVED]sorry it took me so long to check it out . but I don’t know which words your refereeing to.
when I saved the file it showed no errors it worked but the enemy just wasn’t acting the way its supposed to .Now I have an error about the Camera Raycaster when I opened the file and added the new raycaster I have a naming error

Assets/Scripts/CursorAffordance.cs(17,5): error CS0246: The type or namespace name `CameraRaycaster’ could not be found. Are you missing an assembly reference?

Assets/Scripts/PlayerMovement.cs(14,5): error CS0246: The type or namespace name `CameraRaycaster’ could not be found. Are you missing an assembly reference?

1 Like

Hi,

These words, from your code;

void start() and void update()

Unity will completely ignore those because they are not Unity methods… In order for these to work they would need to be in the correct case, e.g; void Start() and void Update().

C# is case-sensitive, so you have to be more careful with naming, and ideally, you would be consistent also, so when you create your own methods, format them the same way, e.g. ChasePlayer() rather than chasePlayer() or chaseplayer etc.

Regarding the CameraRaycaster error, the error on it’s own isn’t very useful, but if you post the code also we may be able to solve it. :slight_smile:

I lost the original Camera Raycaster file I have to redo that part again

That explains your error then.

You had a class named CameraRaycaster, you have other code that is trying to understand what a CameraRaycaster is, but it’s definition - your class - is missing.

Once replaced/re-written, that error should disappear.

exacly how do I fix that issue . it appeared only when the raycast changed on lecture 44 in section 2

I have a huge problem with this enemy scripted on chasing me … I have the prefab in place now on stop game mode but it switched to enemy in play mode automaticly here is a screen shot

using UnityEngine;
using UnityStandardAssets.Characters.ThirdPerson;

public class Enemy : MonoBehaviour {

[SerializeField] float maxHealthPoints = 100f;
[SerializeField] float attackRadius = 4f;

float currentHealthpoint = 100f;
AICharacterControl aiCharacterControl = null;
GameObject player = null;

public float healthAsPercentage
{
    get
    {
        return currentHealthpoint / (float) maxHealthPoints;
    }
}
void Start()
{
	player = GameObject.FindGameObjectWithTag ("Player");
	aiCharacterControl = GetComponent<AICharacterControl> ();
 }
void Update ()
{
	float distanceToPlayer = Vector3.Distance (player.transform.position, transform.position);
	if (distanceToPlayer <= attackRadius) 
	{
		aiCharacterControl.SetTarget(player.transform);
	}
	else
	{
		aiCharacterControl.SetTarget (transform);
	}
}

}

Privacy & Terms