Unity 3D漂浮物(转载)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
/// <summary>
/// 物品浮动效果
/// </summary>
public class TestItemFloat : MonoBehaviour
{
    //漂浮幅度
    public float speed = 1f;
    //漂浮范围X,Y,Z
    public Vector3 offsetXYZ = new Vector3(1, 1, 1);
    //原点
    private Vector3 originalPos;
 
    void Start()
    {
        if (speed <= 0)
        {
            speed = 1f;
        }
        //获取原点
        originalPos = transform.localPosition;
    }
 
    void Update()
    {
        //根据三角函数以时间为变化量模拟浮动效果(看个人喜好如何偏移但是乘上的值必须要在[-1,1]合理范围内变化)
        transform.localPosition = originalPos + new Vector3(offsetXYZ.x * Mathf.Sin(Time.time * speed), offsetXYZ.y * Mathf.Cos(Time.time * speed) * Mathf.Sin(Time.time * speed), offsetXYZ.z * Mathf.Cos(Time.time * speed));
    }
}

https://blog.csdn.net/qq_39574690/article/details/89792664?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164559640616780269886131%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=164559640616780269886131&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~rank_v31_ecpm-1-89792664.nonecase&utm_term=%E6%BC%82%E6%B5%AE%E7%89%A9&spm=1018.2226.3001.4450

posted on 2022-02-23 14:08  zqiang0803  阅读(161)  评论(0编辑  收藏  举报

导航