Unity DoTween 实现对话框从画面外向上弹出的效果
使用 DoTween 实现对话框从画面外向上弹出
要使用 DoTween 实现对话框从画面外向上弹出的效果,你可以按照以下步骤进行操作:
-
在代码中导入 DoTween 库,确保你已经安装了 DoTween 插件。
-
在对话框的初始位置下方设置一个隐藏的目标位置,该位置是对话框弹出后的最终位置。
-
在
Start()函数中,将对话框的初始位置设置为目标位置的下方,确保对话框开始时不可见。 -
在
Update()函数中,当触发对话框弹出条件时,使用 DoTween 进行动画过渡效果。
以下是修改后的代码示例:
using UnityEngine;
using DG.Tweening;
public class DialogueTrigger : MonoBehaviour
{
private bool hasTriggered = false;
[SerializeField] private bool isEntered;
public GameObject notelogueBox;
private Vector3 targetPosition; // 目标位置
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag('Player'))
{
isEntered = true;
}
}
private void OnTriggerExit2D(Collider2D other)
{
isEntered = false;
}
void Start()
{
targetPosition = notelogueBox.transform.position + new Vector3(0, 5, 0); // 设置目标位置
notelogueBox.transform.position = targetPosition + new Vector3(0, -5, 0); // 初始位置在目标位置下方
}
void Update()
{
if (!hasTriggered)
{
if (isEntered)
{
hasTriggered = true;
notelogueBox.SetActive(true);
if (Input.GetKeyDown(KeyCode.F))
{
notelogueBox.SetActive(false);
}
// 使用DoTween实现动画过渡效果
notelogueBox.transform.DOMoveY(targetPosition.y, 1f).SetEase(Ease.OutBounce);
}
}
}
}
在上述代码中,我们使用 DoTween 的 DOMoveY() 方法实现对话框的位置从初始位置向目标位置移动的动画效果。在设置动画时,我们使用了 SetEase() 方法来指定动画的缓动效果,这里使用了 OutBounce 效果来实现弹出的效果。
原文地址: https://www.cveoy.top/t/topic/pMbT 著作权归作者所有。请勿转载和采集!