(Unity)加速・減速するプロペラの作成(Clampの活用)

(スクリプト)

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

public class Propeller : MonoBehaviour
{
    private float velocity;

    void Update()
    {
        // 回転速度に制限を加える。
        velocity = Mathf.Clamp(velocity, 0f, 1500f);

        print(velocity);

        if(Input.GetKey(KeyCode.Tab))
        {
            velocity += 1.2f;
        }
        else
        {
            velocity -= 1.2f;
        }

        transform.Rotate(new Vector3(0, velocity, 0) * Time.deltaTime);
    }
}

(実行結果)

  • tabキーを押し続けると「加速」
  • キーから指を離すと「減速」すれば成功です。