Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 gellihorvath · Mar 16, 2020 at 05:22 PM · unity 2dtimefunctionsspace shooterspaceship

How Can I change between functions after a certain time?

Hi guys! Right now I'm making a Boss for a 2D spaceship shooter game. The Boss after a certain travlled distance appears. I would like to give the Boss three Move function for example. One is the DefaultFunction when he just follow the player and shoot, and two extra functions where he do something else. My question is how can I switch between the functions after a certain time. For example after 10 seconds he change from DefaultFunction to Function1 then after he finishes Function1 he change back to DefaultFunction. I think the two extra function will be choose by a Random number. I hope you guys understand what I would like to do! Thank you for your help!

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

1 Reply

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

Answer by Link17x · Mar 16, 2020 at 05:36 PM

It sounds like you could use Actions here. An Action is basically a piece of code that is set to run in the future at some point.


An example of this could be creating a List (or some collection) of Actions i.e.

 private List<Action> m_Actions;
 private Random m_Random;
 
 private void Start()
 {
     m_Random = new Random();
 
     m_Actions = new List<Action>
     {
        () => AttackMethod1(),
        () => AttackMethod2(),
        () => AttackMethod3()
     }
 }
 
 public Action ChooseRandomAttack()
 {
       var random = m_Random.Next(m_Actions.Count);
       return m_Actions[random];
 }
 
 private void AttackMethod1()
 {
 }
 
 private void AttackMethod2()
 {
 }
 
 private void AttackMethod3()
 {
 }

So basically, in this example, you would have three attack methods that are added to a list of actions. Then you have a public method to choose a random attack (which could be called from another class).


Obviously, change this to suit your needs. You may not want to choose the attacks at random, instead, you might change them to be chosen specifically at certain times. Maybe you don't want to choose the attack from elsewhere, and change the method to be private, etc. It's more just to demonstrate how you could use them to choose different attacks.


P.S. You can also use parameters for actions if you wanted to pass in some damage or time etc, do this the same way you would with a method normally.

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 gellihorvath · Mar 16, 2020 at 07:35 PM 0
Share

And How can I choose between the actions at certain times?

avatar image Link17x gellihorvath · Mar 16, 2020 at 08:15 PM 0
Share

I mean, that's up to you when you want to use them. But you can create some sort of timer and then just call the methods when you want to use them...something like this as an example, if it's just time based you can just call the methods, up to you.

 private float CurrentTime;
 
 Private void Update()
 {
     CurrentTime += Time.deltaTime;
 
     if (CurrentTime >= 10)
     {
         Attack$$anonymous$$ethod1();
     }
     If (CurrentTime >= 20)
     {
         Attack$$anonymous$$ethod2();
         CurrentTime = 0;
     }
 } 

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

130 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 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 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 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 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 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

My space ship is sliding to one side and getting stuck there, what would cause this? 1 Answer

How to get a constantly updating variable to a non-constantly updated function. 1 Answer

unity3d quaternion slerp is off target 0 Answers

How to use collision.name in custom function 1 Answer

My 2d tractor beam is not working 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