• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by malcah08 · Jan 21 at 09:44 PM · enemynavmeshagentenemy ai

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)
     { }
 }

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mateuszwallace · Jan 22 at 06:26 AM 0
Share

Well your code is a little messy. Probably humans can't read this code from your post.

avatar image jackmw94 · Jan 22 at 10:19 AM 0
Share

$$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 :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Casiell · Jan 22 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.

  1. 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

  2. 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.

  3. 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

  4. 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

  5. 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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

122 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Nav Mesh is invalid for some reason 0 Answers

NavMeshAgent does not follow player 0 Answers

Navmesh Agent - stop after player out of range 1 Answer

Horde of NavMeshAgents - stops to recalculate path. 4 Answers

Enemies do damage even after dying 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges