_
This commit is contained in:
parent
bb69e55f8c
commit
0b08977787
102
Assets/Scripts/MovingPlatform.cs
Normal file
102
Assets/Scripts/MovingPlatform.cs
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
public class MovingPlatform : MonoBehaviour
|
||||||
|
{
|
||||||
|
|
||||||
|
public float width;
|
||||||
|
public float height;
|
||||||
|
[Range(0.1f, 8)] [SerializeField] private float speed = 1;
|
||||||
|
|
||||||
|
private bool movingLeft = false;
|
||||||
|
private bool movingUp = false;
|
||||||
|
|
||||||
|
Vector3 vector;
|
||||||
|
Vector3 object_position;
|
||||||
|
Vector3 furthest_position;
|
||||||
|
|
||||||
|
// Start is called before the first frame update
|
||||||
|
void Start()
|
||||||
|
{
|
||||||
|
vector = transform.position;
|
||||||
|
object_position = transform.position;
|
||||||
|
furthest_position = object_position;
|
||||||
|
furthest_position[0] += width;
|
||||||
|
furthest_position[1] += height;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update is called once per frame
|
||||||
|
void Update()
|
||||||
|
{
|
||||||
|
if (width > 0) {
|
||||||
|
if (movingLeft) {
|
||||||
|
if (vector[0] > object_position[0]) {
|
||||||
|
vector[0] -= speed * Time.deltaTime;
|
||||||
|
} else {
|
||||||
|
movingLeft = !movingLeft;
|
||||||
|
vector[0] += speed * Time.deltaTime;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (vector[0] < furthest_position[0]) {
|
||||||
|
vector[0] += speed * Time.deltaTime;
|
||||||
|
} else {
|
||||||
|
movingLeft = !movingLeft;
|
||||||
|
vector[0] -= speed * Time.deltaTime;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (width < 0) {
|
||||||
|
if (movingLeft) {
|
||||||
|
if (vector[0] > furthest_position[0]) {
|
||||||
|
vector[0] -= speed * Time.deltaTime;
|
||||||
|
} else {
|
||||||
|
movingLeft = !movingLeft;
|
||||||
|
vector[0] += speed * Time.deltaTime;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (vector[0] < object_position[0]) {
|
||||||
|
vector[0] += speed * Time.deltaTime;
|
||||||
|
} else {
|
||||||
|
movingLeft = !movingLeft;
|
||||||
|
vector[0] -= speed * Time.deltaTime;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (height > 0) {
|
||||||
|
if (movingUp) {
|
||||||
|
if (vector[1] < furthest_position[1]) {
|
||||||
|
vector[1] += speed * Time.deltaTime;
|
||||||
|
} else {
|
||||||
|
movingUp = !movingUp;
|
||||||
|
vector[1] -= speed * Time.deltaTime;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (vector[1] > object_position[1]) {
|
||||||
|
vector[1] -= speed * Time.deltaTime;
|
||||||
|
} else {
|
||||||
|
movingUp = !movingUp;
|
||||||
|
vector[1] += speed * Time.deltaTime;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (height < 0) {
|
||||||
|
if (movingUp) {
|
||||||
|
if (vector[1] < object_position[1]) {
|
||||||
|
vector[1] += speed * Time.deltaTime;
|
||||||
|
} else {
|
||||||
|
movingUp = !movingUp;
|
||||||
|
vector[1] -= speed * Time.deltaTime;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (vector[1] > furthest_position[1]) {
|
||||||
|
vector[1] -= speed * Time.deltaTime;
|
||||||
|
} else {
|
||||||
|
movingUp = !movingUp;
|
||||||
|
vector[1] += speed * Time.deltaTime;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
transform.position = vector;
|
||||||
|
}
|
||||||
|
}
|
11
Assets/Scripts/MovingPlatform.cs.meta
Normal file
11
Assets/Scripts/MovingPlatform.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 20c0ecf95ac68c343bdf7b8295ff699c
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
Loading…
Reference in New Issue
Block a user