26 lines
764 B
C#
26 lines
764 B
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class ObjectMovement : MonoBehaviour
|
|
{
|
|
public float moveSpeed = 2f; // Kecepatan gerakan objek
|
|
public float verticalRange = 2f; // Rentang gerakan vertikal objek
|
|
public Vector2 startingPosition; // Posisi awal objek
|
|
|
|
private float targetY;
|
|
|
|
void Start()
|
|
{
|
|
targetY = startingPosition.y;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
float t = Mathf.PingPong(Time.time * moveSpeed, verticalRange) / verticalRange;
|
|
float newY = Mathf.Lerp(startingPosition.y - verticalRange / 2, startingPosition.y + verticalRange / 2, t);
|
|
Vector2 newPosition = new Vector2(transform.position.x, newY);
|
|
transform.position = newPosition;
|
|
}
|
|
}
|