1
0
forked from cgvr/DeltaVR
Files
DeltaVR3DModelGeneration/Assets/_PROJECT/Scripts/ModeGeneration/PushableButton.cs

78 lines
1.9 KiB
C#

using DG.Tweening;
using UnityEngine;
public class PushableButton : MonoBehaviour
{
public delegate void OnButtonPressedDelegate();
public event OnButtonPressedDelegate OnButtonPressed;
public bool startDown;
public Transform movableParts;
public float moveDuration = 0.25f;
public Transform wire;
public Material wireActiveMaterial;
public Material wireInactiveMaterial;
private float upPositionY;
private float downPositionY;
private bool isButtonDown;
private void Awake()
{
upPositionY = movableParts.localPosition.y;
downPositionY = movableParts.localPosition.y - 0.1f;
isButtonDown = false;
}
// Start is called before the first frame update
void Start()
{
if (startDown)
{
// Dont call Activate, we dont want to change wire material here
movableParts.DOLocalMoveY(downPositionY, moveDuration);
isButtonDown = true;
}
}
// Update is called once per frame
void Update()
{
}
private void OnCollisionEnter(Collision collision)
{
if (!isButtonDown && collision.gameObject.tag.EndsWith("Hand"))
{
Activate();
OnButtonPressed?.Invoke();
}
}
private void Activate()
{
movableParts.DOLocalMoveY(downPositionY, moveDuration);
isButtonDown = true;
foreach (MeshRenderer meshRenderer in wire.GetComponentsInChildren<MeshRenderer>())
{
meshRenderer.material = wireActiveMaterial;
}
}
public void Deactivate()
{
movableParts.DOLocalMoveY(upPositionY, moveDuration);
isButtonDown = false;
foreach (MeshRenderer meshRenderer in wire.GetComponentsInChildren<MeshRenderer>())
{
meshRenderer.material = wireInactiveMaterial;
}
}
}