Unity——API中Mathf的插值函数,关于对象的移动

8 篇文章 1 订阅

1. Lerp

 从a点->b点->a点来回循环移动

例:从(-1,0,0)到(1,0,0)

using UnityEngine;

public class Lerp : MonoBehaviour
{
    public float minimum = -1.0f;
    public float maximum = 1.0f;
    float t = 0.0f;
    void Update()
    {
        transform.position = new Vector3(Mathf.Lerp(minimum, maximum, t), transform.position.y, transform.position.z);
        t += 0.5f * Time.deltaTime;
        if (t > 1.0f)
        {
            float temp = minimum;
            minimum = maximum;
            maximum = temp;
            t = 0.0f;
        }
    }
}

2. PingPong

从a点->b点->a点来回循环移动

例:从(-1,0,0)到(1,0,0)

using UnityEngine;

public class PingPong : MonoBehaviour
{
    void Update()
    {
        transform.position = new Vector3(-1 + Mathf.PingPong(Time.time, 2), transform.position.y, transform.position.z);
    }
}

3. Repeat

每次都是从a点->b点,循环移动

例:从从(-1,0,0)到(1,0,0)

void Update()
    {
        transform.position = new Vector3(-1 + Mathf.Repeat(Time.time, 2), transform.position.y, transform.position.z);
    }

4. LerpAngle

例:以y轴为旋转中心从0°旋转到90°

    public float minAngle = 0.0f;
    public float maxAngle = 90.0f;
    void Update()
    {
        float angle = Mathf.LerpAngle(minAngle, maxAngle, Time.time);
        transform.eulerAngles = new Vector3(0, angle, 0);
    }

5. MoveTowards

例:血量条

    public float currStrength;//当前值
    public float maxStrength;//最大值
    public float recoveryRate;//恢复速度
    private Slider slider;
    void Start()
    {
        slider = GetComponent<Slider>();
    }
    void Update()
    {
        currStrength = Mathf.MoveTowards(currStrength, maxStrength, recoveryRate * Time.deltaTime);
        slider.value = currStrength;
    }

6. MoveTowardsAngle

例:旋转对象到指定角度

    public float target = 270.0f;
    public float speed = 45.0f;

    void Update()
    {
        float angle = Mathf.MoveTowardsAngle(transform.eulerAngle.z, target, speed * Time.deltaTime);
        transform.eulerAngles = new Vector3(0, 0, angle);
    }

7. SmoothDame

使对象到达目标点慢慢减速运动(阻尼效果);平滑跟踪对象

    public Transform target;
    public float smoothTime = 0.3f;//移动到目标点耗时
    private float yVelocity = 0.0f;
    void Update()
    {
        float newPosition = Mathf.SmoothDamp(transform.position.y, target.position.y, ref yVelocity, smoothTime);
        transform.position = new Vector3(transform.position.x, newPosition, transform.position.z);
    }

8. SmoothDampAngle

相机平滑跟随追踪对象,该脚本需挂相机上

    public Transform target;
    public float smoothTime = 3.0f;//跟随耗时
    public float distance = 10.0f;//相机与跟随对象的距离
    private float yVelocity = 0.0f;
    void Update()
    {
        //计算旋转到目标角度的平滑值
        float yAngle = Mathf.SmoothDampAngle(transform.eulerAngles.y, target.eulerAngles.y, ref yVelocity, smoothTime);
        Vector3 position = target.position;
        position += Quaternion.Euler(0, yAngle, 0) * new Vector3(0, 0, -distance);
        transform.position = position;
        transform.LookAt(target);
    }

9. SmoothStep

SmoothStep的用法和Lerp相同,只是SmoothStep是匀减速移向目标点的。

例:动态加载Panel的淡入淡出

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

public class SmoothStep : MonoBehaviour
{
    public float minimum = 0.0f;
    public float maximum = 1.0f;
    public float duration = 2.0f;
    private float startTime;
    private Image image;
    void Start()
    {
        startTime = Time.time;
        image = GetComponent<Image>();
    }
    void Update()
    {
        float t = (Time.time - startTime) / duration;
        Color c = image.color;
        c.a = Mathf.SmoothStep(minimum, maximum, t);
        image.color = c;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值