unity3d:dotween pingpong 物体来回运动

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class PingPong : MonoBehaviour {

    float m_time = 0.5f;
    Transform m_trans;
    // Use this for initialization
    void Start()
    {
        m_trans = transform;
        MyPingPong(m_trans.localPosition.y,m_trans.localPosition.y + 0.1f);
    }

    private void MyPingPong(float from, float to)
    {
        m_trans.DOLocalMoveY(to, m_time).OnComplete(() => MyPingPong(to, from));
    }

}

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
有多种方法可以在Unity中使物体来回移动,以下是其中的几种: 1. 使用Translate方法:在Update函数中使用Translate方法,将物体沿着指定方向移动,在到达一定距离后改变方向,如下所示: ```csharp public float speed = 1f; public float distance = 5f; private Vector3 direction = Vector3.right; private Vector3 startPosition; void Start() { startPosition = transform.position; } void Update() { transform.Translate(direction * speed * Time.deltaTime); float currentDistance = Vector3.Distance(transform.position, startPosition); if (currentDistance >= distance) { direction = -direction; } } ``` 2. 使用Lerp方法:使用Lerp方法在两个点之间进行插值,使物体在两个点之间来回移动,如下所示: ```csharp public Transform startPoint; public Transform endPoint; public float speed = 1f; private float t = 0f; void Update() { t += Time.deltaTime * speed; transform.position = Vector3.Lerp(startPoint.position, endPoint.position, Mathf.PingPong(t, 1f)); } ``` 3. 使用Animation组件:创建一个移动动画,然后在Animator中设置动画为循环,并使用Animator组件来控制动画播放速度,如下所示: - 创建一个移动动画,将物体移动到一定距离,然后再移回来。 - 将动画循环,并在Animation窗口中设置循环区间。 - 在Animator窗口中,为动画创建一个参数,用于控制动画播放速度。 - 在脚本中,使用Animator组件来控制动画播放速度,如下所示: ```csharp public Animator animator; public float speed = 1f; void Update() { animator.SetFloat("Speed", speed); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

四夕立羽

你的鼓励将是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值