0
\$\begingroup\$

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.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Just add an angular acceleration to your angular speed, the starting acceleration and the ending acceleration can be different:

void Update()
{
    float speed;
    Quaternion clampedTargetRotation;

    if(start)
    {
       speed = Mathf.Clamp(speed + startAcceleration * Time.deltaTime, 0, maxSpeed);
    }
    else
    {
       speed = Mathf.Clamp(speed - endAcceleration* Time.deltaTime, 0, maxSpeed);

    }

       clampedTargetRotation = Quaternion.RotateTowards(fpc.transform.localRotation, whateverRotationTokeepGoing, speed * Time.deltaTime);
       fpc.transform.localRotation = clampedTargetRotation;
}

N.B. this is just a "sketch" of solution

\$\endgroup\$
3
  • \$\begingroup\$ I just edited now my question added the complete script i'm using for now and i'm trying to use your solution too. My problem is where to use the start bool i mean how to decide when it start and when ending ? I know when i want it to stop rotating when the fpc rotation is at 0,0,0 then i also enable other scripts so i want it to stop the rotation at this point and start from it's current rotation state. \$\endgroup\$
    – Daniel Lip
    Nov 7, 2017 at 11:21
  • \$\begingroup\$ Also i'm not sure what should be the whateverRotationTokeepGoing \$\endgroup\$
    – Daniel Lip
    Nov 7, 2017 at 11:22
  • \$\begingroup\$ Do you want to rotate toward a fixed rotation or do you want to just spin on an axis? \$\endgroup\$
    – Alakanu
    Nov 7, 2017 at 17:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .