- Home /
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;
}
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:
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)
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;
}
Your answer
Follow this Question
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