• 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 Jogabba · Mar 19, 2014 at 01:15 PM · coroutinesswitch

Concatenate two behaviours

So what I'm trying to do now is to make an object move in a random direction a certain distance. Then it stops, and shoots some projectiles during a determined amount of time. After this, it all starts over again, moving again and shooting after running a certain distance.

So, what I have is two separate functions: one that makes the object move and one that makes the object shoot. But I don't know how to alternate between these two functions. It seems that coroutines or state machines are an appropiate option to do this, but as a newbie I don't know how to use them properly. Specifically, having both states on a switch, I'm having problems switching them over time, so maybe there's an "appropiate" way to do this that I haven't discovered.

Any clues will be appreciated! :D

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 Fattie · Mar 19, 2014 at 01:16 PM 0
Share

If they are two separate scripts ... and they should be:

simply attach both, to the object.

it's that simple. it's at the absolute heart of how you work with game engines. Enjoy.

avatar image Jogabba · Mar 19, 2014 at 10:20 PM 0
Share

But that does not seem to respect the functions order, it makes them work together. In other words, it makes the object move and shoot at the same time. What I want to do is make it move first, and then stop and shoot, and then stop shooting and move again, etc.

avatar image Fattie · Mar 20, 2014 at 06:16 AM 1
Share

It's very likely the Invoke command or InvokeRepeating will solve your problems. It's how you "order" things in video games.

Imagine the "start of a car race" ..

 Invoke 1.0  light turns yellow
 Invoke 2.0 light turns green
 Invoke 2.0 release brakes
 Invoke 2.5 start competitor cars

and so on .. you get it? Enjoy

avatar image Jogabba · Mar 20, 2014 at 06:28 PM 0
Share

Thanks! It helped me understand how things should be done. (I'd really like to vote up your comment, but for some reason the upvoting button does not show...)

1 Reply

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

Answer by stevethorne · Mar 20, 2014 at 06:43 PM

Sounds like you'll want to use both of these functions that you've made in the Update() loop of the MonoBehaviour. Then, just like you said, use something to switch back and fourth. Here's an example.

 float timer = 3.0f;
 float timeToMove = 3.0f;
 float timeToShoot = 1.0f;
 float shooting = false;
 
 void Update()
 {
     timer -= Time.deltaTime;
 
     if ( shooting )
     {
         Shooting();
 
         if ( timer <= 0 )
         {
             timer = timeToMove;
             shooting = false; // switch back to moving
         }
     }
     else
     {
         Moving();
 
         if ( timer <= 0 )
         {
             timer = timeToShoot;
             shooting = true; // switch back to shooting
         }
     }
 }
 
 void Moving()
 {
     // your moving function
 }
 
 void Shooting()
 {
     // your shooting function
 }

This is an example of what you can do with timers and it's done in C# but you can apply this knowledge to another approach and in js.

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 Fattie · Mar 21, 2014 at 05:34 AM 1
Share

Steve, for beginners it's much better to master Invoke, which is one line of code

avatar image stevethorne · Apr 02, 2014 at 09:11 PM 0
Share

I'd have to disagree with you there. Better to understand fundamentals of programming then to rely on things that do the magic without you knowing about it.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

22 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 avatar image avatar image

Related Questions

Toggle GUI button 1 Answer

Multiple Camera.main.ScreenPointToRay 1 Answer

Changing player model with keypress. 3 Answers

changing lightmaps with javascript / saving multiple lightmaps to lightmap array 1 Answer

Singling out mutiple Character movement 3 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