Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Kalicronic · Aug 18, 2021 at 05:44 PM · rotationmovementtexturecoroutinelerp

rotate object by list values

Hi guys,

maybe I have had a bad day, but I can't find the fail. I want to move a camera through a list of positions and rotations. Positions work fine, but the rotation don't work.

Here the code:

 private IEnumerator startStopCoroutine()
     {
         isPlaying = true;
 
         Transform activeCam = currentCameraSettingsScrp.transform;
         CameraPath currentPath = currentCameraSettingsScrp.getCameraPath();
         List<Tuple<int, string, Vector3, Vector3>> poseList = currentPath.getPoseList();
 
         //set camera to first pose
         CameraSettings.getCurrentActiveCameraScrp().setCameraToPose(poseList[0]);
 
         int pathIndex = 0;
         float time = Time.deltaTime;
         Vector3 startPos = activeCam.position;
         Quaternion startRot = activeCam.rotation;
 
         while (isPlaying && pathIndex < poseList.Count)
         {
             //loop when pause is clicked
             while (isPause)
             {
                 if (!isPlaying)
                     break;
                 yield return null;
             }
 
             //move by time
             time += Time.deltaTime / currentPath.animationDuration;
             activeCam.position = Vector3.Lerp(startPos, poseList[pathIndex].Item3, time);
 
             //IT DONT WORK!!!!
             activeCam.rotation = Quaternion.Lerp(startRot, Quaternion.Euler(poseList[pathIndex].Item4), time);
 
             //handle reaching points
             if (activeCam.position == poseList[pathIndex].Item3 &&
                 activeCam.rotation == Quaternion.Euler(poseList[pathIndex].Item4))
             {
                 pathIndex++;
                 time = Time.deltaTime;
                 startPos = activeCam.position;
                 startRot = activeCam.rotation;
             }
 
             yield return null;
         }
 
         btStartPause.transform.GetChild(0).GetComponent<Text>().text = ">";
         isPlaying = false;
     }
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 DenisIsDenis · Aug 19, 2021 at 03:36 AM 1
Share

Quite strange. To test your script, I had to change it a bit. And the camera after testing both moved and rotated as needed. Perhaps the problem is hidden in other scripts (maybe the camera rotation is also assigned somewhere?). It is also possible that the list is filled incorrectly, or is read incorrectly… Here is the script that worked for me:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MoveAndRotate : MonoBehaviour
 {
     bool isPlaying;
     public List<Vector3> posList = new List<Vector3>();
     public List<Vector3> rotList = new List<Vector3>();
     private bool isPause;
     public float animationDuration = 2;
 
     void Update()
     {
         if (!isPlaying)
             StartCoroutine(startStopCoroutine());
     }
 
     private IEnumerator startStopCoroutine()
     {
         isPlaying = true;
 
         Transform activeCam = Camera.main.transform;
         activeCam.position = posList[0];
         activeCam.rotation = Quaternion.Euler(rotList[0]);
 
         int pathIndex = 0;
         float time = Time.deltaTime;
         Vector3 startPos = activeCam.position;
         Quaternion startRot = activeCam.rotation;
 
         while (isPlaying && pathIndex < posList.Count)
         {
             while (isPause)
             {
                 if (!isPlaying)
                     break;
                 yield return null;
             }
 
             time += Time.deltaTime / animationDuration;
             activeCam.position = Vector3.Lerp(startPos, posList[pathIndex], time);
 
             //it works for me
             activeCam.rotation = Quaternion.Lerp(startRot, Quaternion.Euler(rotList[pathIndex]), time);
 
             if (activeCam.position == posList[pathIndex] &&
                 activeCam.rotation == Quaternion.Euler(rotList[pathIndex]))
             {
                 pathIndex++;
                 time = Time.deltaTime;
                 startPos = activeCam.position;
                 startRot = activeCam.rotation;
             }
 
             yield return null;
         }
 
         isPlaying = false;
     }
 }

And I filled in the lists this way:

Move And Rotate Component Image

moveandrotatecamera.png (16.8 kB)
avatar image Kalicronic DenisIsDenis · Aug 19, 2021 at 08:28 AM 0
Share

Thanks for the effort. I now had the problem that it started to rotate and stopped rotating after the first or second element. It was due to the if query not recognizing that the rotation was reached. I replaced this with a "Vector.Distance() < 0.001f" query. Now it works.

The problem is that there is a tolerance range with the Distance() query, which I want to avoid. The rotation must be completed by the time variable. I don't quite understand why this is not the case.

And no, the camera transform is not accessed anywhere else.

Translated with www.DeepL.com/Translator (free version)

avatar image Bunny83 Kalicronic · Aug 19, 2021 at 12:44 PM 1
Share

You're doing an actual proper Lerp, so your condition when you reached the target is simply

 if(time >= 1f)

Also instead of setting time to Time.deltaTime when you start with the next node, you can simply subtract "1f" from time. This will properly carry over any fractional overshoot from the previous run.


Also initially you should set time to 0 and not Time.deltaTime. Otherwise you give it an unreasonable headstart. The first thing you do inside your loop is increasing time.


Finally your if statement inside your "pause" loop looks strange and can be reduced to

 while (isPause && isPlaying)
 {
     yield return null;
 }

0 Replies

· Add your reply
  • Sort: 

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

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

'Clear' a Coroutine? 1 Answer

Coroutine Moving 1 Answer

better way to rotate instead Coroutine 1 Answer

How to turn a character frame independently? 1 Answer

Mathf.Lerp working in one direction but not the other 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