67 lines
2.0 KiB
C#
67 lines
2.0 KiB
C#
|
|
using System.Collections;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using UnityEngine;
|
||
|
|
|
||
|
|
|
||
|
|
public class JumpEffect : MonoBehaviour
|
||
|
|
{
|
||
|
|
public float jumpHeight = 50f; // 跳跃的高度
|
||
|
|
public float jumpDuration = 0.5f; // 每次跳跃的持续时间
|
||
|
|
public float jumpInterval = 2f; // 跳跃的间隔时间(秒)
|
||
|
|
|
||
|
|
private Vector3 originalPosition; // 初始位置
|
||
|
|
|
||
|
|
void Start()
|
||
|
|
{
|
||
|
|
// 记录图片的原始位置
|
||
|
|
originalPosition = transform.position;
|
||
|
|
|
||
|
|
// 启动一个协程,定时让图片跳跃
|
||
|
|
StartCoroutine(JumpCoroutine());
|
||
|
|
}
|
||
|
|
|
||
|
|
// 定义一个协程,每隔一段时间让图片跳跃
|
||
|
|
private IEnumerator JumpCoroutine()
|
||
|
|
{
|
||
|
|
while (true)
|
||
|
|
{
|
||
|
|
// 让图片向上跳跃
|
||
|
|
yield return StartCoroutine(JumpToPosition(originalPosition + Vector3.up * jumpHeight));
|
||
|
|
|
||
|
|
// 等待一定时间再执行下一次跳跃
|
||
|
|
yield return new WaitForSeconds(jumpInterval);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 定义一个协程,让图片平滑地移动到指定位置
|
||
|
|
private IEnumerator JumpToPosition(Vector3 targetPosition)
|
||
|
|
{
|
||
|
|
float timeElapsed = 0f;
|
||
|
|
Vector3 startPos = transform.position;
|
||
|
|
|
||
|
|
// 上升部分
|
||
|
|
while (timeElapsed < jumpDuration)
|
||
|
|
{
|
||
|
|
transform.position = Vector3.Lerp(startPos, targetPosition, timeElapsed / jumpDuration);
|
||
|
|
timeElapsed += Time.deltaTime;
|
||
|
|
yield return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 确保图片最终到达目标位置
|
||
|
|
transform.position = targetPosition;
|
||
|
|
|
||
|
|
// 下降部分
|
||
|
|
timeElapsed = 0f;
|
||
|
|
Vector3 startReturnPos = transform.position;
|
||
|
|
while (timeElapsed < jumpDuration)
|
||
|
|
{
|
||
|
|
transform.position = Vector3.Lerp(startReturnPos, originalPosition, timeElapsed / jumpDuration);
|
||
|
|
timeElapsed += Time.deltaTime;
|
||
|
|
yield return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 最终回到原始位置
|
||
|
|
transform.position = originalPosition;
|
||
|
|
}
|
||
|
|
}
|