Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 nikhil_mane · Dec 11, 2017 at 02:28 AM · fpsaiartificial intelligencezombies

Friendly AI that follows player and shoot zombies.

Sir/Madam I want such type of friendly AI script which would follow the player and shoot zombies just like left 4 dead 2. I am not that great in scripting (just started) so could you help me please.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by shadowpuppet · Dec 11, 2017 at 05:49 AM

I made the very script for a robot helper I have. I basically just took the enemy attack script (what the enemy uses to attack me) and put it on my robot changing the target to gameObjectsWithTag == enemy. I put an an adaptation of the attack script on the robot ,as well, where I am the target - only his "attack" is just an idle animation which he does when he is within 2 units of me. and when I move...he "chases" me just as in the attack scripts. So I have an Attack script and an Ally script, and I just toggle between them depending on the conditions . In the attack script, if no enemies are within the maxDist range ( say 20 units) then I disable the Attack script and enable the Ally script so he will come to me and follow me. In the Ally script I do the same in reverse, if, while he is being my pal and following me around, an enemy comes within his maxDist range, I toggle off the Ally script and enable the Attack script. I tried putting them both in one script but his attack script got a bit hairy when I coded in seeking out the closet enemy to attack. So if you even have an attack script to start with then just modify them. Below is an old more raw version of the basic attack script I started with before making all the modifications( found it on line years ago) and it still has commented out explanations

 using UnityEngine;
 using System.Collections;
 
 public class enemyAttack : MonoBehaviour {
 
     public enum EnemyState{IDLE = 0, WALK = 1, RUN = 2, ATTACK = 3, KILL = 4};
     public AnimationClip enemyIdle;//setting animations
     public AnimationClip enemyWalk;//walking
     public AnimationClip enemyRun;//running/chasing
     public AnimationClip attack;//attacking player
     public AnimationClip enemyKill;//killing player
     public AnimationClip enemyDie;//killing player
     public enemyHealth enemyHealth;
     public Transform Player;//adds the player to the script
     MonoBehaviour shootbullet;
     private int Stopped = 0;
     public int WalkingSpeed = 1;//sets the normal walking speed
     public int RunningSpeed = 5;//sets the running (chase player) speed
     public int DefaultRunningSpeed = 5;
     public int MaxDist= 8;//maximum distance the player can be before enemy stops chasing the player
     public float MinDist= 1;//how close the player can be to enemy for enemy to start chasing player
     public EnemyState currentState;
     Animation anim;
     private bool PlayerDetected;
     public PlayerHealth playerHealth;
 
     void Start()
     {
         shootbullet = GetComponent<shootBullet>();
         playerHealth = GameObject.FindWithTag("Player").GetComponent <PlayerHealth> ();
     }
     void Update()
     {
         if(playerHealth.currentHealth <= 0)
             GetComponent<Animation>().CrossFade (enemyIdle.name);
         
         if(Vector3.Distance(transform.position,Player.position) <= MaxDist)//if that object has the tag "Player"
         {
             PlayerDetected = true;
             if (PlayerDetected)
             {
                 
                 transform.LookAt(Player);//Looks at the player
                 currentState = EnemyState.RUN;
                 transform.position += transform.forward*RunningSpeed*Time.deltaTime;//follows the player
             }
         }
         else if(Vector3.Distance(transform.position,Player.position) > MaxDist)
         {
             RoamAround ();
         }
         if (Vector3.Distance (transform.position,Player.position) < MinDist)
         {
             currentState = EnemyState.ATTACK;
             Attack ();
             
         }
         
         if (currentState == EnemyState.IDLE)
         {
             
             GetComponent<Animation>().CrossFade (enemyIdle.name);
         
 
 
         }
         else if (currentState == EnemyState.WALK)
         {
             
             GetComponent<Animation>().CrossFade (enemyWalk.name);
         }
         else if (currentState == EnemyState.RUN)
         {
             
             
             GetComponent<Animation>().CrossFade (enemyRun.name);
             RunningSpeed = DefaultRunningSpeed;
         }
         else if (currentState == EnemyState.ATTACK)
         {
             
             GetComponent<Animation>().CrossFade (attack.name);
             RunningSpeed = Stopped;
             if(playerHealth.currentHealth <= 0)
                 GetComponent<Animation>().CrossFade (enemyIdle.name);
             
         }
         else if (currentState == EnemyState.KILL)
         {
             anim.Stop ("machinegunblast");
             GetComponent<Animation>().CrossFade (enemyKill.name);
         }
         
     }
     void RoamAround()
     {
         currentState = EnemyState.IDLE;//Add other RoamAround stuff here
     }
     public void Attack()
     {
         GetComponent<Animation>().CrossFade (enemyIdle.name);    //shootbullet.enabled = true;//Add attacking stuff here
     
         if(playerHealth.currentHealth <= 0)
             GetComponent<Animation>().CrossFade (enemyIdle.name);
         if(playerHealth.currentHealth <= 0)
         currentState = EnemyState.ATTACK;
     }
 }
 
Comment
Add comment · Show 4 · 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
avatar image nikhil_mane · Dec 11, 2017 at 10:18 AM 0
Share

@shadowpuppet I got this error: alt text

capture.png (5.0 kB)
avatar image shadowpuppet nikhil_mane · Dec 11, 2017 at 03:46 PM 0
Share

well, yeah, this script has references to other scripts that you don't have or need .It's template. I added Playerhealth scripts so the enemy would stop attacking when I was dead and the enemy health script is referencing the enemy that has this attack script. delete anything to do with playerhealth, enemyhealth or whatever else gives you an error

avatar image nikhil_mane shadowpuppet · Dec 11, 2017 at 06:17 PM 0
Share

Thanks It Worked :)

Show more comments

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.

Follow this Question

Answers Answers and Comments

141 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 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

I am making an fps and I want to make allies... Help me 1 Answer

fps tutorial problem 2 Answers

Hurting enemy when attacking status is equal to true and player is colliding with enemy object. 1 Answer

Set target for Enemy to follow that is not in the scene yet 0 Answers

Conflicting animations (attack and idle) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges