56 lines
1.2 KiB
C#
56 lines
1.2 KiB
C#
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using UnityEngine;
|
|||
|
|
|||
|
public class BounceUpAndDown : MonoBehaviour
|
|||
|
{
|
|||
|
public float yMin = 0.9f;
|
|||
|
public float yMax = 1.2f;
|
|||
|
private bool goingDown;
|
|||
|
private bool bounce = false;
|
|||
|
|
|||
|
private void OnEnable()
|
|||
|
{
|
|||
|
PassTheEdgeTrigger.OnPassingTheEdge += StartBouncing;
|
|||
|
}
|
|||
|
|
|||
|
private void OnDisable()
|
|||
|
{
|
|||
|
PassTheEdgeTrigger.OnPassingTheEdge -= StartBouncing;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
void Start()
|
|||
|
{
|
|||
|
goingDown = true;
|
|||
|
}
|
|||
|
|
|||
|
void Update()
|
|||
|
{
|
|||
|
if (bounce)
|
|||
|
{
|
|||
|
if (goingDown && transform.position.y > yMin)
|
|||
|
{
|
|||
|
transform.Translate(Vector3.down * Time.deltaTime, Space.World);
|
|||
|
}
|
|||
|
if (transform.position.y <= yMin)
|
|||
|
{
|
|||
|
goingDown = false;
|
|||
|
}
|
|||
|
if (!goingDown && transform.position.y < yMax)
|
|||
|
{
|
|||
|
transform.Translate(Vector3.up * Time.deltaTime, Space.World);
|
|||
|
}
|
|||
|
if (transform.position.y >= yMax)
|
|||
|
{
|
|||
|
goingDown = true;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void StartBouncing()
|
|||
|
{
|
|||
|
bounce = true;
|
|||
|
}
|
|||
|
}
|