Hello,
I am working on a hyper-casual app and right now I have a rotating circle game object. It rotates by clicking a button but when the button is released it slows down to a certain speed and stays there. I am wondering how would I go about when the button is released to have the game object slow down to a complete stop over time.
Here is how the code is set up right now.
void Update()
{
if (_rotate)
{
transform.Rotate(0, 0, 75 * Time.deltaTime);
}
else if (!_rotate)
{
transform.Rotate(0, 0, 30 * Time.deltaTime);
}
}
Any suggestions would be great
Thanks!
One possible way would be to use rigidbody physics. Instead of changing Rotate directly, use AddTorque() method. And on rigidbody you would need to set Angular drag to be at least 1 in order for that rotation to slow down
`
Rigidbody rb;
// Set negative to rotate to the opposite side
float rotationForce = 5.0f
void Start()
{
rb = GetComponen();
}
void FixedUpdate()
{
if (_rotate)
{
rb.AddTorque(rotationForce);
}
}
`
What you could do is set a rotation speed and reduce it when you want to slow it down;
private float _rotationSpeed;
private const float _maxRotationSpeed = 75f;
private const float _rotationSpeedDecrease = 0.5f;
private bool _rotate;
//Set this instead of _rotate
public bool Rotate
{
get { return _rotate; }
set
{
_rotationSpeed = _maxRotationSpeed;
_rotate = value;
}
}
private void Update()
{
if (_rotate)
{
transform.Rotate(0, 0, _rotationSpeed * Time.deltaTime);
}
else if (!_rotate)
{
if (_rotationSpeed >= 0)
{
_rotationSpeed -= _rotationSpeedDecrease * Time.deltaTime;
transform.Rotate(0, 0, _rotationSpeed * Time.deltaTime);
}
}
}