In the top of the script:
public FirstPersonController fpc;
public float speed;
In the Update
void Update()
{
fpc.transform.localRotation = Quaternion.Slerp(fpc.transform.rotation,
Quaternion.Euler(0, 0, 0), speed * Time.deltaTime);
}
The problem is that the rotation start immediately. And i want it to start slowly smooth then it will get some speed and then near the end to slowly smooth again until stop.
Update:
This is the complete script for now:
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;
private Material material;
private float targetAlpha = 0;
private float lerpParam;
private float startAlpha = 1;
private bool rotated = false;
private float rotationSpeed;
private Quaternion clampedTargetRotation;
private bool start;
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);
rotationSpeed = Mathf.Clamp(rotationSpeed - endAcceleration * Time.deltaTime, 0, maxSpeed);
clampedTargetRotation = Quaternion.RotateTowards(fpc.transform.localRotation, whateverRotationTokeepGoing, rotationSpeed * Time.deltaTime);
fpc.transform.localRotation = clampedTargetRotation;
}
if (fpc.transform.localRotation == Quaternion.Euler(0,0,0))
{
fpc.GetComponent<FirstPersonController>().enabled = true;
blur.enabled = false;
blurOptimized.enabled = false;
rotated = true;
}
}
}
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;
}
}
My problem is in the rotation part:
if (rotated == false)
{
fpc.GetComponent<FirstPersonController>().enabled = false;
rotationSpeed = Mathf.Clamp(rotationSpeed + startAcceleration * Time.deltaTime, 0, maxSpeed);
rotationSpeed = Mathf.Clamp(rotationSpeed - endAcceleration * Time.deltaTime, 0, maxSpeed);
clampedTargetRotation = Quaternion.RotateTowards(fpc.transform.localRotation, whateverRotationTokeepGoing, rotationSpeed * Time.deltaTime);
fpc.transform.localRotation = clampedTargetRotation;
}
The rotation should start from the current fpc rotation state and end at 0,0,0 This is where the rotation should be end when fpc is 0,0,0
if (fpc.transform.localRotation == Quaternion.Euler(0,0,0))
If fpc rotation is at 0,0,0 then stop. Between the start and stop make the whole rotation operation.