-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovingPlatform.cs
More file actions
55 lines (46 loc) · 2.21 KB
/
MovingPlatform.cs
File metadata and controls
55 lines (46 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovingPlatform : MonoBehaviour
{
[SerializeField] private WaypointPath waypointPath; //objekt Parent u kojem se nalaze Child objekti waypointova
[SerializeField] float platformSpeed; //brzina platforme
private int nextWaypoint; //sledeci waypoint
private Transform preWaypoint; //pozicija prijasnjeg waypointa
private Transform targWaypoint; //pozicija trenutnog
private float timeToWaypoint; //vrijeme do waypointa --> udaljenost izmedu (trenutnog i prijasnjeg)/brzina
private float elapsedTime; //proteklo vrijeme
//dohvacanje ciljanog waypointa
void Start()
{
TargetWaypoint();
}
//micanje platforme od waypointa do waypointa
void FixedUpdate()
{
elapsedTime += Time.deltaTime;
float elpPer = elapsedTime / timeToWaypoint;
elpPer = Mathf.SmoothStep(0,1,elpPer);
transform.position = Vector3.Lerp(preWaypoint.position, targWaypoint.position, elpPer);
if(elpPer >=1){
TargetWaypoint();
}
}
//dohvacanje sljedeceg waypointa u nizu i izracunavanje vrijeme potrebno do njega
private void TargetWaypoint(){
preWaypoint = waypointPath.GetWaypoint(nextWaypoint);
nextWaypoint = waypointPath.GetNextWaypoint(nextWaypoint);
targWaypoint = waypointPath.GetWaypoint(nextWaypoint);
elapsedTime = 0;
float distance = Vector3.Distance(preWaypoint.position, targWaypoint.position);
timeToWaypoint = distance/platformSpeed;
}
//kad igrac stane na platformu koja se mice postane njezino dijete kako bi se mogao kretati s njome
private void OnTriggerEnter(Collider other) {
other.transform.SetParent(transform);
}
//kad igrac izadje iz prostora pokretne platforme, makne se iz hijerarhije platforme
private void OnTriggerExit(Collider other) {
other.transform.SetParent(null);
}
}