initial room transitioning

This commit is contained in:
2026-02-27 23:55:04 -06:00
parent 41d3e38870
commit 6052d0a2ce
22 changed files with 4683 additions and 18912 deletions
+33 -121
View File
@@ -5,137 +5,49 @@
public class CameraController : MonoBehaviour
{
private bool isTracking = false; // don't track when parented
private bool returningHome = false; // cam is on its way home
public float baseCamSize = 7; // this is how "zoomed" the ortho cam is
private float camMovementTime = 0.9f; // how much time to switch cam positions
private float camTargetRotation = 0;
private bool isCamRotating = false;
public Transform playerCamHome;
private float moveSpeed;
private float zoomSpeed;
public Transform playerCamHome; // camera's home base
public GameObject cameraObj;
private Camera cameraComponent;
private Vector3 currentCamPosition;
private float desiredCamSize;
private int positionsSet = 0; // this keeps track of how many cam zones we're in
private float timeReturningHome = 0; // how long has the cam been moving home
private List<StaticCamRoom> roomsActive;
public void SetCurrentPosition(Vector3 currentCamPosition, float desiredCamSize)
private void Update()
{
positionsSet++; // we have entered a cam zone
HandleCamRotation();
}
if (currentCamPosition != this.currentCamPosition)
/// <summary>
/// Trigger cam rotation
/// </summary>
public void RotateCam()
{
if (!isCamRotating)
{
cameraObj.transform.parent = null;
float distanceToMove = Vector3.Distance(this.currentCamPosition, currentCamPosition);
float amountToZoom = Mathf.Abs(this.desiredCamSize - desiredCamSize);
moveSpeed = distanceToMove / camMovementTime;
zoomSpeed = amountToZoom / camMovementTime;
isTracking = true;
returningHome = false;
this.currentCamPosition = currentCamPosition;
this.desiredCamSize = desiredCamSize;
float currentY = playerCamHome.transform.rotation.eulerAngles.y;
camTargetRotation = currentY + 90f;
isCamRotating = true;
}
}
public void ReturnCameraToHome()
/// <summary>
/// Move cam to desired rotation (every frame)
/// </summary>
void HandleCamRotation()
{
positionsSet--; // we have exited a cam zone
float currentY = playerCamHome.transform.rotation.eulerAngles.y;
// if this number is greater than 1, we have left a zone but are still in another
if (positionsSet <= 0)
float step = 200 * Time.deltaTime;
float newY = Mathf.MoveTowardsAngle(currentY, camTargetRotation, step);
playerCamHome.RotateAround(
transform.position,
Vector3.up,
newY - currentY
);
if (Mathf.Abs(Mathf.DeltaAngle(newY, camTargetRotation)) < 0.01f)
{
positionsSet = 0;
float distanceToMove = Vector3.Distance(this.currentCamPosition, playerCamHome.position);
float amountToZoom = Mathf.Abs(this.desiredCamSize - baseCamSize);
moveSpeed = distanceToMove / camMovementTime;
zoomSpeed = amountToZoom / camMovementTime;
isTracking = false;
returningHome = true;
currentCamPosition = playerCamHome.position;
desiredCamSize = baseCamSize;
}
else
{
positionsSet--;
SetCurrentPosition(roomsActive[roomsActive.Count - 1].camPosition.position, roomsActive[roomsActive.Count - 1].camZoom);
}
}
public void AddToRoomsActive(StaticCamRoom room)
{
roomsActive.Add(room);
room.RoomEnter();
}
public void RemoveFromRoomsActive(StaticCamRoom room)
{
List<Transform> wallsToKeep = new List<Transform>();
roomsActive.Remove(room);
foreach (StaticCamRoom staticCamRoom in roomsActive)
{
wallsToKeep.AddRange(staticCamRoom.objectsToHide);
}
room.RoomExit(wallsToKeep);
}
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
cameraComponent = cameraObj.GetComponent<Camera>();
roomsActive = new List<StaticCamRoom>();
ReturnCameraToHome();
}
// Update is called once per frame
void Update()
{
if (isTracking)
{
cameraObj.transform.position = Vector3.MoveTowards(cameraObj.transform.position, currentCamPosition, Time.deltaTime * moveSpeed);
cameraComponent.orthographicSize = Mathf.MoveTowards(cameraComponent.orthographicSize, desiredCamSize, Time.deltaTime * zoomSpeed);
}
if (returningHome)
{
currentCamPosition = playerCamHome.position;
// Speed up cam if we've been returning home longer than a certain amount
if (timeReturningHome > 1.0f)
{
cameraObj.transform.position = Vector3.MoveTowards(cameraObj.transform.position, currentCamPosition, Time.deltaTime * moveSpeed * 1.5f);
cameraComponent.orthographicSize = Mathf.MoveTowards(cameraComponent.orthographicSize, desiredCamSize, Time.deltaTime * zoomSpeed * 1.5f);
}
else
{
cameraObj.transform.position = Vector3.MoveTowards(cameraObj.transform.position, currentCamPosition, Time.deltaTime * moveSpeed);
cameraComponent.orthographicSize = Mathf.MoveTowards(cameraComponent.orthographicSize, desiredCamSize, Time.deltaTime * zoomSpeed);
}
if (Vector3.Distance(cameraObj.transform.position, currentCamPosition) < 0.1f)
{
cameraObj.transform.parent = playerCamHome;
cameraObj.transform.localPosition = Vector3.zero;
cameraComponent.orthographicSize = baseCamSize;
returningHome = false;
}
timeReturningHome += Time.deltaTime;
}
else
{
timeReturningHome = 0;
isCamRotating = false;
}
}
}
@@ -20,7 +20,7 @@ AnimationClip:
m_Curve:
- serializedVersion: 3
time: 0
value: {x: 0, y: -250, z: 0}
value: {x: 0, y: -478, z: 0}
inSlope: {x: 0, y: 0, z: 0}
outSlope: {x: 0, y: 0, z: 0}
tangentMode: 0
@@ -99,7 +99,7 @@ AnimationClip:
m_Curve:
- serializedVersion: 3
time: 0
value: -250
value: -478
inSlope: 0
outSlope: 0
tangentMode: 136
+40 -43
View File
@@ -7,6 +7,7 @@ public class PlayerController : MonoBehaviour
public GameObject DougBody;
public Shovel Shovel;
public float walkSpeed;
public float gravity = 10;
float digTime = 0.9f;
float digTimestamp = 0;
@@ -16,10 +17,7 @@ public class PlayerController : MonoBehaviour
private CharacterController characterController;
private CameraController cameraController;
private Vector3 moveDir;
private float gravity = 10;
private float camTargetRotation = 0;
private bool isCamRotating = false;
private bool hasControl = true; // set this to false if we want to stop reading player inputs
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
@@ -31,24 +29,28 @@ void Start()
// Update is called once per frame
void Update()
{
if (!isDigging)
if (hasControl)
{
RotatePlayerTowardMouse();
TryInteract();
if (!isDigging)
{
RotatePlayerTowardMouse();
TryInteract();
}
Walk();
DigDetector();
// TODO: This is just for testing, remove or clean up if used
if (Input.GetKeyDown(KeyCode.LeftShift))
{
cameraController.RotateCam();
}
}
Walk();
DigDetector();
// TODO: This is just for testing, remove or clean up if used
if (Input.GetKeyDown(KeyCode.LeftShift))
{
RotateCam();
}
HandleCamRotation();
}
/// <summary>
/// Handle movement
/// </summary>
void Walk()
{
float verticalMovement = 0;
@@ -81,6 +83,9 @@ void Walk()
characterController.Move(moveDir * Time.deltaTime);
}
/// <summary>
/// Detect input for digging (will eventually be tool agnostic)
/// </summary>
void DigDetector()
{
if (!isDigging)
@@ -99,8 +104,11 @@ void DigDetector()
isDigging = false;
}
}
}
}
/// <summary>
/// Face character graphic toward mouse
/// </summary>
void RotatePlayerTowardMouse()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
@@ -118,6 +126,9 @@ void RotatePlayerTowardMouse()
}
}
/// <summary>
/// Try to interact by detecting nearest interactable
/// </summary>
void TryInteract()
{
if (Input.GetKeyDown(KeyCode.E) && nearestInteractable != null)
@@ -151,33 +162,19 @@ private void OnTriggerExit(Collider other)
}
}
void RotateCam()
/// <summary>
/// Sets the character's "hasControl" bool. All input is ignored when false
/// </summary>
public void SetCharacterControl(bool hasControl)
{
if (!isCamRotating)
{
float currentY = cameraController.playerCamHome.transform.rotation.eulerAngles.y;
camTargetRotation = currentY + 90f;
isCamRotating = true;
}
this.hasControl = hasControl;
}
void HandleCamRotation()
/// <summary>
/// Gets the character's "hasControl" bool. All input is ignored when false
/// </summary>
public bool GetCharacterHasControl()
{
float currentY = cameraController.playerCamHome.transform.rotation.eulerAngles.y;
float step = 200 * Time.deltaTime;
float newY = Mathf.MoveTowardsAngle(currentY, camTargetRotation, step);
cameraController.playerCamHome.RotateAround(
transform.position,
Vector3.up,
newY - currentY
);
if (Mathf.Abs(Mathf.DeltaAngle(newY, camTargetRotation)) < 0.01f)
{
isCamRotating = false;
}
return hasControl;
}
}
+53
View File
@@ -0,0 +1,53 @@
using UnityEngine;
public class RoomDoor : Interactable
{
public RoomDoor linkedDoor;
public bool requiresInteraction; // Do we need to press a button to enter?
TeleportHandler teleportHandler;
// not required and only plays if interaction is required
public Animation doorAnimator;
public AnimationClip openDoor;
public AnimationClip closeDoor;
private void Start()
{
teleportHandler = GameObject.FindWithTag("Player").GetComponent<TeleportHandler>();
}
public override void Interact()
{
if (requiresInteraction)
{
if (doorAnimator && openDoor)
{
doorAnimator.clip = openDoor;
doorAnimator.Play();
}
teleportHandler.EnterRoom(this);
}
}
public void CloseDoor()
{
if (doorAnimator && closeDoor)
{
doorAnimator.clip = closeDoor;
doorAnimator.Play();
}
}
public override void MoveInsideRange()
{
if (!requiresInteraction)
{
teleportHandler.EnterRoom(this);
}
}
public override void MoveOutsideRange()
{
// Do nothing
}
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: f99c228d7f83ab54fb3a97a8f13fb239
-83
View File
@@ -1,83 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class StaticCamRoom : MonoBehaviour
{
// these will be hidden when room is entered
public Transform camPosition;
public Transform[] objectsToHide;
public float camZoom = 5f;
public void RoomEnter()
{
StopAllCoroutines();
foreach (var obj in objectsToHide)
{
StartCoroutine(FadeOutMaterial(0.5f, obj.gameObject));
}
}
public void RoomExit(List<Transform> wallsToKeep)
{
StopAllCoroutines();
foreach (var obj in objectsToHide)
{
if (!wallsToKeep.Contains(obj))
StartCoroutine(FadeInMaterial(0.5f, obj.gameObject));
}
}
private void OnTriggerEnter(Collider other)
{
CameraController controller = other.gameObject.GetComponent<CameraController>();
if (controller != null)
{
controller.AddToRoomsActive(this);
controller.SetCurrentPosition(camPosition.position, camZoom);
}
}
private void OnTriggerExit(Collider other)
{
CameraController controller = other.gameObject.GetComponent<CameraController>();
if (controller != null)
{
controller.RemoveFromRoomsActive(this);
controller.ReturnCameraToHome();
}
}
private IEnumerator FadeOutMaterial(float fadeSpeed, GameObject wallObj)
{
Renderer rend = wallObj.GetComponent<Renderer>();
Color matColor = rend.material.color;
if (matColor.a > 1f)
matColor = new Color(matColor.r, matColor.g, matColor.b, 1f);
while (rend.material.color.a > 0f)
{
matColor.a -= Time.deltaTime / fadeSpeed;
rend.material.color = matColor;
yield return null;
}
rend.material.color = new Color(matColor.r, matColor.g, matColor.b, 0f);
}
private IEnumerator FadeInMaterial(float fadeSpeed, GameObject wallObj)
{
Renderer rend = wallObj.GetComponent<Renderer>();
Color matColor = rend.material.color;
while (rend.material.color.a < 1f)
{
matColor.a += Time.deltaTime / fadeSpeed;
rend.material.color = matColor;
yield return null;
}
rend.material.color = new Color(matColor.r, matColor.g, matColor.b, 1f);
}
}
-2
View File
@@ -1,2 +0,0 @@
fileFormatVersion: 2
guid: 35d74944899dd8b4c9659da46cd64278
+70
View File
@@ -0,0 +1,70 @@
using System.Collections;
using UnityEngine;
using UnityEngine.Rendering.Universal;
using UnityEngine.UI;
// Handle room and scene transitions
public class TeleportHandler : MonoBehaviour
{
public Image blackScreenObject;
PlayerController playerController;
private void Start()
{
playerController = GetComponent<PlayerController>();
}
/// <summary>
/// Triggers the room entrance coroutine, only works if the character is not controllable (keeps from double transitioning)
/// </summary>
public void EnterRoom(RoomDoor door)
{
if (playerController.GetCharacterHasControl())
StartCoroutine(EnterRoomCoroutine(door));
}
/// <summary>
/// Handle teleportation and timing for entering rooms within a scene
/// </summary>
IEnumerator EnterRoomCoroutine(RoomDoor door)
{
float fadeDuration = 0.2f;
float fadeTime = 0;
Color blackScreenColor = Color.black;
playerController.SetCharacterControl(false);
// Play enter room animation (wait seconds during animation)
if (door.requiresInteraction)
yield return new WaitForSeconds(0.2f);
blackScreenObject.gameObject.SetActive(true);
while (fadeTime < fadeDuration)
{
blackScreenColor.a = Mathf.Lerp(0, 1, fadeTime / fadeDuration);
blackScreenObject.color = blackScreenColor;
fadeTime += Time.deltaTime;
yield return null;
}
fadeTime = 0;
transform.position = door.linkedDoor.transform.position;
playerController.DougBody.transform.rotation = door.linkedDoor.transform.rotation;
while (fadeTime < fadeDuration)
{
blackScreenColor.a = Mathf.Lerp(1, 0, fadeTime / fadeDuration);
blackScreenObject.color = blackScreenColor;
fadeTime += Time.deltaTime;
yield return null;
}
fadeTime = 0;
blackScreenObject.gameObject.SetActive(false);
// Play exit animation for linked door (if it exists)
door.linkedDoor.CloseDoor();
yield return new WaitForSeconds(0.2f);
playerController.SetCharacterControl(true);
}
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 4e4075a1c9b67af429062d6dffd07682