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
1
Question by Sythen · Jan 16, 2012 at 02:28 AM · enemyissuefollow

Enemy Follow Issue

My issue resides with the Enemy Ghosts in my game, I have created a targetarea (trigger sphere) around each enemy. When the player enters the sphere of one enemy, this script is called:

 function OnTriggerStay (other3 : Collider) {
 
     
     if (other3.gameObject.tag == "Player")
     {
         Safe = true;
         Debug.Log("Safe");
     }
     
 }

Which leads then onto the safe function,

 if (EnemyTargetArea.Safe)
 {
     EnemyGhostMovement.speed = 0.55;

 }

Unfortunately for some odd reason, when the player enters one targetarea sphere of a single enemy. All enemies follow. How do I fix this issue?

  • Sythen.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Posly · Jan 16, 2012 at 03:32 AM

Haha what a coincidence I was working on a enemy follow script today too! Here's what I did and it works fine:


var speed : int = 0; var followrange : int; var stoprange : int; static var canmove : boolean = true; var target : Transform; private var stopped : boolean = false;

function Update(){

if(Vector3.Distance(target.position, transform.position) speed Time.deltaTime); }

if(Vector3.Distance(target.position, transform.position)

if(Vector3.Distance(target.position, transform.position) > stoprange) { stopped = false; }

if(Vector3.Distance(target.position, transform.position) > followrange) { canmove = true; }

}


I just used vector3 distances instead of triggers. You can edit the speed at which the enemy follows you, the range with followrange, and stoprange is if you want him to stop at a certain range when he comes close to you. I hope it works!

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 aldonaletto · Jan 16, 2012 at 03:45 AM

EnemyTargetArea.Safe is a static variable: a static variable is unique, thus setting it in one script affects all other script instances (it's the same variable for everybody).
You should remove the static key word from the Safe variable declaration - and I suspect that you should do it with speed in EnemyGhostMovement as well, or all enemies will have the same speed.
The solution depends on your game object hierarchy. If the trigger is an enemy's child and EnemyGhostMovement is attached to the enemy, you could do the following:

// EnemyTargetArea script:

function OnTriggerEnter (other3 : Collider) { if (other3.gameObject.tag == "Player") { // call the function SetSpeed(0.55) in the parent or grandparent SendMessageUpwards("SetSpeed", 0.55); } }

// in the EnemyGhostMovement script:

var speed: float;

function SetSpeed(vel: float){ speed = vel; }

Another alternative that follows your original script (and it's a little faster too):

// EnemyTargetArea script:

var Safe: boolean = false;

function OnTriggerEnter (other3 : Collider) { if (other3.gameObject.tag == "Player") { Safe = true; } }

// in the EnemyGhostMovement script:

var speed: float; // remove the static keyword! var areaScript: EnemyTargetArea; // declare a reference to the script ... // find script if necessary (happens only once): if (!areaScript) areaScript = GetComponentInChildren(EnemyTargetArea); if (areaScript.Safe) speed = 0.55; ... Notice that I changed OnTriggerStay to OnTriggerEnter: usually you should set Safe true in OnTriggerEnter and false in OnTriggerExit, thus the variable would show whether the player is inside the area or not.

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 Sythen · Feb 01, 2012 at 11:59 AM

Hey guys,

I fixed the issue; Rather than using a hit sphere around each enemy using the same script I removed it, and searched for the distance between that and the player. In this case, it dealt with each enemy separately and allowed them to move correctly.

In short, aldonaletto, your answer about static variables did allow me to see where I was going wrong, but posly, your script about the difference between target distance and player distance was most correct and helpful.

Thank you guys for your help, - Sythen.

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

make enemy attack 0 Answers

Enemy not following player. 2 Answers

Script to make an enemy follow the player when they trigger a door? 0 Answers

Enemy Targetting Help 1 Answer

Enemy Script 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