When i start now the game the camera is very close to the player. I want this to be a pause view mode and also in this mode to display the main menu. I didn’t create yet the main menu i will do that later.
What i want to do is to use the Escape key when i will click the Escape key while playing the game in any place and any situation it the camera will fly smooth to the character very close view and will display the main menu.
another click on escape will move the camera fly back to the game playing mode view.
My guess or my thinking is that the came in game playing mode should be somewhere in position behind the player and a bit above the player.
In the screenshot is how should the pause/main menu view mode should look like.
And when i mean pause it’s not to stop everything freezing but idle.
I’m using now Time.scaleTime 0 and 1 values to pause/continue the game but scaleTime freeze the game on pause and i want to be idle to not freeze/stop.
And the in game playing view mode it should be something like that:
So when clicking the escape key it will switch between the two views but it should move the camera smooth between the view like the camera fly between the two positions. And also it should calculate automatic the view in any place in the game where ever the player is when i will click the escape key it will back to each view.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MainMenu : MonoBehaviour {
int count = 0;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (count == 0)
{
Time.timeScale = 0;
count = 1;
}
else
{
Time.timeScale = 1;
count = 0;
}
// to make here a switching mode between
//playing screen and pause/main menu screen.
}
}
}
Then i tried to play also with the camera position in this script but i have no idea how to make the camera fly between the two positions and how to make that it will work in any place in the game.
I tried to play with the camera. So i can get the first time distance between the camera and player the main menu mode view. And i can get the first time camera position. But how do i set that in any place in the game it will back to the original distance and position ? If the player is walking somewhere in the game the camera position is now not the same So i know the distance it should be but not the position. How do i set the psotion between the camera and player each time in main menu view ?
And how do i make the camera to fly smooth to the in game playing view and back ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MainMenu : MonoBehaviour {
int count = 0;
public Camera _camera;
public GameObject player;
Vector3 currentCameraPosition;
Vector3 currentPlayerPosition;
// Use this for initialization
void Start ()
{
currentCameraPosition = _camera.transform.position;
float distance = Vector3.Distance(currentPlayerPosition, currentCameraPosition);
currentPlayerPosition = player.transform.position;
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (count == 0)
{
_camera.transform.position = currentCameraPosition;
//Time.timeScale = 0;
count = 1;
}
else
{
_camera.transform.position = new Vector3(_camera.transform.position.x - 20, _camera.transform.position.y - 20, 0);
//Time.timeScale = 1;
count = 0;
}
// to make here a switching mode between
//playing screen and pause/main menu screen.
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MainMenu : MonoBehaviour
{
public Camera _camera;
public GameObject player;
Vector3 oldCameraPosition;
Quaternion oldCameraRotation;
bool isPaused = false;
bool isMoving = false;
// Set these to position the camera close to the player
public float distanceBehind;
public float distanceUp;
// the camera is going to zoom down to the player
// then spin around to face him. Set those two times
public float zoomDownTime;
public float spinAroundTime;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape) && !isMoving)
{
if (!isPaused)
{
oldCameraPosition = _camera.transform.position;
oldCameraRotation = _camera.transform.rotation;
isPaused = true;
StartCoroutine(PauseGame());
}
else
{
isPaused= false;
StartCoroutine(UnPauseGame());
}
// to make here a switching mode between
//playing screen and pause/main menu screen.
}
}
IEnumerator PauseGame()
{
// Don't let the player hit Escape consnatly and start a million Coroutines
isMoving = true;
// lets get a position that is directly behind the player by some amount
// tweak this till it looks right
Vector3 nearPlayer = player.transform.position - player.transform.forward * distanceBehind;
// Now lets move up a little
nearPlayer.y += distanceUp;
// Now we Lerp from our current position to here
float currentTime = 0;
while (currentTime < zoomDownTime)
{
currentTime += Time.deltaTime;
float t = currentTime / zoomDownTime;
_camera.transform.position = Vector3.Lerp(oldCameraPosition, nearPlayer, t);
yield return null;
}
// Now lets spin around the player
Vector3 eulerAngles = oldCameraRotation.eulerAngles;
eulerAngles.y += 180;
Quaternion newRotation = Quaternion.Euler(eulerAngles);
currentTime = 0;
while (currentTime < spinAroundTime)
{
currentTime += Time.deltaTime;
float t = currentTime / spinAroundTime;
_camera.transform.rotation = Quaternion.Slerp(oldCameraRotation, newRotation, t);
yield return null;
}
isMoving = false;
}
IEnumerable UnPauseGame()
{
isMoving = true;
Vector3 startPosition = _camera.transform.position;
Vector3 startRotation = _camera.transform.rotation;
float currentTime = 0;
while (currentTime< spinAroundTime)
{
currentTime += Time.deltaTime;
float t = currentTime / spinAroundTime;
_camera.transform.rotation = Quaternion.Slerp(startRotation, oldCameraRotation, t);
yield return null;
}
currentTime = 0;
while (currentTime < zoomDownTime)
{
currentTime += Time.deltaTime;
float t = currentTime / zoomDownTime;
_camera.transform.position = Vector3.Lerp(startPosition, oldCameraPosition, t);
yield return null;
}
isMoving = false;
}
}
This code should zoom down to the player, then spin around to be in front of him. It assume your camera starts behind the player. You have to set some variables in the inspector (I commented them in the code). You need to set the distance behind the player to zoom to, as well as high high above him. And the time it takes to zoom down, and spin around.\
You might need to modify the full 180 degree spin if its not where you want it. Also the camera might be looking too far down at the player’s feet. In that case also subtract some from the eulerangles.x it will make it look up.
One thing you might try is to combine the two while loops into one so it zooms in and spins around at the same time. Then you would just need one time variable and run both Vector3.lerp and Quaternion.Slerp in the same while loop off that one time.