At the top of script:
public Quaternion _lookRotation;
In Update:
void Update()
{
transform.rotation = Quaternion.Slerp(transform.rotation, _lookRotation,
Time.deltaTime * RotationSpeed);
}
I want to know when the transform rotation ended and is at _lookRotation Maybe using some flag ?
This is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;
public class FadeScript : MonoBehaviour
{
public UnityStandardAssets.ImageEffects.Blur blur;
public UnityStandardAssets.ImageEffects.BlurOptimized blurOptimized;
public FirstPersonController fpc;
public float fadeDuration = 5;
public float startAcceleration = 0.6f;
public float endAcceleration = 0.6f;
public float maxSpeed = 1.0f;
public float rotationSpeed = 0.6f;
private Material material;
private float targetAlpha = 0;
private float lerpParam;
private float startAlpha = 1;
private bool rotated = false;
private Quaternion clampedTargetRotation;
void Start()
{
material = GetComponent<Renderer>().material;
SetMaterialAlpha(1);
fpc.enabled = false;
}
void Update()
{
lerpParam += Time.deltaTime;
float alpha = Mathf.Lerp(startAlpha, targetAlpha, lerpParam / fadeDuration);
SetMaterialAlpha(alpha);
if (alpha == 0)
{
fpc.enabled = true;
if (rotated == false)
{
fpc.GetComponent<FirstPersonController>().enabled = false;
rotationSpeed += Mathf.Clamp(rotationSpeed + startAcceleration * Time.deltaTime, 0, maxSpeed);
}
if (fpc.transform.localRotation == Quaternion.Euler(0,0,0))
{
fpc.GetComponent<FirstPersonController>().enabled = true;
rotationSpeed = Mathf.Clamp(rotationSpeed - endAcceleration * Time.deltaTime, 0, maxSpeed);
blur.enabled = false;
blurOptimized.enabled = false;
rotated = true;
}
clampedTargetRotation = Quaternion.RotateTowards(fpc.transform.localRotation, Quaternion.Euler(0, 0, 0), rotationSpeed * Time.deltaTime);
fpc.transform.localRotation = clampedTargetRotation;
}
}
public void FadeTo(float alpha, float duration)
{
startAlpha = material.color.a;
targetAlpha = alpha;
fadeDuration = duration;
lerpParam = 0;
}
private void SetMaterialAlpha(float alpha)
{
Color color = material.color;
color.a = alpha;
material.color = color;
}
}
What i want in the rotation part is to start the rotation slowly and then accelerate a bit and then in the end when it's getting "close enough" to 0,0,0 so start accelerate down until complete stop. Complete stop not must be at 0,0,0 but close to it. Th way i'm doing it now is wrong i'm not using the t 0/1 solution and not courtine.
The rotation now in my script is almost working as i want. It start slowly and accelerate but in the end it stop at once without smoothly accelerate down.