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 /
avatar image
0
Question by Chappy · Nov 18, 2013 at 07:20 PM · c#enemyfollow

Enemies running when they should be following.

I've got a basic follow script set up for a set of enemies. The script is supposed to make the enemy look at the player up to a certain distance and, when the player gets close enough, follow the player. The script sort of works, but for some reason, when I set the maximum follow distance to anything above 10, the enemies will run away from the player instead. If I somehow get the player to the with the 10 range of the enemy before they spawn they will follow as they are supposed to.

Here's the entire enemy code:

 public float MaxDistance = 10.0f;
 public float MoveSpeed = 5.0f;
 public float MinDistance = 0.0f;
 public Transform Target;
 public float LookAtDistance = 10.0f;
 public float Distance;
 public float Damping = 5.0f;
 private Quaternion rotation;
 public bool parenting = false;
 private PlayerHealth_Test playerHealth;
 private float nextDam;
 //private Bank rolling;
 public bool rolling = false;
 public float DamDelay = 2.0f;
 private Leech_Mother_AI_Test spawnCount;
 private GameObject LeechMother;
 
 
 
 void Start (){
 
     Target = GameObject.FindGameObjectWithTag ("Player").transform;
     playerHealth = Target.GetComponent<PlayerHealth_Test>();
     parenting = false;
     // rolling = Target.GetComponent<Bank>();
     nextDam = Time.time + DamDelay;
     GameObject spawner = GameObject.Find ("Leech_Mother");
     
     spawnCount = spawner.GetComponent<Leech_Mother_AI_Test>();
     
     
 }
 
 
 void OnTriggerEnter(Collider Player)
     {
         transform.parent = GameObject.FindGameObjectWithTag("Player").transform;        
             parenting = true;    
         }
     
 void Update () {
         
 
     Distance = Vector3.Distance (Target.position + Target.transform.forward, transform.position);
         
     if (Distance < LookAtDistance)
     {
         LookAt();
     }
         
     if (Distance > MinDistance && Distance < MaxDistance && parenting == false)
     {
         follow ();
     }
         
     if (rolling == true)
         {
             parenting = false;
             GameObject.Destroy(this.gameObject);
             
             spawnCount.SpawnDown(1f);
         }
         
     if (parenting == true && nextDam <= Time.time)
         {
             nextDam = Time.time + DamDelay;
             playerHealth.CurrentHealth -= 1.0f;
         }    
         
     if (parenting == true && playerHealth.CurrentHealth <= 0)
         {
             parenting = false;    
         }
         
 }
 
 void LookAt(){
 
     rotation = Quaternion.LookRotation (Target.localPosition - transform.position);
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
     
 }
     
 void follow(){
         
     transform.Translate(Vector3.forward * MoveSpeed * Time.deltaTime);
         
         
 }
 
     
     
 }
 
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 gajdot · Nov 18, 2013 at 07:27 PM 0
Share

Wow, you wrote quite a code to do this... Just a small tought before I think of an answer, you know that the transform have a lookAt function? Plus I'm not sure if your distance calculation is correct.

avatar image Chappy · Nov 18, 2013 at 07:43 PM 0
Share

This is technically the entire enemy AI which includes dealing damage, dying, parenting and the like. I just like putting the entire code up just in case my problem is somewhere else in the code that I didn't think to look. Also, this is one of the first $$anonymous$$ajor pieces of code that I've done so it's rather...messy.

The follow and LookAt function I got from a tutorial on the subject, I basically copied those parts of the code over. And again, unless I put the $$anonymous$$axDistance above 10 the enemy will follow and if I somehow get within 10 before the enemy spawns they still follow even if the $$anonymous$$axDistance is above that.

1 Reply

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

Answer by tanoshimi · Nov 18, 2013 at 08:54 PM

"when I set the maximum follow distance to anything above 10, the enemies will run away from the player instead"

The reason your enemies "run away" is because, based on these two lines, your enemies only start looking at the player when they are less than 10 units away:

 public float LookAtDistance = 10.0f;
 if (Distance < LookAtDistance)
 {
   LookAt();
 }

If you set the "max follow Distance" anything greater than the "LookAtDistance" (currently 10), then they're still attempting to follow the player (i.e. the follow() routine still executes), but they do so by just walking forward in whatever direction they happen to be facing at the time:

 transform.Translate(Vector3.forward * MoveSpeed * Time.deltaTime);
Comment
Add comment · Show 1 · 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 Chappy · Nov 19, 2013 at 03:29 AM 0
Share

I...I just...GOD I'$$anonymous$$ DU$$anonymous$$B! Deletes code, burns laptop, becomes hermit

I honestly didn't even thing about the fact that the "forward" of the enemies was the issue. I guess you learn something new about coding every day. ...Even if that thing is something you should have known months ago.

Thank you very much for clearing that up.

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

20 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

Related Questions

Enemy Targetting Help 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Enemy Pirate Ship AI 1 Answer

c# array of game objects movement help 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