0

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!

papi
  • 379
  • 7
  • 25
  • 1
    I think you should put effort into doing this before posting. It's not hard to do. Use `Input.GetKeyDown` to detect when the button is down the `Input.GetKeyUp` to detect when the button is released. As for the increment/decrement, use coroutine. See the method [here](https://stackoverflow.com/questions/45736799/decrease-variable-over-a-specific-amount-of-time/45737410#45737410). – Programmer Aug 21 '17 at 22:37

1 Answers1

0

Change:

public float speed = 500f;

to:

public float speed = 0f;
public float maxspeed = 500f;
public float acceleration = 1f;

In Update, add:

if (isSpinning)
{
    if (speed < maxspeed)
    {
        speed += acceleration;
    }
    if (speed > maxspeed)
    {
        speed = maxspeed;
    }
}
else
{
    if (speed > 0)
    {
        speed -= acceleration;
    }
    if (speed < 0)
    {
        speed = 0;
    }
}

Increase the value of acceleration to make your spinner reach maximum speed faster.

0liveradam8
  • 752
  • 4
  • 18
  • That didn't work really. I mean, at least not as I explained how I wanted it. But instead, it goes from 0 to set speed just when Space pressed once. I want it so people have to hold Space button in order to increase speed and once they will let it to, the speed will decrease back to 0. – papi Aug 21 '17 at 22:44
  • This is because in your code you've detected the first and second time the spacebar is pressed. You're going to have to check for when it is released to stop the fidget spinner. – 0liveradam8 Aug 21 '17 at 22:56
  • And probaby acceleration is too fast for you to detect. With 1f, to reach 500 from 0 is quite fast. – Thalthanas Aug 22 '17 at 06:07