initial room transitioning
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user