Moving platform script

This commit is contained in:
OverflowNarhoym 2022-02-25 10:36:45 +01:00
parent f9776a81be
commit 545d0aa965
2 changed files with 182 additions and 10 deletions

View File

@ -2383,6 +2383,154 @@ Transform:
m_CorrespondingSourceObject: {fileID: 7008207192594766311, guid: 2ed6bfce9ad3e19428cb9bef743e0fa3, type: 3}
m_PrefabInstance: {fileID: 7008207193554258683}
m_PrefabAsset: {fileID: 0}
--- !u!1 &1139056417
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1139056422}
- component: {fileID: 1139056421}
- component: {fileID: 1139056420}
- component: {fileID: 1139056419}
- component: {fileID: 1139056418}
m_Layer: 0
m_Name: Square
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1139056418
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1139056417}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 1637c8c06cf9fae4eb7bfa1a0ebb0e55, type: 3}
m_Name:
m_EditorClassIdentifier:
xOffset: 10
yOffset: 0
--- !u!61 &1139056419
BoxCollider2D:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1139056417}
m_Enabled: 1
m_Density: 1
m_Material: {fileID: 0}
m_IsTrigger: 0
m_UsedByEffector: 0
m_UsedByComposite: 0
m_Offset: {x: 0, y: 0}
m_SpriteTilingProperty:
border: {x: 0, y: 0, z: 0, w: 0}
pivot: {x: 0.5, y: 0.5}
oldSize: {x: 1, y: 1}
newSize: {x: 1, y: 1}
adaptiveTilingThreshold: 0.5
drawMode: 0
adaptiveTiling: 0
m_AutoTiling: 0
serializedVersion: 2
m_Size: {x: 1, y: 1}
m_EdgeRadius: 0
--- !u!50 &1139056420
Rigidbody2D:
serializedVersion: 4
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1139056417}
m_BodyType: 2
m_Simulated: 1
m_UseFullKinematicContacts: 0
m_UseAutoMass: 0
m_Mass: 1
m_LinearDrag: 0
m_AngularDrag: 0.05
m_GravityScale: 1
m_Material: {fileID: 0}
m_Interpolate: 0
m_SleepingMode: 1
m_CollisionDetection: 0
m_Constraints: 0
--- !u!212 &1139056421
SpriteRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1139056417}
m_Enabled: 1
m_CastShadows: 0
m_ReceiveShadows: 0
m_DynamicOccludee: 1
m_StaticShadowCaster: 0
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RayTracingMode: 0
m_RayTraceProcedural: 0
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 10754, guid: 0000000000000000f000000000000000, type: 0}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_ReceiveGI: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 1
m_SelectedEditorRenderState: 0
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_Sprite: {fileID: 7482667652216324306, guid: 311925a002f4447b3a28927169b83ea6, type: 3}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_FlipX: 0
m_FlipY: 0
m_DrawMode: 0
m_Size: {x: 1, y: 1}
m_AdaptiveModeThreshold: 0.5
m_SpriteTileMode: 0
m_WasSpriteAssigned: 1
m_MaskInteraction: 0
m_SpriteSortPoint: 0
--- !u!4 &1139056422
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1139056417}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 10, y: -2.57, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 8
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1524344315
GameObject:
m_ObjectHideFlags: 0

View File

@ -1,3 +1,4 @@
using System;
using Movement;
using UnityEngine;
@ -5,27 +6,39 @@ namespace Platforms
{
public class MovingPlatform : MonoBehaviour, IMovement
{
public float xOffeset;
public float xOffset;
public float yOffset;
private Vector3 _originalPos;
private Vector3 _futurePos;
private const float VerificationOffset = 0.04f;
private bool _goingToFuturePos;
private Rigidbody2D _platformRigidbody;
private Vector3 _velocity = Vector3.zero;
private void Awake()
{
_platformRigidbody = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
private void Start()
{
_originalPos = transform.position;
_futurePos = new Vector3(_originalPos.x + xOffset, _originalPos.y + yOffset, _originalPos.z);
_goingToFuturePos = true;
}
private void Update()
{
if (!IsPaused)
Move(BaseSpeed);
}
// PAUSING METHODS
public bool IsPaused { get; private set; }
public void Pause()
{
IsPaused = true;
@ -37,17 +50,28 @@ namespace Platforms
IsPaused = false;
_platformRigidbody.simulated = !IsPaused;
}
// MOVEMENT METHODS
public float BaseSpeed { get; set; }
public void Move(float value)
{
var position = transform.position;
var targetPosition = new Vector3(position.x + xOffeset, position.y + yOffset, 0.0f);
position = Vector3.SmoothDamp(position, targetPosition, ref _velocity, 1.0f, 1.0f);
transform.position = position;
switch (_goingToFuturePos)
{
case true:
transform.position += new Vector3(0.05f, 0, 0);
if (Math.Abs(_futurePos.x - transform.position.x) < VerificationOffset &&
Math.Abs(_futurePos.y - transform.position.y) < VerificationOffset)
_goingToFuturePos = false;
break;
case false:
transform.position += new Vector3(-0.05f, 0, 0);
if (Math.Abs(_originalPos.x - transform.position.x) < VerificationOffset &&
Math.Abs(_originalPos.y - transform.position.y) < VerificationOffset)
_goingToFuturePos = true;
break;
}
}
}
}