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 JayFitz91 · Nov 06, 2013 at 05:05 PM · followflare

Make enemy follow flare

 #pragma strict
 
 var flare : GameObject;
 var speed : float = 1;
 
 function Start () 
 {
     flare = GameObject.FindGameObjectWithTag("flare");
 }
 
 function Update () 
 {
 
     var distance = Vector3.Distance(flare.transform.position, transform.position);
     
     if (distance < 100)
     {
         Debug.Log ("Enemy is close to flare");
         
         var delta = flare.transform.position - transform.position;
         delta.Normalize();
         
         var moveSpeed = speed * Time.deltaTime;
         
         transform.position = transform.position + (delta * moveSpeed);
     }
     else
     {
         Debug.Log("Not close yet" + distance);
     }
 
 }

This is the script I have, it is attached to my enemy object. When i right click on the mouse, it shoots out a flare, what I want to happen is for the enemy to go towards the flare when its active, at the moment, my enemy just ignores it. Any chance anyone knows why?

Any replies appreciated.

Comment
Add comment · Show 4
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 Huacanacha · Nov 06, 2013 at 05:15 PM 0
Share

What object is this script on... Player, Enemy, or Flare?

If it's on the enemy then I don't see how the flare variable will be set, as the flare almost certainly doesn't exist when the Enemy is created.

If it's on the flare, then you are getting the flare to move towards the flare.

You need to do something like set a 'target' variable on the script of all enemies when a flare is created, rather than trying to set it when the enemy is created (if that's what you are doing). There are more efficient ways to do this but that should get things working.

Also, you can use Vetcor3.Lerp to help with the movement... it will be less lines of code than you have above.

avatar image Huacanacha · Nov 06, 2013 at 05:34 PM 0
Share

Ok so the script is on your Enemy. Remove the line where you set 'flare' in Start(), and set it on all enemies when a flare is created (and delete it when it expires). If you have an 'enemy' tag you could get all Enemy scripts and set flare that way.

avatar image TheValar · Nov 06, 2013 at 05:37 PM 0
Share

The problem may be how you are assigning flare but it's hard to say wihtout more information.

Is there a flare in the scene to begin with or is it instantiated from a prefab when the mouse is clicked?

Is it possible for there to be more than one flare in the scene at once?

avatar image JayFitz91 · Nov 06, 2013 at 05:40 PM 0
Share

Yes, there is a flare in the scene to begin with, just as a test. The enemy will move towards that flare. $$anonymous$$ore are instantiated when i use the mouse click, these show up as flare(Clone) and the enemy just ignores those ones but they are the ones i want the enemy to follow.

It is possible to have more than one as there is an inventory system that can hold upto 5

3 Replies

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

Answer by TheValar · Nov 06, 2013 at 05:45 PM

The problem is that flare is assigned only in the Start function so any flares not in the scene at the start are ignored. You need to re-assign this variable when a new flare is created. You may want to assign the flare variable in the update loop so that it's always looking for new flares.

Another problem is that GameObject.FindGameObjectWithTag("flare"); will only find one flare (not necessarily the one you want). You may want to GameObject.FindGameObjectsWithTag("flare"); to get a list of all the flares, then see which one is the closest and assign it to the flare variable.

Comment
Add comment · Show 2 · 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 Huacanacha · Nov 06, 2013 at 06:02 PM 1
Share

This really isn't the best approach. You don't want to poll for flares being created every Update, you want to push this info out whenever a flare is created. If you want the closest flare to each enemy just do a distance check before updating the flare variable. Better still you could manage flare objects somewhere centrally (store a list of all active flares) so you can access them directly and add a listener callback for interested objects that need to know about creation or destruction of flares.

avatar image TheValar · Nov 06, 2013 at 06:26 PM 0
Share

I agree completley with Huacanacha about efficiency. $$anonymous$$y way is simply the simplest implementation so that you could get the behavior working. It should certainly be optimized at some point as per Huacanacha's recommendations.

avatar image
0

Answer by Huacanacha · Nov 06, 2013 at 05:43 PM

The flare that's already in your scene will work as it exists when the Start() function is called on the Enemy script. If you create clones in code the enemy has no way of knowing what these objects are as they are created after Start has run, so you need to figure out a way to tell the enemies a new flare has been created (See GameObject. FindGameObjectsWithTag to get all objects tagged 'enemy, for example).

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 JayFitz91 · Nov 06, 2013 at 06:12 PM

Brilliant, have it working now, thanks for the help lads,i appreciate it

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 Huacanacha · Nov 06, 2013 at 06:23 PM 0
Share

You're welcome! Btw. thumbs up on any useful comments is appreciated.

This answer should really be a comment as well...

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

17 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

Related Questions

Camera rotation around player while following. 6 Answers

Gun object following mouse. 2 Answers

Problems with the script Smooth Follow 1 Answer

I want to make a train go round a track using physics? 7 Answers

Follow an object from a certain distance keep only on z-axis 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