Hi there,
I have the situation where i can rotate an object based on start en stop. There is no specific angle (from - to) in the rotation. When i start or stop the rotation it is ‘direct/ hard’. I want to start en stop the rotation smoothly. I can find information about this, but it is all with a specific angle.
the rotation starts when a object becomes active and stops when the object goes in a inactive state.
Can someone help me out here or give me a direction to start.
Thanks.
Greet, Anne
Control the rotation base on an amount, and on starting, gradually increase the amount from 0 to max amount, and on stopping gradually decrease the amount from max amount to 0.
But the thing is, the “amount” I’m referring to would be the angle per frame, and you said you’re not looking for anything to do with the angle.
My question is how are you rotating the object without specifying an angle?
Hi IronLionZion,
This is the code i’m using:
transform.Rotate (Vector3.up, speed * Time.deltaTime); print("Object is rotating");
Greet, Anne
All versions of transform.Rotate require an angle in some form or another. In your example, the angle is specified by speed * Time.deltaTime. You could try the following and modify it to suit your needs:
public float maxSpeed = 1;
public float increaseRate = 2f;
private float currentSpeed = 0;
private bool increasing = true;
// reset rotation speed in order to start increasing every time the game object gets activated
void OnEnable()
{
currentSpeed = 0;
increasing = true;
}
void Update()
{
if(Input.GetKeyDown(Keycode.R) // press R to toggle rotation increase/decrease
{
increasing = !increasing;
}
// increase or decrease the current speed depending on the value of increasing
currentSpeed = Mathf.Clamp(currentSpeed + Time.deltaTime * increaseRate * (increasing? 1 : -1), 0, maxSpeed);
transform.Rotate(Vector3.up, currentSpeed * Time.deltaTime);
// OPTIONAL - disable after slowing to a stop
// you'll have to re-enable this game object from another script
//if(currentSpeed == 0 && !increasing)
//{
// gameObject.SetActive(false);
//}
}
1 Like
Hé, thanks! That was really helpful.
Got already working in my situation.
Greet, Anne