holopy3/Assets/Scripts/BounceUpAndDown.cs

56 lines
1.2 KiB
C#
Raw Normal View History

2021-01-28 12:07:52 +00:00
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;
}
}