본문 바로가기
개발/C#

유니티 C# 오브젝트 둥둥 떠다니는 효과 주기 간단 구현 UFO

by SPNK 2024. 2. 22.
반응형

코드 작성

using UnityEngine;
using System.Collections;

public class Floating : MonoBehaviour
{
    
    public float RottationSpeed = 15.0f;
    public float Bounciness = 0.5f;
    public float Frequency = 1f;

    
    Vector3 posOffset = new Vector3();
    Vector3 tempPos = new Vector3();

    
    void Start()
    {     
        posOffset = transform.position;
    }

    
    void Update()
    {
        
        transform.Rotate(new Vector3(0f, Time.deltaTime * RottationSpeed, 0f), Space.World);

        tempPos = posOffset;
        tempPos.y += Mathf.Sin(Time.fixedTime * Mathf.PI * Frequency) * Bounciness;

        transform.position = tempPos;
    }
}
반응형

댓글