leomovskii

CoinAnim.cs

Oct 28th, 2021 (edited)
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.03 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CoinAnim : MonoBehaviour {
  6.  
  7.     public float speed = 2f; // швидкість руху монетки
  8.     public bool direction = true; // напрямок руху
  9.  
  10.     float yMin; // початкова координата
  11.  
  12.     void Start() {
  13.         yMin = transform.position.y; // ініціалізація початкової координати
  14.     }
  15.  
  16.     void FixedUpdate() {
  17.         if (transform.position.y > yMin + 1) { // якщо монетка вийшла за верхню межу
  18.             direction = false; // змінюємо напрямок на ВНИЗ
  19.         } else if (transform.position.y < yMin) { // якщо монетка вийшла за нижню межу
  20.             direction = true; // змінюємо напрямок на ВГОРУ
  21.         }
  22.         float delta = speed * (direction ? 1 : -1) * Time.deltaTime; // шаг анімації
  23.        
  24.         transform.position += Vector3.up * delta; // зрушуємо платформу
  25.     }
  26. }
Add Comment
Please, Sign In to add comment