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的方法能够检测到射线但动画并没有被调用。这是我所遇到的问题,下篇博客解决。
本文介绍了如何在Unity3D中利用DOTween脚本控制动画的播放和暂停。通过点击蝴蝶实现其沿着预设路径运动并返回原点,蝴蝶回到起点后激活精灵动画,精灵缓缓降落。关键代码涉及到DOPause()和DOPlay()函数的应用。还提到了在实现过程中遇到的autoplay问题,将在后续博客中解决。

5776

被折叠的 条评论
为什么被折叠?



