Train-of-Thought/Assets/Scripts/MovingPlatform.cs
Gerard Gascón 862afc9b7a init
2025-04-24 16:56:52 +02:00

42 lines
884 B
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovingPlatform : MonoBehaviour {
public Transform target;
public float speed;
public bool i;
private Vector3 start, end;
// Use this for initialization
void Start () {
if(target != null) {
target.parent = null;
start = transform.position;
end = target.position;
}
}
// Update is called once per frame
void Update () {
}
void FixedUpdate(){
if (target != null && !i) {
float fixedSpeed = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target.position, fixedSpeed);
}
if (transform.position == target.position){
target.position = (target.position == start) ? end : start;
}
}
private void OnCollisionEnter2D(Collision2D collision){
if(collision.gameObject.tag == "Player"){
i = false;
}
}
}