我想利用dotween;让我的对话框从下向上凹出,请问该怎么写代码内容:你可以使用DoTween的DOMove方法来实现对话框从下向上凹出的效果。以下是一个示例代码:

c#
using UnityEngine;
using DG.Tweening;

public class DialogueBox : MonoBehaviour
{
 private bool hasTriggered = false;
 [SerializeField] private bool isEntered;
 public GameObject notelogueBox;
 public Transform dialogueBoxTransform;
 public float moveDuration = 1f;
 public Vector3 startPoint;
 public Vector3 endPoint;

 private void OnTriggerEnter2D(Collider2D other)
 {
 if (other.CompareTag("Player"))
 {
 isEntered = true;
 }
 }

 private void OnTriggerExit2D(Collider2D other)
 {
 isEntered = false;
 }

 private void Start()
 {
 startPoint = dialogueBoxTransform.position;
 endPoint = dialogueBoxTransform.position + Vector3.up * 10f; // 设置对话框的目标位置
 }

 private void Update()
 {
 if (!hasTriggered)
 {
 if (isEntered)
 {
 hasTriggered = true;
 ShowDialogueBox();
 }
 }
 }

 private void ShowDialogueBox()
 {
 notelogueBox.SetActive(true);
 dialogueBoxTransform.DOMove(endPoint, moveDuration).SetEase(Ease.OutBack); // 使用DoTween的DOMove方法实现对话框的动画效果

 if (Input.GetKeyDown(KeyCode.F))
 {
 HideDialogueBox();
 }
 }

 private void HideDialogueBox()
 {
 dialogueBoxTransform.DOMove(startPoint, moveDuration).SetEase(Ease.InBack).OnComplete(() =>
 {
 notelogueBox.SetActive(false);
 });
 }
}


在这个示例代码中,我们使用DoTween的DOMove方法来实现对话框的动画效果。在Start方法中,我们设置了对话框的初始位置(startPoint)和目标位置(endPoint)。在ShowDialogueBox方法中,我们使用DOMove方法将对话框移动到目标位置,并在按下F键时调用HideDialogueBox方法隐藏对话框,同时将对话框移动回初始位置。


原文地址: https://www.cveoy.top/t/topic/pL9Q 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录