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 Stactic_Games · May 10, 2020 at 09:26 PM · aienemyfollowenemy aifollow player

How do I make a enemy follow me

I am making a fps shooter and I need my enemy to follow me when I am with in a certain range of the enemy this is what I have so far.` using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class MovetowrdsPlayer : MonoBehaviour
 {
     public GameObject Player;
     public bool Speed;
     public void Update()
     {
         rigidbody.velocity = transform.forward * Speed;
     }
 }
 

is this really the way

Comment
Add comment · Show 1
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 PlusEqual · May 11, 2020 at 12:33 AM 0
Share

You can do it, try to do step-by-step:

  • Find the distance between the enemy and the player position.

  • Try to rotate the enemy to face the player, it's a nice detail.

  • Don't forget to assign a Rigidbody to the enemy in order to move it

See if you can work it out!

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by UnityToMakeMoney · May 11, 2020 at 12:02 AM

Here is a script that might help

 //set the values in the inspector
 public Transform target; //drag and stop player object in the inspector
 public float within_range;
 public float speed;
 
 public void Update(){
      //get the distance between the player and enemy (this object)
     float dist = Vector3.Distance(target.position, transform.position);
     //check if it is within the range you set
     if(dist <= within_range){
       //move to target(player) 
       transform.position = Vector3.MoveTowards(transform.position, target.transform.position, speed);      
     }
     //else, if it is not in rage, it will not follow player
 }

The way you were trying to accomplish wouldn't work, but I see what you were going for.

Comment
Add comment · Show 7 · 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 Stactic_Games · May 11, 2020 at 06:18 PM 0
Share

The only problem is that all the enemy's clump together into one big enemy any advice

avatar image boss2070301 Stactic_Games · May 11, 2020 at 06:23 PM 0
Share

@Stactic_Games You can make less enemies which will $$anonymous$$imize clumping.

avatar image UnityToMakeMoney Stactic_Games · May 11, 2020 at 06:49 PM 0
Share

as someone else suggested, you can make fewer enemies, but alternatively, you can use this exact same script, but now it would be for enemies so that they aren't together as much. You can also set a limit of enemies spawned at a time by counting how many have been spawned and destroyed.

avatar image Stactic_Games · May 11, 2020 at 09:24 PM 0
Share

I have 5 enemies? is there any colder I can add

avatar image UnityToMakeMoney Stactic_Games · May 12, 2020 at 01:21 AM 0
Share

what do you mean? Are they not prefabs? Can you please elaborate?

avatar image chrisf112233 Stactic_Games · May 12, 2020 at 01:32 AM 0
Share

You can add rigidbody and colliders to prevent the enemies from stacking on top of eachother

avatar image faraza158 · May 20, 2020 at 07:22 PM 0
Share

Or maybe you could try A* path finding algorithm for this.

check it out : youtube link

avatar image
0

Answer by unity_414apache · May 11, 2020 at 08:24 AM

using UnityEngine; using System.Collections;

//[RequireComponent(typeof(CharacterController))]

public class Chaser : MonoBehaviour {

 public float speed = 20.0f;
 public float minDist = 1f;
 public Transform target;

 // Use this for initialization
 void Start () 
 {
     // if no target specified, assume the player
     if (target == null) {

         if (GameObject.FindWithTag ("Player")!=null)
         {
             target = GameObject.FindWithTag ("Player").GetComponent<Transform>();
         }
     }
 }
 
 // Update is called once per frame
 void Update () 
 {
     if (target == null)
         return;

     // face the target
     transform.LookAt(target);

     //get the distance between the chaser and the target
     float distance = Vector3.Distance(transform.position,target.position);

     //so long as the chaser is farther away than the minimum distance, move towards it at rate speed.
     if(distance > minDist)    
         transform.position += transform.forward * speed * Time.deltaTime;    
 }

 // Set the target of the chaser
 public void SetTarget(Transform newTarget)
 {
     target = newTarget;
 }

} @ Static_Games , this is the basic chaser script. Hope this helps !

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
avatar image
0

Answer by lucasdvazquez7 · May 11, 2020 at 04:14 AM

You can do this in a variety of ways.

One method I recently used was adding a Sphere Collider to the enemy, with a big radius. Then, I created a script that tells the enemy to follow the player if the player collides with the sphere collider.

 public class EnemyFollow : MonoBehaviour
 {
     //I first start getting the transform (position) of my player
         public Transform target;
         //Then I set up the speed of the enemy, that I can edit later
     public float speed = 2f;
         //Lastly, I added the enemy a rigidbody
     public Rigidbody rb;
 
 //First thing, I will create a function that follows the player
 void FollowPlayer(){
         //I will create a vector 3 called pos that stores the movement that I want my player to do
          Vector3 pos = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
         //I will use these two built-in functions to follow the player
         rb.MovePosition(pos);
            transform.LookAt(target);
     }
     
 //Finally, I add a collider function that calls the FollowPlayer() function when it is within its range
 void OnTriggerStay(Collider player){
               if(player.tag == "Player"){
                 FollowPlayer();
             }
         }
 }

There are many other ways, but I think this is the simplest one and will work for you. Hope this helps!

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.

Follow this Question

Answers Answers and Comments

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

Help with Enemy AI 1 Answer

How To Make Enemy Move Around 0 Answers

Problem with a monster (he follows me backwards) 1 Answer

Enemy Pathing 1 Answer

Following AI - Similar to Snake Game 2 Answers


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