• 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
Question by octolord · Apr 21, 2015 at 05:59 AM · javascriptenemyinvoketiming

Making Enemy Retreat

Hello! I'm trying to make an enemy retreat after so many seconds after it's spawned. I tested out the transform.position and the enemy moves straight towards, so I know that part works. However, when I tried to make the time delay happen by using invoke() code and putting the transform code in a custom function, the enemy remains where it was spawned.

  #pragma strict
     
     public var enemyspeed : float; //how fast enemy goes
     private var retreatPoint: Vector3; //holds the coordinates to the retreat point
     private var step : float; //speed *frames value
     
     function Awake () {
         // sets speed of enemy
         var step = enemyspeed * Time.deltaTime;
     }
     
     function Start () {
 //after 4 seconds, bunyip retreats
         Invoke ("BunyipRetreat", 4);
     }
     
     function Update () {
         //coordinates for retreatingpoint
             retreatPoint = Vector3(24,0,20);
         
     }
 
 //custom function
     function BunyipRetreat() {
 //moves bunyip to retreating point
         transform.position = Vector3.MoveTowards(transform.position,retreatPoint,step);
     }
Comment

People who like this

0 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 KdRWaylander · Apr 21, 2015 at 06:44 AM 0
Share

First of all you have some illogical things here in your code: - Awake will be called only once, you shouldn't put something whose value changes at every frame (Time.deltaTime) - Update is read at each frame, and you put something static in it, once you told the coordinates to the computer, it will remember it, you don't need to tell him back everytime.

Hence, you should but what is in your Awake, in your Update and what is in your Update in your Start (or Awake, but Start is enough here)

Instead of invoke you can also make a timer (if you don't know how, there are a lot of answers here on Unity Answers), when you reach your timer, you set a bool to true. And in Update you add an if statement: if my bool about retreat i true, call the bunyipRetreat() function.

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by DwaynePritchett · Apr 21, 2015 at 06:21 AM

I think MoveTowards needs to be in the Update function. Try this.

  #pragma strict
      
      public var enemyspeed : float; //how fast enemy goes
      private var retreatPoint: Vector3; //holds the coordinates to the retreat point
      private var step : float; //speed *frames value
      public var retreating : bool; //A trigger to see if retreating is true
      
      function Awake () {
          // sets speed of enemy
          var step = enemyspeed * Time.deltaTime;
      }
      
      function Start () {
  //after 4 seconds, bunyip retreats
          Invoke ("BunyipRetreat", 4);
      }
      
      function Update () {
          //coordinates for retreatingpoint
              retreatPoint = Vector3(24,0,20);
          if(retreating){
                   transform.position = Vector3.MoveTowards(transform.position,retreatPoint,step);
          }
      }
  
  //custom function
      function BunyipRetreat() {
      retreating = true;
 
      }



Note* I do not use UnityScript/JavaScript, so that might be broken terribly. But, I hope it gives you an idea of what you need to do.

-Dwayne Pritchett

Comment
octolord

People who like this

1 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 octolord · Apr 21, 2015 at 07:13 AM 0
Share

It worked!! Thank you very much!

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

2 in 1 question - Enemy transforms and Frames per second 0 Answers

Enemy not rotating when inside range 1 Answer

Decreasing Player Health On Collision with Enemy 2 Answers

Counting down in seconds opposed to frames 1 Answer

Once game is built, it crashes when enemy dies. 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