IEnumerator instead of LateUpdate in unity
Hello, can anyone help to turn the function in void LateUpdate() into a IEnumerator Coroutine so transform.eulerAngles = currentAngle; stops being called once the camera reaches its destination.
I have a panning function on another script but it won't work properly because currentAngle keeps firing
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class designerCameraControl : MonoBehaviour { public Transform[] views; [Space(10)] public float transitionSpeed; private Transform currentView;
void FixedUpdate()
{
if (Input.GetMouseButtonDown(0))
{
Ray toMouse = Camera.main.ScreenPointToRay(Input.mousePosition);
bool didHit = Physics.Raycast(toMouse, out RaycastHit rhInfo, 100.0f);
if (rhInfo.collider.name == "Waypoint Handler")
{
{
currentView = views[1];
}
}
if (rhInfo.collider.name == "Waypoint Handler (1)")
{
{
currentView = views[2];
}
}
}
if (Input.GetKey(KeyCode.Escape))
{
{
currentView = views[0];
}
}
}
void LateUpdate()
{
//Lerp position
if (currentView == null) return;
transform.position = Vector3.Lerp(transform.position, currentView.position, Time.deltaTime * transitionSpeed);
Vector3 currentAngle = new Vector3
(
Mathf.LerpAngle(transform.rotation.eulerAngles.x, currentView.transform.rotation.eulerAngles.x, Time.deltaTime * transitionSpeed),
Mathf.LerpAngle(transform.rotation.eulerAngles.y, currentView.transform.rotation.eulerAngles.y, Time.deltaTime * transitionSpeed),
Mathf.LerpAngle(transform.rotation.eulerAngles.z, currentView.transform.rotation.eulerAngles.z, Time.deltaTime * transitionSpeed)
);
transform.eulerAngles = currentAngle;
}
}
Answer by Legacy7005 · Jul 05, 2019 at 06:41 PM
Hello again everyone, I was able to get it to do what i want by using IEnumerator but still a bit glitchy
Here my solution to my problem if anyone is insterested
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class designerCameraControl : MonoBehaviour { public Transform[] views; [Space(10)] public float transitionSpeed; private Transform currentView;
void FixedUpdate()
{
if (Input.GetMouseButtonDown(0))
{
Debug.Log("Keeps Firing");
// From screen to mouse position
Ray toMouse = Camera.main.ScreenPointToRay(Input.mousePosition);
// RaycastHit rhInfo: This gives all the information of the object such as location, etc.
// This checks if it hit or not
bool didHit = Physics.Raycast(toMouse, out RaycastHit rhInfo, 100.0f);
if (rhInfo.collider.name == "Waypoint Handler")
{
StartCoroutine(FaceTheIcon());
{
currentView = views[1]; ;
}
}
if (rhInfo.collider.name == "Waypoint Handler (1)")
{
StartCoroutine(FaceTheIcon());
{
currentView = views[2];
}
}
}
if (Input.GetKey(KeyCode.Escape))
{
StartCoroutine(FaceTheIcon());
{
currentView = views[0];
}
}
}
void LateUpdate()
{
}
IEnumerator FaceTheIcon()
{
//Lerp position
// if (currentView == null) return;
while (transform.position != currentView.position)
{
transform.position = Vector3.Lerp(transform.position, currentView.position, Time.deltaTime * transitionSpeed);
Vector3 currentAngle = new Vector3
(
Mathf.LerpAngle(transform.rotation.eulerAngles.x, currentView.transform.rotation.eulerAngles.x, Time.deltaTime * transitionSpeed),
Mathf.LerpAngle(transform.rotation.eulerAngles.y, currentView.transform.rotation.eulerAngles.y, Time.deltaTime * transitionSpeed),
Mathf.LerpAngle(transform.rotation.eulerAngles.z, currentView.transform.rotation.eulerAngles.z, Time.deltaTime * transitionSpeed)
);
transform.eulerAngles = currentAngle;
Debug.Log("IEnumerator Script is running");
yield return null;
}
}
}