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 /
  • Help Room /
avatar image
0
Question by laurienash · Jul 02, 2016 at 02:24 PM · c#transformcoroutinelerpyield

Coroutine: delay transform rotation but start transform movement immediately

Hi,

At the moment I've written this script so that when a boolean value is toggled on, I move the camera to a new position and rotation over time.

The problem I'm having is that in the coroutine MoveToPositionOrtho, I want to start the movement immediately, but have a delay of 2 seconds before rotating the camera. But I still want the movement and rotation to finish at the same time.

I can't work out how I need to write this, because if I move the transform.position above the line yield WaitForSeconds this stops the movement during the delay.

Does anyone have any pointers on how I should go about scripting this - is it possible to have a delay only affecting the rotation and not the movement?

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof(MatrixBlender))]
 public class PerspectiveSwitchFinal : MonoBehaviour {
     
     private Matrix4x4   ortho, perspective;
     public float fov = 60f,
     near = 0.3f,
     far  = 1000f,
     orthographicSize = 50f;
     private float aspect;
     private MatrixBlender blender;
     public bool orthoOn;
     
     public GameObject cameraMain;
     
     private float degree;
     private float angle;
     
     
     void Start()
     {
         aspect = (float) Screen.width / (float) Screen.height;
         ortho = Matrix4x4.Ortho(-orthographicSize * aspect, orthographicSize * aspect, -orthographicSize, orthographicSize, near, far);
         perspective = Matrix4x4.Perspective(fov, aspect, near, far);
         GetComponent<Camera>().projectionMatrix = ortho;
         orthoOn = true;
         blender = (MatrixBlender) GetComponent(typeof(MatrixBlender));    
         
     }
 
 
 
     public void orthoToggle(bool on)
     {
         orthoOn = on;    
 
         if (orthoOn != true)
         {
             blender.BlendToMatrix(perspective, 12f);
             degree = 6f;
             StopAllCoroutines();
             StartCoroutine (MoveToPositionPersp (new Vector3(0,0.5f,-12f), 8f, 0f));
         }
         
         
         if (orthoOn == true)
         {
             blender.BlendToMatrix(ortho, 6f);
             degree = 45f;
             StopAllCoroutines();
             StartCoroutine ( MoveToPositionOrtho (new Vector3(0,5.5f,-3.6f), 2f, 4f)); //4 3.2
         }
 
     }
     
     
     
     public IEnumerator MoveToPositionOrtho(Vector3 position, float timeToMove, float waitTime)
     {
         yield return new WaitForSeconds(waitTime);
         
         
         var currentPos = transform.position;
         var currentPosRotate = transform.rotation;
         var t = 0f;
         
         
         while(t < 1)
         {
             t += Time.deltaTime / timeToMove;
             
             transform.position = Vector3.Lerp(currentPos, position, t);
             
             angle = Mathf.LerpAngle(transform.rotation.x, degree, Time.deltaTime);
             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(degree, 0, 0), t);
             
             yield return null;
         }
     }
     
     
     
     
     
     public IEnumerator MoveToPositionPersp(Vector3 position, float timeToMove, float waitTime)
     {
         yield return new WaitForSeconds(waitTime);
         
         var currentPos = transform.position;
         var currentPosRotate = transform.rotation;
         var t = 0f;
         
         
         while(t < 1)
         {
             t += Time.deltaTime / timeToMove;
             transform.position = Vector3.Lerp(currentPos, position, t);
             
             angle = Mathf.LerpAngle(transform.rotation.x, degree, Time.deltaTime);
             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(degree, 0, 0), t);
             
             yield return null;
         }
     }
     
 }


Any help would be so much appreciated!

Best, Laurien

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

2 Replies

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

Answer by NoseKills · Jul 02, 2016 at 10:14 PM

Calculate another 't' that for example starts '2 seconds below zero' , increment it proportionally faster so it reaches 1 at the same time with 't' and use that value as 't' for rotation.

 const float delay = 2f;
 var t = 0f;
 var ratio = delay/(timeToMove - delay);// ratio of delay vs. time left for rotation
 var t2 = -ratio;
 while(t < 1)
 {
     t += Time.deltaTime / timeToMove;
     t2 += Time.deltaTime / (timeToMove/(1 + ratio));
     ...
    transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(degree, 0, 0), t2);
 }

On my mobile. Not sure about the math coz i couldn't test it.

Another way of course would be to use separate coroutines for movement & rotation.

You must separately handle situations where timeToMove is less than delay.

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 laurienash · Jul 02, 2016 at 11:33 PM 0
Share

That's so great thanks - I've got that working now!!! Though what do you mean by separately handling the situations where timeTo$$anonymous$$ove is less than the delay, as isn't the delay is always fixed?

avatar image NoseKills laurienash · Jul 03, 2016 at 01:02 PM 0
Share

Delay is always 2 in this code but if you call the nethod with parameter 'timeTo$$anonymous$$ove' as 1, this will break.

avatar image laurienash NoseKills · Jul 03, 2016 at 01:57 PM 0
Share

Ohhh of course - thank you :)

avatar image
0

Answer by SrGonzalez · Apr 19, 2017 at 04:48 PM

you must use better in the coroutine that:

 IEnumerator Counter()
 {
     yield return new WaitForSeconds(2);     //Anything after this line is going to be called
     //here goes your code                   //after 2 seconds
 }
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

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

MoveTowards and Lerp not working 1 Answer

Object moving chaotically using lerp and repeating. 1 Answer

Transform search function only sporadically works?! 1 Answer

Nothing after coroutine gets executed 1 Answer

Problem with Mathf.Lerp 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