Hello everyone! Recently I have started developing video games, and I am new to C# and I don’t know how to script as good as everyone.
There are my both Camera First Person Look and Pause Menu Scripts, I hope someone can offer me the help I need as fast as possible.
I will explain the situation. So basically when I press “ESC” the button that gives the menu, the cursor is here, But the FPS Controller Camera is still moving and I don’t wish it like that.
FirstPersonLook.cs >
using UnityEngine;
public class FirstPersonLook : MonoBehaviour
{
[SerializeField]
Transform character;
public float sensitivity = 2;
public float smoothing = 1.5f;
Vector2 velocity;
Vector2 frameVelocity;
void Reset()
{
// Get the character from the FirstPersonMovement in parents.
character = GetComponentInParent<FirstPersonMovement>().transform;
}
void Start()
{
// Lock the mouse cursor to the game screen.
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
// Get smooth velocity.
Vector2 mouseDelta = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
Vector2 rawFrameVelocity = Vector2.Scale(mouseDelta, Vector2.one * sensitivity);
frameVelocity = Vector2.Lerp(frameVelocity, rawFrameVelocity, 1 / smoothing);
velocity += frameVelocity;
velocity.y = Mathf.Clamp(velocity.y, -90, 90);
// Rotate camera up-down and controller left-right from velocity.
transform.localRotation = Quaternion.AngleAxis(-velocity.y, Vector3.right);
character.localRotation = Quaternion.AngleAxis(velocity.x, Vector3.up);
}
}
PauseMenu.cs >
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PauseMenu : MonoBehaviour
{
public GameObject pauseMenu;
public bool isPaused;
private float speedY;
private float speedX;
// Start is called before the first frame update
void Start()
{
pauseMenu.SetActive(false);
}
private static void SetActive(bool v)
{
throw new NotImplementedException();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (isPaused)
{
ResumeGame();
}
else
{
PauseGame();
}
}
}
public void PauseGame()
{
pauseMenu.SetActive(true);
Cursor.visible = true;
Time.timeScale = 0f;
isPaused = true;
AudioListener.volume = 0;
_ = Time.deltaTime * 1;
}
public void ResumeGame()
{
pauseMenu.SetActive(false);
Cursor.visible = false;
Time.timeScale = 1f;
isPaused = false;
AudioListener.volume = 1;
}
public void GoToMainMenu()
{
Time.timeScale = 1f;
SceneManager.LoadScene("Menu");
}
}
Please help.
,Hello everyone! Recently I have started developing video games, and I am new to C# and I don’t know how to script as good as everyone.
There are my both Camera First Person Look and Pause Menu Scripts, I hope someone can offer me the help I need as fast as possible.
I will explain the situation. So basically when I press “ESC” the button that gives the menu, the cursor is here, But the FPS Controller Camera is still moving and I don’t wish it like that.
FirstPersonLook.cs >
using UnityEngine;
public class FirstPersonLook : MonoBehaviour
{
[SerializeField]
Transform character;
public float sensitivity = 2;
public float smoothing = 1.5f;
Vector2 velocity;
Vector2 frameVelocity;
void Reset()
{
// Get the character from the FirstPersonMovement in parents.
character = GetComponentInParent<FirstPersonMovement>().transform;
}
void Start()
{
// Lock the mouse cursor to the game screen.
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
// Get smooth velocity.
Vector2 mouseDelta = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
Vector2 rawFrameVelocity = Vector2.Scale(mouseDelta, Vector2.one * sensitivity);
frameVelocity = Vector2.Lerp(frameVelocity, rawFrameVelocity, 1 / smoothing);
velocity += frameVelocity;
velocity.y = Mathf.Clamp(velocity.y, -90, 90);
// Rotate camera up-down and controller left-right from velocity.
transform.localRotation = Quaternion.AngleAxis(-velocity.y, Vector3.right);
character.localRotation = Quaternion.AngleAxis(velocity.x, Vector3.up);
}
}
PauseMenu.cs >
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PauseMenu : MonoBehaviour
{
public GameObject pauseMenu;
public bool isPaused;
private float speedY;
private float speedX;
// Start is called before the first frame update
void Start()
{
pauseMenu.SetActive(false);
}
private static void SetActive(bool v)
{
throw new NotImplementedException();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (isPaused)
{
ResumeGame();
}
else
{
PauseGame();
}
}
}
public void PauseGame()
{
pauseMenu.SetActive(true);
Cursor.visible = true;
Time.timeScale = 0f;
isPaused = true;
AudioListener.volume = 0;
_ = Time.deltaTime * 1;
}
public void ResumeGame()
{
pauseMenu.SetActive(false);
Cursor.visible = false;
Time.timeScale = 1f;
isPaused = false;
AudioListener.volume = 1;
}
public void GoToMainMenu()
{
Time.timeScale = 1f;
SceneManager.LoadScene("Menu");
}
}
Please help.