0
\$\begingroup\$
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Rotations : MonoBehaviour
{
    public Text textName;
    public float rotationSpeed;
    public bool rotateText = false;
    public string textToDisplay;
    public float displayCurrentSpeed;

    public float rotationDuration;
    public float startAngle;
    public float stopAngle;
    public AnimationCurve curve;

    private float currentRotationTime;

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

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

        if(rotateText)
        {
            /*currentRotationTime += Time.deltaTime;
            var progress = curve.Evaluate(currentRotationTime / rotationDuration);
            var currentAngle = startAngle + (stopAngle - startAngle) * progress;

            textName.transform.rotation = Quaternion.Euler(0f, currentAngle, 0f);
            */

            if (currentRotationTime < 3f)
            {
                currentRotationTime += Time.deltaTime;
                textName.transform.Rotate(0, rotationSpeed * currentRotationTime, 0);
            }
        }
    }
}

Using the animationcurve is working fine but I want to make that the speed will increase slowly to maximum speed and that it will keep rotating on that maximum speed.

The problems is how to limit the rotation speed to maximum ? and how to display the current rotation speed to see the rotation speed get increasing ?

Doing :

if (currentRotationTime < 3f)

Is not limiting the maximum rotation speed.

\$\endgroup\$
2
  • \$\begingroup\$ Does your element has a rightbody? \$\endgroup\$
    – Zibelas
    Jan 17, 2022 at 18:13
  • \$\begingroup\$ @Zibelas no rigidbody. \$\endgroup\$
    – Daniel Lip
    Jan 17, 2022 at 18:15

1 Answer 1

-1
\$\begingroup\$

This seems to be working as I wanted. Though I wonder if I did it right and if there is a way to get to exactly speed 3.

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

public class Rotations : MonoBehaviour
{
    public Text textName;
    public float rotationSpeed;
    public bool rotateText = false;
    public string textToDisplay;
    public float currentSpeed;

    public float rotationDuration;
    public float startAngle;
    public float stopAngle;
    public AnimationCurve curve;

    private float currentRotationTime;

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

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

        if(rotateText)
        {
            if ((rotationSpeed * currentRotationTime) < 3f)
            {
                currentRotationTime += Time.deltaTime;
                textName.transform.Rotate(0, rotationSpeed * currentRotationTime, 0);
            }
            else
            {
                textName.transform.Rotate(0, rotationSpeed * currentRotationTime, 0);
            }

            currentSpeed = rotationSpeed * currentRotationTime;
        }
    }
}

Edit

Tried to remove the same rotation but then it will not keep rotating when it's reaching 3. I don't want it to stop when reaching to 3 but to keep rotating nonsotp at the reached speed 3.

currentSpeed = rotationSpeed * currentRotationTime;

            if (currentSpeed < 3f)
            {
                currentRotationTime += Time.deltaTime;
                textName.transform.Rotate(0, rotationSpeed * currentRotationTime, 0);
            }
\$\endgroup\$
3
  • \$\begingroup\$ You are applying the same rotation in the codition -> can be after the whole condition. And you would save a multiplication if you check in the condition against currentSpeed. \$\endgroup\$
    – Zibelas
    Jan 17, 2022 at 19:00
  • \$\begingroup\$ @Zibelas I didn't understand but edited my answer. if I don't make the same rotation in both places when it's reaching 3 it will stop rotating. I want it to keep rotating at the reached speed 3 that's why I'm using rotation in both places. what am I missing ? \$\endgroup\$
    – Daniel Lip
    Jan 17, 2022 at 21:32
  • \$\begingroup\$ if ((rotationSpeed * currentRotationTime) < 3f){currentRotationTime += Time.deltaTime;}textName.transform.Rotate(0, rotationSpeed * currentRotationTime, 0); \$\endgroup\$
    – Zibelas
    Jan 18, 2022 at 8:24

You must log in to answer this question.

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