SinをUnityでつかえると、意外と一気にビジュアル的ゲームのクオリティをあげられることに気づいたので、ここに例を挙げておこうとおもいます。
泡がゆらゆらと昇っていく様子
まずひとつ目。泡を等速度上昇させながらxまたはz軸方向に等速で往復させるという手もある。しかし、sin関数を使うことで、より滑らかで泡らしいうごきをするようになるのです。
以下スクリプト
bubblesin.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class bubblesin : MonoBehaviour {
private float bubble_x;
private float bubble_y;
private float bubble_z;
private float time;
private int posxrandom;
// Use this for initialization
void Start () {
time = 0;
posxrandom = Random.Range (-3, 4);
}
// Update is called once per frame
void Update () {
time += Time.deltaTime;
bubble_x = Mathf.Sin (time) * 0.5f + posxrandom;
bubble_y = time * 0.7f;
bubble_z = this.transform.position.z;
this.transform.position = new Vector3 (bubble_x, bubble_y, bubble_z);
}
}
このスクリプトをゆらゆらさせたい泡のゲームオブジェクトにひっつければよい。
フレームを点滅させる。
まず、「点滅させる」とは、その不透明度を変化させるということである。
redFlash.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class redFlash : MonoBehaviour {
private float speed = 1.0f;
//private Text text;
private Image image;
private float time;
//RGBの各値とα値の各変数を宣言する。
float alfa;
float red, green, blue;
void Update () {
//点滅させたいオブジェクトのImageの色を手に入れる
GetComponent<Image>().color = new Color(red, green, blue, alfa);
//点滅させたいオブジェクトのRGB値を代入する
red = 145;
green = 5;
blue = 30;
time += Time.deltaTime *5.0f * speed;
//α値にSin関数を代入する
alfa = Mathf.Sin(time) * 0.5f + 0.5f;
}
}
こちらのスクリプトは、点滅させたいゲームオブジェクトに直接アタッチする必要はない。
是非これからは、一次関数だけでなく、三角関数を上手く使用して、より良いステージを作りあげましょう!