PingPong缓动

作者:追风剑情 发布于:2019-5-30 10:46 分类:Unity3d

示例

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// PingPong缓动
/// </summary>
public class TweenPingPong : MonoBehaviour
{
    public Vector3 startPosition;
    public Vector3 endPosition;
    public float speed = 1;
    private float length = 1;
    private float t = 0;
    private Transform mTransform;

    private void Awake()
    {
        mTransform = transform;
        length = Vector3.Distance(startPosition, endPosition);
    }

    void Update()
    {
        t += speed * Time.deltaTime;
        //让返回值在0到length之间往返
        float p = Mathf.PingPong(t, length);
        Vector3 pos = Vector3.Lerp(endPosition, startPosition, p);
        mTransform.localPosition = pos;
    }
}

运行效果

2222.gif

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号