1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

unityでアイテムをプカプカ浮遊させる方法

Posted at

参考動画

無題の動画 ‐ Clipchampで作成 (1) (1).gif

コード

    transform.Rotate(Vector3.up,rotation_speed*Time.deltaTime);//回転
        
    float updown = startposition.y + Mathf.Sin(Time.time * updown_speed) * 0.5f;//上下の動き
        
    transform.position=new Vector3(transform.position.x,updown , transform.position.z);

startposition.yはオブジェクトの開始地点のy座標を表している

Sin関数の動き

例えば、Mathf.Sin(Time.time)を実行したとき1から-1の間を繰り返す動きになる

スクリーンショット 2025-03-02 181503.png

sin関数を使うことで波のような動きになるので滑らかな動きを再現できる

sin関数の制御

Mathf.Sin(Time.time * updown_speed) * 0.5f

updown_speedは上下の動作速度を表している

0.5fをかけることで可動域を0.5から-0.5に可動域を設定できる

まとめ

アニメーションのように滑らかな動きを作りたいときに使える!

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?