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
1
Question by tuto424 · May 09, 2017 at 06:13 AM · movementmultipleactionexecution

Executing multiple actions one after another Unity

Hi, every one. I want execute multiple action in one only funtion, for example

 // "moveForward, rotate and moveBackward" are function that have diferent time of execution
 // I want to see slow execution 
   void Update()
   {
         if (Input.GetKey(KeyCode.UpArrow))
         {
             moveForward();
             for(i=0 to 6){
                 rotate(90°);
             }

             moveBackward();
         }
 }

 public void moveForward(){
     for(1 to 500){
         gameObject.Transform. position = new vector3(1,y,z);
         sleep(0.500s);
     }
 }

 public void rotate(){}
 public void moveBackward(){}


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
1
Best Answer

Answer by slavo · May 09, 2017 at 11:12 AM

Hello,

Coroutines + dumping should do the job.

https://docs.unity3d.com/Manual/Coroutines.html https://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html https://docs.unity3d.com/ScriptReference/Vector3.RotateTowards.html

Cheers

Comment
Add comment · Show 4 · 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 tuto424 · May 09, 2017 at 12:58 PM 0
Share

Hi Slavo. Fine, but a want execute first action after others action. See example(first moveForward after rotate after moveBackward). Whit Coroutine this execute in parallel (I dont want this)

avatar image slavo tuto424 · May 09, 2017 at 01:43 PM 1
Share

You can achive this using coroutines. If you yield coroutine, then code is waiting for it ins$$anonymous$$d of run it in parallel.

     float speed = 5f; //m/s
     float angularSpeed = 10f; //degres per sec
 
     IEnumerator PerformStuffs()
     {
         for (int i = 0; i < 500; i++)
         {
             yield return $$anonymous$$oveForward();
         }
 
         for (int i = 0; i < 6; i++)
         {
             yield return Rotate();
         }
 
         yield return $$anonymous$$oveBackward();
     }
 
     //$$anonymous$$ove Forward 2m
     IEnumerator $$anonymous$$oveForward()
     {
         var newPos = transform.position + transform.forward * 2f;
 
         while (Vector3.Distance(newPos, transform.position) > 0.01f)
         {
             transform.position =  Vector3.$$anonymous$$oveTowards(transform.position, newPos, Time.deltaTime * speed);
             yield return null; //wait for next frame
         }
     }
 
     //$$anonymous$$ove Back 2m
     IEnumerator $$anonymous$$oveBackward()
     {
         var newPos = transform.position - transform.forward * 2f;
 
         while (Vector3.Distance(newPos, transform.position) > 0.01f)
         {
             transform.position = Vector3.$$anonymous$$oveTowards(transform.position, newPos, Time.deltaTime * speed);
             yield return null; //wait for next frame
         }
     }
 
     IEnumerator Rotate()
     {
         Quaternion desiredRotation = transform.rotation * Quaternion.Euler(0f, 90f, 0f);
 
         while (Quaternion.Angle(transform.rotation, desiredRotation) > 0.5f)
         {
             Quaternion.RotateTowards(transform.rotation, desiredRotation, angularSpeed * Time.deltaTime);
             yield return null; //wait for next frame
         }
     }
avatar image tuto424 slavo · May 10, 2017 at 02:14 AM 0
Share

Its work fine (y), thanks

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Having a lot of trouble with 2d movement 1 Answer

Third person controller (fast paced, action RPG, rigidbody) 1 Answer

New Input System 'Started' and 'Performed' actions fire at the same time? 1 Answer

Barrel Roll on Double Key Press, right & left 2 Answers

RTS Movement and grid questions 0 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