- Home /
My enemy keeps shooting at me until I get close
Help, I am trying to make a code, where my enemy looks around for me, and then when he spots me, starts shooting slowly, but what ends up happening is from the start my enemy spam shoots me until I get 1 meter or closer to him, and when i back up he spam shoots me again! any help?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Enemy : MonoBehaviour
{
public NavMeshAgent agent;
public Transform player;
public LayerMask whatIsGround, whatIsPlayer;
public GameObject enemybulletPrefab;
public float health;
//patrolling
public Vector3 walkPoint;
bool walkPointSet;
public float walkPointRange;
//attacking
public float timeBetweenAttacks;
bool alreadyAttacked;
//states
public float sightRange, attackRange;
public bool PlayerInSightRange, PlayerInAttackRange;
private void Awake()
{
player = GameObject.Find("Fps player").transform;
agent = GetComponent<NavMeshAgent>();
}
private void Patrolling()
{
if (!walkPointSet)
{
SearchWalkPoint();
}
if (walkPointSet)
{
agent.SetDestination(walkPoint);
}
Vector3 distanceToWalkPoint = transform.position - walkPoint;
//walkpoint reached
if (distanceToWalkPoint.magnitude < 1f)
{
walkPointSet = false;
}
}
private void SearchWalkPoint()
{
//calculate random point in range
float randomZ = Random.Range(-walkPointRange, walkPointRange);
float randomX = Random.Range(-walkPointRange, walkPointRange);
walkPoint = new Vector3(transform.position.x + randomX, transform.position.y, transform.position.z + randomZ);
if (Physics.Raycast(walkPoint, -transform.up, 2f, whatIsGround))
{
;
}
walkPointSet = true;
}
private void ChasePlayer()
{
agent.SetDestination(player.position);
}
private void Attackplayer()
{
//make sure enemy doesn't move
agent.SetDestination(transform.position);
transform.LookAt (player);
//shooting
GameObject bulletObject = Instantiate(enemybulletPrefab);
bulletObject.transform.position = transform.position + transform.forward;
bulletObject.transform.forward = transform.forward;
if (alreadyAttacked)
{
alreadyAttacked = true;
Invoke(nameof(ResetAttack), timeBetweenAttacks);
}
}
private void ResetAttack()
{
alreadyAttacked = false;
}
private void Update()
{
//checking if player is in sight or attack range
PlayerInSightRange = Physics.CheckSphere(transform.position, sightRange, whatIsPlayer);
PlayerInAttackRange = Physics.CheckSphere(transform.position, attackRange, whatIsPlayer);
if (!PlayerInAttackRange && !PlayerInAttackRange)
{
Patrolling();
}
if (!PlayerInAttackRange && !PlayerInAttackRange)
{
ChasePlayer();
}
if (!PlayerInAttackRange && !PlayerInAttackRange)
{
Attackplayer();
}
}
public void TakeDamage(int damage)
{ }
}
Well your code is a little messy. Probably humans can't read this code from your post.
$$anonymous$$ateusz has a point, you might struggle to get answers on here if your code is all on one line. There's a button on the question input box with two lines of 1s and 0s that lets you input your code.
If you can edit your question and paste your code in there you'll get more help :)
Answer by Casiell · Jan 22, 2021 at 01:20 PM
So I've allowed myself to reformat your code and it revealed tons of problems and weird things right of the bat.
In 62'nd line you had a ';' right after the if statement, so walkPointSet was always set to true in this method, no matter what
In Update each behavior is checking for !PlayerInAttackRange, so every behavior has the same condition which doesn't look correct. Also you are checking the same condition twice.
Your AttackPlayer method doesn't even check for that AlreadyAttacked variable. It just shoots. The check at the end should be reversed (!AlreadyAttacked) and few lines higher, just before bullet instantiation at least
Nitpicking, but don't use GameObject.Find method. One day you'll rename your object and will look for this bug for ages, it's really not that easy to find
Really, format your code. I'm not talking about formatting on this site (which you should also do) but about things like if statement and the method you call being in the same line. It's 1000x more readable if they are in separate lines. I also really recommend braces for single line if statements, it helps notice things like what happened in 62nd line.
Your answer
![](https://koobas.hobune.stream/wayback/20220613023744im_/https://answers.unity.com/themes/thub/images/avi.jpg)