Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by anynomous_10 · Jul 31, 2014 at 08:54 PM · aienemy ai

enemy AI follows exact path

how to make enemy AI which will follow the exact path of a player in FPS game.. it will be great if you tell with example! thanks in advance! :)

Comment
Add comment · Show 5
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 robertbu · Jul 31, 2014 at 10:18 PM 1
Share

The only way for an enemy to follow an exact path is to create a path (array of Vector3 positions) as the player moves along for the enemy to follow. Even then, it won't be absolutely exact unless you do the player movement and the path point saving in FixedUpdate and move the enemy from point to point defined by the path. $$anonymous$$ost of the time for this kind of thing, the path is treated as a series of waypoints, so the enemy can land between the Vector3 points saved by the player.

avatar image Kiwasi · Jul 31, 2014 at 10:34 PM 1
Share

In some situations you could store the sequence of inputs a player makes, and use it to drive the AI with the same inputs at a time delay. It does rely on the AI starting in the same state as the player started in.

This is probably not worth doing in most situations.

avatar image robertbu · Jul 31, 2014 at 10:37 PM 1
Share

@Bored$$anonymous$$ormon - cool alternative that I did not consider. Would likely not work if any physics was involved though.

avatar image anynomous_10 · Aug 01, 2014 at 04:40 AM 0
Share

guys, can you give some code like how to take players Vector3 positions in array and then make enemy to follow them. that would be great! cheers!

avatar image Kiwasi · Aug 01, 2014 at 06:48 AM 0
Share

You might get lucky, but generally UA frowns on requests to write scripts.

A lot is contextual, how your player and enemy move is a key consideration.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Bilelmnasser · Aug 01, 2014 at 06:58 AM

 var target : Transform; //the enemy's target
 var moveSpeed = 3; //move speed
 var rotationSpeed = 3; //speed of turning
 var range : float=10f;
 var range2 : float=10f;
 var stop : float=0;
 var myTransform : Transform; //current transform data of this enemy
 function Awake()
 {
     myTransform = transform; //cache transform data for easy access/preformance
 }
  
 function Start()
 {
      target = GameObject.FindWithTag("Player").transform; //target the player
  
 }
  
 function Update () {
     //rotate to look at the player
     var distance = Vector3.Distance(myTransform.position, target.position);
     if (distance<=range2 &&  distance>=range){
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
     Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
     }
  
  
     else if(distance<=range && distance>stop){
  
     //move towards the player
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
     Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
     }
     else if (distance<=stop) {
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
     Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
     }
  
  
 }
Comment
Add comment · Show 3 · 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 Kiwasi · Aug 01, 2014 at 07:11 AM 0
Share

This is just a standard follow script. Not sure how it helps the OP.

avatar image anynomous_10 · Aug 01, 2014 at 08:41 AM 0
Share

@Bored$$anonymous$$ormon nope man! not this one! Still thanks for ur time! :)

avatar image fallengamer · Jan 01, 2017 at 06:24 AM 0
Share

Due to this script enemies start moving upward don't know why? I am using root motion and animations from $$anonymous$$ixamo . help needed

avatar image
0

Answer by behzad.robot · Aug 01, 2014 at 09:01 AM

I've not done this before but i did come up with a soloution for this in my mind a while ago..if ur player has a rigidbody(if not the only thing we need is to know it's speed!)we can handle things like this:

Step #1:When Ever Player's speed has changed it means he's going somewhere!

Step #2:We'll create a pathpoint for EnemyAI at player current position which will tell AI change ur speed to this!

Step #3:if player stopped somewhere for a few seconds then what?!?!?! Answer:If This PathPoints Speed is zero also save how much time did our player spent standing here!

Step #4:There's no Step#4! :P

So Let me clear this with an example: -Player Starts moving with velocity.x=10 Make a pathpoint here with it's speed property(A vector 3) as x=10! -Player goes forward with that speed a bit then stops changes his speed to velocity.z=10 So we make a pathpoint here with this as SpeedProperty(X=0,Y=0,Z=10).

Now our AI Gets in the First PathPoint And Sees wow i must go forward with velocity.x = 10 speed so it goes forward reaches the second point and starts with this new speed that this new path point is giving him(X=0,Y=0,Z=10)! So like this the enemy is going the exact same way the player went but if player drops some obstacles while he's going i'm sure our AI would be in trouble but if player has to avoid some obstacles in his way the AI would do the exact same thing! Unfortunately i'm way too lazy to write the whole code at the moment but if u dont get it and need the code i'll do it no worries! :D

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

6 People are following this question.

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

Related Questions

how to refer to enemy health 2 Answers

Enemy AI problems 2 Answers

Vision cone with obstacles? 2 Answers

How do I make a 2d enemy change direction upon hitting a wall? 1 Answer

Looping Between Waypoints 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