add train moving logic
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
using System.Linq;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using DG.Tweening;
|
||||
using UnityEngine;
|
||||
|
||||
public enum TrainType
|
||||
{
|
||||
EP2D,
|
||||
Ivolga2,
|
||||
Ivolga3
|
||||
}
|
||||
|
||||
public class LocomotionHandler : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Train[] trains;
|
||||
[SerializeField] private Material[] materials;
|
||||
[SerializeField] private float effectDuration;
|
||||
|
||||
public float trainSpeed;
|
||||
public float pullUpPositionBefore;
|
||||
public float pullUpPositionAfter;
|
||||
|
||||
public readonly float pullUpPosition = -12;
|
||||
private Coroutine c_effect;
|
||||
|
||||
void Update()
|
||||
{
|
||||
if(Input.GetKeyDown(KeyCode.Alpha1))
|
||||
ShowTrain(TrainType.EP2D);
|
||||
|
||||
if(Input.GetKeyDown(KeyCode.Alpha2))
|
||||
ShowTrain(TrainType.Ivolga2);
|
||||
|
||||
if(Input.GetKeyDown(KeyCode.Alpha3))
|
||||
ShowTrain(TrainType.Ivolga3);
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
foreach(var train in trains)
|
||||
{
|
||||
train.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
foreach(var material in materials)
|
||||
{
|
||||
material.SetFloat("_EffectPosition", 5);
|
||||
}
|
||||
}
|
||||
|
||||
public void ShowTrain(TrainType trainType)
|
||||
{
|
||||
var activeTrain = trains.FirstOrDefault(x => x.gameObject.activeSelf);
|
||||
if(activeTrain != null)
|
||||
activeTrain.Hide();
|
||||
|
||||
trains.First(x => x.type == trainType)
|
||||
.gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
// void SetEffect(bool isShow)
|
||||
// {
|
||||
// if(c_effect != null)
|
||||
// {
|
||||
// StopCoroutine(c_effect);
|
||||
// c_effect = null;
|
||||
// }
|
||||
|
||||
// c_effect = StartCoroutine(SetEffectRoutine(isShow));
|
||||
// }
|
||||
|
||||
IEnumerator SetEffectRoutine(bool isShow)
|
||||
{
|
||||
float startPosition = materials[0].GetFloat("_EffectPosition");
|
||||
float currentValue = startPosition;
|
||||
float endPosition = 0;
|
||||
|
||||
if(isShow) endPosition = 5;
|
||||
else endPosition = -5;
|
||||
|
||||
float progress = 0;
|
||||
float duration = effectDuration;;
|
||||
|
||||
while(progress < 1)
|
||||
{
|
||||
currentValue = Mathf.Lerp(startPosition, endPosition, progress);
|
||||
|
||||
for(int i = 0; i < materials.Length; i++)
|
||||
{
|
||||
materials[i]
|
||||
.SetFloat("_EffectPosition", currentValue);
|
||||
}
|
||||
|
||||
progress += duration * Time.deltaTime;
|
||||
yield return null;
|
||||
}
|
||||
|
||||
for(int i = 0; i < materials.Length; i++)
|
||||
{
|
||||
materials[i]
|
||||
.SetFloat("_EffectPosition", endPosition);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6f9218c03aa8e5342b69e3cb669e38a1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,37 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using DG.Tweening;
|
||||
using UnityEngine;
|
||||
|
||||
public class Train : MonoBehaviour
|
||||
{
|
||||
public TrainType type;
|
||||
private LocomotionHandler locomotionHandler;
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
if(locomotionHandler == null)
|
||||
locomotionHandler = FindObjectOfType<LocomotionHandler>();
|
||||
|
||||
var startPosition = locomotionHandler.pullUpPositionBefore;
|
||||
var speed = locomotionHandler.trainSpeed;
|
||||
|
||||
transform.localPosition = new Vector3(transform.localPosition.x,
|
||||
transform.localPosition.y,
|
||||
startPosition);
|
||||
|
||||
transform.DOLocalMoveZ(0, speed)
|
||||
.SetEase(Ease.OutSine);
|
||||
}
|
||||
|
||||
public void Hide()
|
||||
{
|
||||
var endPosition = locomotionHandler.pullUpPositionAfter;
|
||||
var speed = locomotionHandler.trainSpeed;
|
||||
|
||||
transform.DOLocalMoveZ(endPosition, speed)
|
||||
.SetEase(Ease.InSine)
|
||||
.OnComplete(() => gameObject.SetActive(false));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ccec552147968e249a80635b7d589c37
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user