Made the elevator button-functional. Needs some fine-tuning and audio work.

This commit is contained in:
2025-11-30 22:05:25 +02:00
parent bcf7e3121d
commit dab5bfc677
12 changed files with 483 additions and 74 deletions

View File

@@ -4,12 +4,20 @@ using UnityEngine;
public class ElevatorCaller : MonoBehaviour
{
// Start is called before the first frame update
private void OnTriggerEnter(Collider other)
{
SpringyButtonPhysics button = other.GetComponent<SpringyButtonPhysics>();
//Debug.Log(other);
ElevatorSpringyButton button = other.GetComponent<ElevatorSpringyButton>();
if (button == null) return;
else button.Pressed();
else
{
button.Pressed();
}
}
void Start()
{

View File

@@ -7,9 +7,9 @@ using UnityEditor;
using UnityEngine;
public enum ElevatorState
{
Stationary,
OpenDoorsStationary,
CloseDoorsStationary,
OpeningDoors,
AwaitingPassengers,
ClosingDoors,
Moving
}
@@ -59,10 +59,10 @@ public class ElevatorBox : NetworkBehaviour
public AudioClip[] closeDoorsClips;
public AudioClip[] moveElevatorClips;
private int currentFloor = 2;
private int targetFloor = 2;
public int currentFloor = 2;
public int targetFloor = 2;
private ElevatorState state = ElevatorState.Stationary;
public ElevatorState state = ElevatorState.CloseDoorsStationary;
void Awake()
{
@@ -88,7 +88,7 @@ public class ElevatorBox : NetworkBehaviour
{
//Debug.Log("Calling Elevator");
Debug.Log(state);
if (state == ElevatorState.Stationary || state == ElevatorState.ClosingDoors)
if (state == ElevatorState.OpenDoorsStationary || state == ElevatorState.CloseDoorsStationary || state == ElevatorState.ClosingDoors)
{
Debug.Log(floor);
@@ -103,13 +103,13 @@ public class ElevatorBox : NetworkBehaviour
StartCoroutine(OpenDoors());
SetState(ElevatorState.AwaitingPassengers);
SetState(ElevatorState.OpenDoorsStationary);
}
}
public void interestExpired()
{
if (state == ElevatorState.AwaitingPassengers)
if (state == ElevatorState.OpenDoorsStationary)
{
if (IsElevatorEmpty())
{
@@ -130,12 +130,13 @@ public class ElevatorBox : NetworkBehaviour
}
private void OnTriggerEnter(Collider other)
{
if (other.GetComponentInParent<SmartHandPresence>() != null) return; // So it ignores player hands.
XROrigin enteredPlayerVR = other.GetComponent<XROrigin>();
KbmController enteredPlayerKbm = other.GetComponent<KbmController>();
XRPlayerMirror networkPlayerDisplay = other.GetComponentInParent<XRPlayerMirror>();
Debug.Log("Player Entered box");
//Debug.Log("Player Entered box");
if (enteredPlayerVR != null)
{
addChild(enteredPlayerVR);
@@ -150,11 +151,11 @@ public class ElevatorBox : NetworkBehaviour
}
else {
Debug.Log(other + " Cannot ride in lifts.");
//Debug.Log(other + " Cannot ride in lifts.");
return;
}
Debug.Log("Calling Lift transfer sequence");
/*Debug.Log("Calling Lift transfer sequence");
switch (currentFloor)
{
@@ -170,7 +171,7 @@ public class ElevatorBox : NetworkBehaviour
break;
}
StartCoroutine(LiftTransferSequence());
StartCoroutine(LiftTransferSequence()); */
}
@@ -199,7 +200,7 @@ public class ElevatorBox : NetworkBehaviour
Debug.Log("Player exited box, state is: " + this.state);
if (this.state == ElevatorState.AwaitingPassengers)
if (this.state == ElevatorState.OpenDoorsStationary)
{
if (IsElevatorEmpty())
{
@@ -278,27 +279,30 @@ public class ElevatorBox : NetworkBehaviour
audioSource.Play();
}
private IEnumerator LiftTransferSequence()
public IEnumerator LiftTransferSequence()
{
Debug.Log("Started Lidt transfer sequence");
if (state != ElevatorState.Moving);
Debug.Log("Started Lift transfer sequence");
yield return StartCoroutine(CloseDoors());
yield return StartCoroutine(MoveToFloor(targetFloor));
}
public IEnumerator MoveToFloor(int floorNumber)
{
Debug.Log("Moving to floor " + floorNumber);
if (floorDict.TryGetValue(floorNumber, out Transform target))
{
yield return StartCoroutine(MoveElevator(target.position));
SetCurrentFloor(targetFloor);
}
else
{
Debug.LogWarning($"No floor defined for number {floorNumber}");
}
if (state == ElevatorState.CloseDoorsStationary) {
//Debug.Log("Moving to floor " + floorNumber);
if (floorDict.TryGetValue(floorNumber, out Transform target))
{
yield return StartCoroutine(MoveElevator(target.position));
SetCurrentFloor(targetFloor);
}
else
{
Debug.LogWarning($"No floor defined for number {floorNumber}");
}
state = ElevatorState.CloseDoorsStationary;
yield return StartCoroutine(OpenDoors());
yield return StartCoroutine(OpenDoors());
}
}
private IEnumerator CloseAndStationary()
{
@@ -307,12 +311,14 @@ public class ElevatorBox : NetworkBehaviour
yield return StartCoroutine(CloseDoors());
// After doors have closed, set state
SetState(ElevatorState.Stationary);
SetState(ElevatorState.CloseDoorsStationary);
}
private IEnumerator CloseDoors()
public IEnumerator CloseDoors()
{
//if (state == ElevatorState.AwaitingPassengers) {
Debug.Log(state);
if (state != ElevatorState.OpeningDoors && state != ElevatorState.CloseDoorsStationary && state != ElevatorState.ClosingDoors)
{
Debug.Log("Closing doors");
if (Vector3.Distance(leftDoor.transform.position, leftDoorClosedPos.position) < 0.01f)
@@ -328,14 +334,15 @@ public class ElevatorBox : NetworkBehaviour
if (currentCaller != null) currentCaller.CloseDoors();
yield return StartCoroutine(MoveDoors(leftDoor, leftDoorClosedPos, rightDoor, rightDoorClosedPos, doorCloseTime));
//}
SetState(ElevatorState.CloseDoorsStationary);
}
}
private IEnumerator OpenDoors()
public IEnumerator OpenDoors()
{
if (state != ElevatorState.AwaitingPassengers) {
//Debug.Log("Opening doors");
if (state != ElevatorState.OpeningDoors && state != ElevatorState.OpenDoorsStationary && state != ElevatorState.Moving) {
Debug.Log("Opening doors");
SetState(ElevatorState.OpeningDoors);
playRandomAudioClipFrom(openDoorsClips);
@@ -349,7 +356,7 @@ public class ElevatorBox : NetworkBehaviour
yield return StartCoroutine(MoveDoors(leftDoor, leftDoorOpenPos, rightDoor, rightDoorOpenPos, doorOpenTime));
SetState(ElevatorState.AwaitingPassengers);
SetState(ElevatorState.OpenDoorsStationary);
yield return new WaitForSeconds(doorStayOpenTime); // wait for passengers
}
}
@@ -376,30 +383,31 @@ public class ElevatorBox : NetworkBehaviour
private IEnumerator MoveElevator(Vector3 targetPos)
{
if (targetFloor > currentFloor)
foreach (ElevatorOuter caller in callers)
caller.statusPlate.SetMoveState(ElevatorMoveState.Ascending);
if (targetFloor != currentFloor) {
if (targetFloor > currentFloor)
foreach (ElevatorOuter caller in callers)
caller.statusPlate.SetMoveState(ElevatorMoveState.Ascending);
else
foreach (ElevatorOuter caller in callers)
caller.statusPlate.SetMoveState(ElevatorMoveState.Decending);
else
foreach (ElevatorOuter caller in callers)
caller.statusPlate.SetMoveState(ElevatorMoveState.Decending);
Vector3 startPos = transform.position;
float t = 0;
Vector3 startPos = transform.position;
float t = 0;
SetState(ElevatorState.Moving);
playRandomAudioClipFrom(moveElevatorClips);
SetState(ElevatorState.Moving);
playRandomAudioClipFrom(moveElevatorClips);
AudioManager.Instance.PlayAttachedInstance(FMODEvents.Instance.ElevatorMovement, gameObject); //fmod attached 3d oneshot event
AudioManager.Instance.PlayAttachedInstance(FMODEvents.Instance.ElevatorMovement, gameObject); //fmod attached 3d oneshot event
while (t < 1f)
{
t += Time.deltaTime / floorMoveTime;
float easedT = Mathf.SmoothStep(0f, 1f, t); // ease in/out
transform.position = Vector3.Lerp(startPos, targetPos, easedT);
yield return null;
while (t < 1f)
{
t += Time.deltaTime / floorMoveTime;
float easedT = Mathf.SmoothStep(0f, 1f, t); // ease in/out
transform.position = Vector3.Lerp(startPos, targetPos, easedT);
yield return null;
}
}
foreach (ElevatorOuter caller in callers)
caller.statusPlate.SetMoveState(ElevatorMoveState.NotMoving);
}

View File

@@ -45,12 +45,9 @@ public class ElevatorOuter : NetworkBehaviour
}
}
private void OnTriggerEnter(Collider other)
public void CallElevator()
{
Debug.Log("Something entered call area");
if (other.GetComponentInParent<XRPlayerMirror>() == null) return;
StartCoroutine(box.callElevator(floor));
Debug.Log("Player entered call area");
}
private void OnTriggerExit(Collider other)

View File

@@ -1,9 +1,13 @@
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class SpringyButtonPhysics : MonoBehaviour {
public class ElevatorSpringyButton : MonoBehaviour {
public Transform buttonDirectionRoot;
public ElevatorOuter ElevatorOuter;
public ElevatorBox box;
[Header("Movement limits")]
public float minX = 0f;
public float maxX = 0f;
public float minY = 0f;
@@ -11,14 +15,31 @@ public class SpringyButtonPhysics : MonoBehaviour {
public float minZ = 0.005f;
public float maxZ = 0f;
[Header("Visuals")]
public Renderer[] targetRenderers;
public Material defaultMaterial;
public Material pressedMaterial;
[Header("Spring Settings")]
public float springForce = 300f; // how strong it returns
public float damping = 8f; // prevents vibration
[Header("Button Type Settings")]
public bool isFloorButton;
public bool isInsideButton;
public bool isCloseDoorsButton;
public int designatedFloor;
private Rigidbody rb;
private Vector3 restLocalPos;
public bool disabled;
private bool isAwaitingResponse = false;
void Start()
{
rb = GetComponent<Rigidbody>();
@@ -58,9 +79,94 @@ public class SpringyButtonPhysics : MonoBehaviour {
// --- 5. Convert back to world and move via Rigidbody ---
Vector3 worldPos = buttonDirectionRoot.TransformPoint(newLocalPos);
rb.MovePosition(worldPos);
//Keeping the buttons firing until their needs have been met
if (!isAwaitingResponse || disabled) return;
if (!isInsideButton && box != null)
{
if (box.currentFloor == designatedFloor) StartCoroutine(DeactivateAfterDelay(0.5f));
else Pressed();
return;
}
if (isInsideButton && box != null)
{
if (isFloorButton)
{
if (designatedFloor == box.currentFloor) StartCoroutine(DeactivateAfterDelay(0.5f));
else Pressed();
return;
}
if (isCloseDoorsButton)
{
if (box.state == ElevatorState.ClosingDoors|| box.state == ElevatorState.Moving) StartCoroutine(DeactivateAfterDelay(0.5f));
else Pressed();
return;
}
else
{
if (box.state == ElevatorState.OpeningDoors || box.state == ElevatorState.Moving) StartCoroutine(DeactivateAfterDelay(0.5f));
else Pressed();
return;
}
}
}
public void Pressed()
{
Debug.Log("Button is pressed");
isAwaitingResponse = true;
foreach (Renderer renderer in targetRenderers)
{
renderer.material = pressedMaterial;
}
//Debug.Log("Button is pressed");
if (ElevatorOuter != null && !isInsideButton) ElevatorOuter.CallElevator();
if (disabled) StartCoroutine(DeactivateAfterDelay(0.5f));
if (box != null && isInsideButton)
{
if (isFloorButton)
{
box.targetFloor = designatedFloor;
if (box.targetFloor != box.currentFloor) {
Debug.Log("Lift transfer sequence");
StartCoroutine(box.LiftTransferSequence());
}
else StartCoroutine(box.OpenDoors());
return;
}
if (isCloseDoorsButton)
{
//Debug.Log("Closing Doors");
StartCoroutine(box.CloseDoors());
return;
}
else
{
StartCoroutine(box.OpenDoors());
//Debug.Log("Opening Doors");
return;
}
}
}
public void Deactivate()
{
Debug.Log("Deactivating");
//if (isAwaitingResponse) return;
isAwaitingResponse = false;
foreach (Renderer renderer in targetRenderers)
{
renderer.material = defaultMaterial;
}
}
private System.Collections.IEnumerator DeactivateAfterDelay(float delay)
{
yield return new WaitForSeconds(delay);
//isAwaitingResponse = false;
Deactivate();
}
}