Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 RaiderWrestler133 · Aug 12, 2015 at 04:21 AM · instantiatepathing

Pause the enemy follow script after instantiate (2D, C#)

I have the following script that forces the "BabySpider" to chase after the player:

void FixedUpdate() {

     //Use the following script to have an enemy run towards and attempt to touch the player
     target = GameObject.FindWithTag("Player").transform;

     Vector3 forwardAxis = new Vector3(0, 0, -1);




     transform.LookAt(target.position, forwardAxis); //this makes the enemy face the direction that it is travelling
     Debug.DrawLine(transform.position, target.position); 
     transform.eulerAngles = new Vector3(0, 0, -transform.eulerAngles.z); 
     transform.position -= transform.TransformDirection(Vector2.up) * speed; //This equation determines the speed that the enemy is travelling
 }


The are instantiated out of an "EggSack". I want them to pause for a second or two before they begin going after the player.

Comment
Add comment · Show 3
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 RaiderWrestler133 · Aug 12, 2015 at 01:55 AM 0
Share

I tried searching for the terms you mentioned but I couldn't find a good answer. I may not be typing in the right key words.

avatar image Sethhalocat · Aug 16, 2015 at 08:50 AM 0
Share

R8 Up bro :D

avatar image Priyanshu · Aug 16, 2015 at 09:14 AM 0
Share

Please click Tick, If answered correctly. SO that others can know the Question is answered.

alt text

tick.jpg (1.4 kB)

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by GiyomuGames · Aug 12, 2015 at 05:50 AM

You can simply store the Time.time in the Start method of your script, then in your FixedUpdated you don't do anything until the actual time is greater than start time + 1 or 2 seconds.

1 additional tip, put this

target = GameObject.FindWithTag("Player").transform;

in your Start method. It is unnecessary to call it at every cycle.

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 getyour411 · Aug 12, 2015 at 06:28 AM 1
Share

$$anonymous$$ight as well tack on - why is OP using FixedUpdate(), there's no physics or Rigidbody involved here.

avatar image GiyomuGames · Aug 12, 2015 at 06:32 AM 0
Share

Ah yes, that is correct ^^

avatar image Shadowphax · Aug 12, 2015 at 12:58 PM 0
Share

@getyou411 Because it gaurentees framerate independence. Unless you want to write "* Time.deltaTime" everywhere. Better use FixedUpdate unless the frequency of everything in the function will never ever influence the gameplay in any way.

avatar image
1

Answer by Runalotski · Aug 12, 2015 at 10:27 AM

You can use a Corutine to delay functions as they wont go past the yield point until the specified time has passed wich is one second in this example in "IEnumerator SpiderHatching()"

Then we only want it to run once when the spider is Instantiated so we start the corutine in the start function

and then write the UpdateCode inside an if target != null so that if target is not set to anything yet then the spider will not move until it is given a target.

 IEnumerator SpiderHatching()
     {     
           //this code will wait for one second
           yield return new WaitForSeconds(1f);
     
           //and then set the player as target
           target = GameObject.FindWithTag("Player").transform;
     }
     
     //now we want the start this when the spider is spawned
     void Start()
     {
          StartCoroutine(SpiderHatching());
     }
     
     //now change you update to not run if target is Null
     
     void Update()
     {
         if(target != null)
         {
              //I took this line out because we do not need it
              //target = GameObject.FindWithTag("Player").transform;
              Vector3 forwardAxis = new Vector3(0, 0, -1);
      
              transform.LookAt(target.position, forwardAxis); //this makes the enemy face the direction that it is travelling
              Debug.DrawLine(transform.position, target.position); 
              transform.eulerAngles = new Vector3(0, 0, -transform.eulerAngles.z); 
              transform.position -= transform.TransformDirection(Vector2.up) * speed;
         }
     }
 
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

Instantiate an object in a prefab? 1 Answer

Checking if object intersects? 1 Answer

Instantiated objects at beginning of runtime are messing with the pathing for my NavMesh. 0 Answers

gameobject spawn in randomrange coordinates 1 Answer

Why is there a difference between script and in-game sequence of a script loop? 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