I currently have a script which basically makes an object rotate once Space button is pressed. When it is pressed again, the object stops. Easy right?
How do I go on making it, for example, by pressing Space activate spinning to the speed from 0 to slowly to (for example) 1000 spins/minute. And when I let the space go, let the speed decrease back to 0?
Here's what I have so far:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class spin : MonoBehaviour
{
public float speed = 500f;
public Button starter;
public Button stopper;
bool isSpinning = false;
IEnumerator spinnerCoroutine;
void Start()
{
spinnerCoroutine = spinCOR();
}
void Update()
{
//Start if Space-key is pressed AND is not Spinning
if (Input.GetKeyDown(KeyCode.Space) && !isSpinning)
{
FidgetSpinnerStart();
}
//Stop if Space-key is pressed AND is already Spinning
else if (Input.GetKeyDown(KeyCode.Space) && isSpinning)
{
FidgetSpinnerStop();
}
}
IEnumerator spinCOR()
{
//Spin forever until FidgetSpinnerStop is called
while (true)
{
transform.Rotate(Vector3.up, speed * Time.deltaTime);
//Wait for the next frame
yield return null;
}
}
void FidgetSpinnerStart()
{
//Spin only if it is not spinning
if (!isSpinning)
{
isSpinning = true;
StartCoroutine(spinnerCoroutine);
}
}
void FidgetSpinnerStop()
{
//Stop Spinning only if it is already spinning
if (isSpinning)
{
StopCoroutine(spinnerCoroutine);
isSpinning = false;
}
}
}
Cheers!