UnityDOTween动画脚本控制播放与暂停。

本文介绍了如何在Unity3D中利用DOTween脚本控制动画的播放和暂停。通过点击蝴蝶实现其沿着预设路径运动并返回原点,蝴蝶回到起点后激活精灵动画,精灵缓缓降落。关键代码涉及到DOPause()和DOPlay()函数的应用。还提到了在实现过程中遇到的autoplay问题,将在后续博客中解决。

Unity3D DOTween 动画播放与暂停的脚本控制

 通过点击蝴蝶激活DOTweenPath使蝴蝶按照一定的轨迹运动后回到原来的位置,当蝴蝶回到原位置后,精灵动画激活,精灵从天而降。

蝴蝶点击事件,通过射线检测,点击蝴蝶。部分代码

  void Update() {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            Debug.Log("zzzzzzzzzzz");//检验程序执行
            if (Physics.Raycast(ray, out hit)) {
                if (hit.transform.gameObject.name == "butterfly") {
                    Debug.Log("yyyyyyyyyyyy");//检验程序执行

点击蝴蝶前DOTween动画不播放,点击后播放。部分脚本。

 
 void Start() {
butterfly.GetComponent<DOTweenPath>().DOPause();//动画不播放
}


void Update() {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            Debug.Log("zzzzzzzzzzz");
            if (Physics.Raycast(ray, out hit)) {
                if (hit.transform.gameObject.name == "butterfly") {
                    Debug.Log("yyyyyyyyyyyy");
                    butterfly.GetComponent<DOTweenPath>().DOPlay();动画播放
                    Debug.Log("qqqqqqqqq");

这里用到了DOTween中的DOPause()和DOPlay()两个方法。

点击蝴蝶动画播放延迟八秒后。精灵从天而降。依旧调用上面所用到的方法,完整代码。

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

public class B_Move : MonoBehaviour {
    public GameObject butterfly;
    public GameObject jingling1;
    public GameObject jingling2;
    public GameObject jingling3;

    // [AddComponentMenu("DOTween/DOTween Path")]
    // Use this for initialization
    void Start() {
        jingling1.GetComponent<DOTweenAnimation>().DOPause();
        jingling2.GetComponent<DOTweenAnimation>().DOPause();
        jingling3.GetComponent<DOTweenAnimation>().DOPause();

        butterfly.GetComponent<DOTweenPath>().DOPause();
        Debug.Log("ddddddddd");
    }

    // Update is called once per frame
    void Update() {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            Debug.Log("zzzzzzzzzzz");
            if (Physics.Raycast(ray, out hit)) {
                if (hit.transform.gameObject.name == "butterfly") {
                    Debug.Log("yyyyyyyyyyyy");
                    butterfly.GetComponent<DOTweenPath>().DOPlay();
                    Debug.Log("qqqqqqqqq");
                    StartCoroutine(Wait(8f));
                }
            }
        }
    }
  IEnumerator Wait(float t) {
        yield return new WaitForSeconds(t);
        jingling1.GetComponent<DOTweenAnimation>().DOPlay();
        jingling2.GetComponent<DOTweenAnimation>().DOPlay();
        jingling3.GetComponent<DOTweenAnimation>().DOPlay();

    }


      
        

        

  }
   

之前我尝试使用,autoplay=true和autoplay=false的方法能够检测到射线但动画并没有被调用。这是我所遇到的问题,下篇博客解决。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值