-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovingPlatform.cs
More file actions
36 lines (29 loc) · 984 Bytes
/
Copy pathMovingPlatform.cs
File metadata and controls
36 lines (29 loc) · 984 Bytes
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
using UnityEngine;
public class MovingPlatform : MonoBehaviour
{
public float moveSpeed = 2.0f;
public float moveDistance = 5.0f;
private Vector3 initialPosition;
private bool movingRight = true;
void Start()
{
initialPosition = transform.position;
}
void Update()
{
MovePlatform();
}
void MovePlatform()
{
// Calculate the new position based on the direction and speed
Vector3 newPosition = movingRight ? initialPosition + Vector3.right * moveDistance : initialPosition - Vector3.right * moveDistance;
// Move the platform towards the new position
transform.Translate((newPosition - transform.position).normalized * moveSpeed * Time.deltaTime);
// Check if the platform has reached the target position
if (Vector3.Distance(transform.position, newPosition) < 0.1f)
{
// Change direction
movingRight = !movingRight;
}
}
}