0
\$\begingroup\$

Now when I set the rotateText flag to true the object will rotate on the y and then when I set the rotateText flag to false the rotation will stop and the object will rotate back to it's original rotation.

but I want to add to it maybe using animationcurve and lerp or some other way ease in out for the rotation. so if the flag rotateText is true it will start slowly smooth to rotate to max rotation speed and when the flag is false it will slowly smooth rotate down to speed 0 until stop.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MyRotationTests : MonoBehaviour
{
    public Text textName;
    public float rotationSpeed;
    public bool rotateText = false;
    public string textToDisplay;
    
    private Quaternion originalRotationValue;

    private void Start()
    {
        textName.text = textToDisplay;
        originalRotationValue = textName.transform.rotation;
    }

    private void Update()
    {
        Vector3 namePos = Camera.main.WorldToScreenPoint(this.transform.position);
        textName.transform.position = namePos;

        if(rotateText)
        {
            textName.transform.Rotate(Vector3.up, rotationSpeed * Time.deltaTime);
        }
        else
        {
            textName.transform.rotation = Quaternion.Slerp(textName.transform.rotation,
                originalRotationValue,
                Time.deltaTime * rotationSpeed);
        }
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ I created an example for rotating an object using an animation curve here. Does that solve your problem? \$\endgroup\$
    – Philipp
    Jan 17 at 8:50

1 Answer 1

0
\$\begingroup\$

This is empirical formula for easing in and out.

\$1-sin(\frac{\pi}{2}(1-t))\$

\$sin(\frac{\pi t}{2})\$

\$t\in[0,1]\$

Example code:

float time, easeDuration;

private void Update()
{
    const float HALF_PI = Mathf.PI / 2;

    time += Time.deltaTime;
    var t = Mathf.Clamp01(time / easeDuration);

    if(rotateText)
        t = 1 - Mathf.Sin((1-t) * HALF_PI); // ease in
    else
        t = Mathf.Sin(t * HALF_PI); // ease out

    float realRotationSpeed = rotationSpeed * t;
    // Use realRotationSpeed instead of rotationSpeed
}
\$\endgroup\$

You must log in to answer this question.

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