mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-06-26 19:34:47 +00:00
40 lines
1.1 KiB
C#
40 lines
1.1 KiB
C#
using UnityEngine;
|
|
|
|
namespace HPJ.Examples
|
|
{
|
|
/// <summary>
|
|
/// An Example Script to ping pong an object between 2 points
|
|
/// </summary>
|
|
public class ObstaclePingPong : MonoBehaviour
|
|
{
|
|
public Transform startPoint;
|
|
public Transform endPoint;
|
|
public float speed = 5.0f;
|
|
[Range(0, 1)]
|
|
public float startingPercent = 0;
|
|
public bool ReverseDirection = false;
|
|
|
|
private float pingPongTime = 0.0f;
|
|
|
|
private void Start()
|
|
{
|
|
if (ReverseDirection)
|
|
{
|
|
startingPercent = 2 - startingPercent;
|
|
}
|
|
pingPongTime = startingPercent;
|
|
}
|
|
|
|
private void OnValidate()
|
|
{
|
|
transform.position = Vector3.Lerp(startPoint.position, endPoint.position, startingPercent);
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
pingPongTime += Time.deltaTime * speed;
|
|
float pingPongValue = Mathf.PingPong(pingPongTime, 1.0f);
|
|
transform.position = Vector3.Lerp(startPoint.position, endPoint.position, pingPongValue);
|
|
}
|
|
}
|
|
} |