The question is answered, right answer was accepted
Smooth camera movement and lookAt [solved]
Got stucked with camera movement in my Unity3D C# project.
What i have:
some objects on the scene a camera, which would fly from any object's poition or from it's own current position What i need:
smooth rotare camera to one of the object's origin fly to the spot near the object (there is an empty, so i fly to empty's coords) Algorithm: rotare to the object's origin -> when rotation finishes, start to fly to the empty's position. while flying, look at object's origin.
The problem is that it's not smooth, the camera "jumps" at the end of the movement.
My code: c#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class testMove : MonoBehaviour
{
public GameObject startObj;
public GameObject endObj;
public float speed = 1.0F;
private float startTime;
private float journeyLength;
private string endObjName;
private GameObject endObjLookAt;
void Start()
{
startTime = Time.time;
if (startObj)
{
}
else
{
startObj = this.gameObject;
}
journeyLength = Vector3.Distance(startObj.transform.position, endObj.transform.position);
endObjName = endObj.name;
endObjLookAt = GameObject.Find(endObjName + "LookAt");
}
void Update()
{
if (endObj)
{
float distCovered = (Time.time - startTime) * speed;
float fracJourney = distCovered / journeyLength;
tweenLook(endObjLookAt, fracJourney);
float angle = Quaternion.Angle(transform.rotation, Quaternion.LookRotation(endObjLookAt.transform.position - transform.position));
if (angle <= 0.0001)
{
Debug.Log("rotation finished");
tweenPos(startObj, endObj, fracJourney);
transform.LookAt(endObjLookAt.transform.position);
}
}
}
private void tweenPos(GameObject startObj, GameObject endObj, float fracJourney)
{
Vector3 newposition = Vector3.Lerp(startObj.transform.position, endObj.transform.position, fracJourney);
transform.position = newposition;
}
private void tweenLook(GameObject endObjLookAt, float fracJourney)
{
Quaternion newrotation = Quaternion.LookRotation(endObjLookAt.transform.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, newrotation, fracJourney);
}
}
Many thanks to all of you, guys in advance! My c# code (attached to the camera):
Solved!
public class test$$anonymous$$ove : $$anonymous$$onoBehaviour { public Transform startObj; public Transform endObj; private Transform endObjLookAt;
public float rotationDuration;
public AnimationCurve rotationCurve;
public float movementDuration;
public AnimationCurve movementCurve;
private IEnumerator moveAndRotateCameraIEnumerator;
void Start()
{
// If you want to do it on start just call $$anonymous$$oveAndRotateCamera() here, else call if from anywhere you want (a script, a game button, ...)
}
void Update()
{
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
{
$$anonymous$$oveAndRotateCamera();
}
}
public void $$anonymous$$oveAndRotateCamera(Transform startTransform = null, Transform endTransform = null)
{
if(startTransform)
{
startObj = startTransform;
}
else
{
startObj = this.transform;
}
if(endTransform)
{
endObj = endTransform;
}
endObjLookAt = GameObject.Find(endObj.name + "LookAt").transform;
if(moveAndRotateCameraIEnumerator != null)
{
StopCoroutine(moveAndRotateCameraIEnumerator);
}
moveAndRotateCameraIEnumerator = $$anonymous$$oveAndRotateCameraCoroutine();
StartCoroutine(moveAndRotateCameraIEnumerator);
}
private IEnumerator $$anonymous$$oveAndRotateCameraCoroutine()
{
//ROTATION
Vector3 startEulerAngles = transform.eulerAngles;
transform.LookAt(endObjLookAt);
Vector3 deltaEulerAngles = new Vector3($$anonymous$$athf.DeltaAngle(startEulerAngles.x, transform.eulerAngles.x), $$anonymous$$athf.DeltaAngle(startEulerAngles.y, transform.eulerAngles.y), $$anonymous$$athf.DeltaAngle(startEulerAngles.z, transform.eulerAngles.z));
Debug.Log("Starting rotation...");
float timer = 0.0f;
while(timer < rotationDuration)
{
timer += Time.deltaTime;
transform.eulerAngles = startEulerAngles + deltaEulerAngles * rotationCurve.Evaluate(timer / rotationDuration);
yield return new WaitForEndOfFrame();
}
transform.eulerAngles = startEulerAngles + deltaEulerAngles;
Debug.Log("Rotation done!");
//----
//$$anonymous$$OVE$$anonymous$$ENT
Vector3 startPosition = transform.position;
Debug.Log("Starting movement...");
timer = 0.0f;
while(timer < movementDuration)
{
timer += Time.deltaTime;
transform.position = Vector3.Lerp(startPosition, endObj.position, movementCurve.Evaluate(timer / movementDuration));
transform.LookAt(endObjLookAt);
yield return new WaitForEndOfFrame();
}
transform.position = endObj.position;
transform.LookAt(endObjLookAt);
Debug.Log("$$anonymous$$ovement done!");
//----
}
}
Follow this Question
Related Questions
3d camera not working as I want it to 0 Answers
Sit Down First person 0 Answers
Maximum Camera Angle Rotation 0 Answers
Third person look around camera, jagged results 0 Answers
Make camera follow and look at object at the same time 0 Answers