Unity仿iOS桌面图标抖动效果

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ESharkByAxisZ : MonoBehaviour
{
    [Tooltip("Z轴抖动幅度,以Z轴为中心向两侧等角度抖动")]
    public int sharkAngle = 10;

    bool isShark = false;

    Vector3 angle = new Vector3(0, 0, 0);
    int halfAngle;

    [Tooltip("抖动步进比例,比例基数为抖动幅度")]
    public float sharkStep = 0.5f;

    int dir = 1;

    public void Open()
    {
        isShark = true;
        halfAngle = sharkAngle / 2;
        angle = transform.localEulerAngles;
    }

    public void Close()
    {
        isShark = false;
        transform.localRotation = Quaternion.identity;
    }

    void Update()
    {
        if (!isShark)
            return;

        angle.z += dir * sharkStep * halfAngle;
        if (angle.z >= halfAngle)
            dir = -1;
        else if (angle.z <= -halfAngle)
            dir = 1;
        transform.localEulerAngles = angle;
    }
}

你可能感兴趣的:(Unity仿iOS桌面图标抖动效果)