今回は、オブジェクトをふわふわ浮かせる方法を紹介していきたいと思います。
準備
まずは、アセットストアで「iTween」というフリーアセットをダウンロードします。
リンクはこちら↓

iTween | Animation Tools | Unity Asset Store
Use the iTween tool from Pixelplacement on your next project. Find this & more animation tools on the Unity Asset Store.
この「iTween」を使うことでスクリプトでオブジェクトに簡単なアニメーションをさせることができます。
ダウンロードができたら、インポートしましょう。
そしてふわふわさせたいオブジェクトを用意します。
今回、僕が使うのは、この敵です。

これで準備は完了です。
ふわふわさせるスクリプト
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Updown : MonoBehaviour
{
float amplitude =0.01f; // 振幅
void FixedUpdate()
{
UpDown();
}
void UpDown()
{
float T = 3.0f;
float F = 1.0f / T;
// 上下に振動させる
float posYSin = Mathf.Sin(2.0f * Mathf.PI * F * Time.time);
iTween.MoveAdd(gameObject, new Vector3(0,amplitude*posYSin, 0),0.0f);
}
}
このスクリプトをふわふわさせたいオブジェクトにアタッチしましょう。

これで実行をしますと

こんな風になめらかに上下してふわふわを表現することができました!
スクリプトの参考にしたサイト

UnityのiTweenを試してみる その1 Move系 - Qiita
初めまして、株式会社ZOOA(ゾーア)のzobと申します。弊社はフリーランスや、個人事業主、給料UPを目指している方向けに高単価求人案件配信サイト(ZOB)も運営しておりますの…

【Unity】Sin関数の使い方 - Qiita
概要高校生の時は**「三角関数とか何の役に立つんですか~?」側の生徒でした。後になって学びなおして苦労しました。sin cosをちゃんと理解するには「回る」という事は具体的にどういう事なのか?*…
まとめ
itweenを使えば、長かったコードが短くなってとっても便利です。
コメント