Menu eingebaut, + Keybind Settings

This commit is contained in:
2024-08-13 21:11:05 +02:00
parent 92a8ac7768
commit 3a82d1e682
84 changed files with 18133 additions and 27 deletions

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 1d866f9c3fb7f96469b610bdcd45e067
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,96 @@
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
public class InputManagerScript : MonoBehaviour
{
// Input Options
[Header("Input")]
[SerializeField] private KeyCode moveRight;
[SerializeField] private KeyCode moveLeft;
[SerializeField] private KeyCode moveForward;
[SerializeField] private KeyCode moveBack;
[SerializeField] private KeyCode jump;
[SerializeField] private KeyCode shoot;
[SerializeField] private KeyCode reload;
// for the UI of button remaping
[SerializeField] private TextMeshProUGUI[] currentKeys;
// Update is called once per frame
void Update()
{
}
//---------------------------------------- Get Input (for other Scripts) -----------------------------------------
public KeyCode getMoveRight()
{
return moveRight;
}
public KeyCode getMoveLeft()
{
return moveLeft;
}
public KeyCode getMoveForward()
{
return moveForward;
}
public KeyCode getMoveBack()
{
return moveBack;
}
public KeyCode getJump()
{
return jump;
}
public KeyCode getShoot()
{
return shoot;
}
public KeyCode getReload()
{
return reload;
}
//---------------------------------------------- Remap Buttons ----------------------------------------------------
public void remapKey(string actionName, KeyCode key) // remaps the action to the new Button and changes it in the UI (keybinding)
{
switch(actionName)
{
case "moveRight":
moveRight = key; // new Button
currentKeys[0].text = key.ToString();
break;
case "moveLeft":
moveLeft = key;
currentKeys[1].text = key.ToString();
break;
case "moveForward":
moveForward = key;
currentKeys[2].text = key.ToString();
break;
case "moveBack":
moveBack = key;
currentKeys[3].text = key.ToString();
break;
case "shoot":
shoot = key;
currentKeys[4].text = key.ToString();
break;
case "reload":
reload = key;
currentKeys[5].text = key.ToString();
break;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ef2f2a0211f2ace4fb0a262d491323f2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,56 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MenuScript : MonoBehaviour
{
[SerializeField] int menuLayer; // Shows at what layer the current Menu is and what esc should do
[SerializeField] GameObject defaultMenu;
[SerializeField] GameObject settingsMenu;
// Update is called once per frame
void Update()
{
escPressed();
}
public void escPressed()
{
if(Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown("o"))
{
if(menuLayer == 0)
{
defaultMenu.SetActive(true);
Cursor.lockState = CursorLockMode.None;
menuLayer = 1;
}
else if(menuLayer == 1)
{
defaultMenu.SetActive(false);
lockCursor();
menuLayer = 0;
}
else if(menuLayer == 2)
{
settingsMenu.SetActive(false);
defaultMenu.SetActive(true);
menuLayer = 1;
}
}
}
public void setMenuLayer(int i)
{
menuLayer = i;
}
public void quit()
{
Application.Quit();
}
public void lockCursor()
{
Cursor.lockState = CursorLockMode.Locked;
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0100531b5445cc04eb42d8054e43e9c1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,48 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewKeybindScript : MonoBehaviour
{
private bool keybindWasChanged = true;
[SerializeField] private string actionName;
private KeyCode newKey;
private InputManagerScript i;
// Start is called before the first frame update
void Start()
{
i = GameObject.Find("InputManager").GetComponent<InputManagerScript>();
}
// Update is called once per frame
void Update()
{
remapKey();
}
public void remapKey()
{
if(keybindWasChanged == false)
{
if (Input.anyKey) // if a key is pressed
{
foreach (KeyCode kcode in System.Enum.GetValues(typeof(KeyCode))) // goes through all keys to find out which was pressed
{
if (Input.GetKey(kcode))
{
i.remapKey(actionName, kcode); // function from InputManager (changes key)
keybindWasChanged = true;
this.gameObject.SetActive(false); // makes the UI for changing the Key disappear
break;
}
}
}
}
}
public void setChangingKey(string actionName)
{
keybindWasChanged = false;
this.actionName = actionName;
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 00d2815ff536fbb4fa1d8bd94f22131b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -19,15 +19,14 @@ public class PlayerMoveScript : MonoBehaviour
[SerializeField] private Camera cam;
private Rigidbody rb;
private bool cursorIsLocked;
private LayerMask ground;
private InputManagerScript i;
void Start()
{
rb = GetComponent<Rigidbody>();
Cursor.lockState = CursorLockMode.Locked;
cursorIsLocked = true;
i = GameObject.Find("InputManager").GetComponent<InputManagerScript>();
ground = LayerMask.GetMask("Ground");
}
@@ -37,7 +36,6 @@ public class PlayerMoveScript : MonoBehaviour
jump();
move();
look();
lockCursor();
}
@@ -47,10 +45,10 @@ public class PlayerMoveScript : MonoBehaviour
int moveDirX = 0;;
int moveDirZ = 0;
if(Input.GetKey(KeyCode.W)){moveDirZ++;}
if(Input.GetKey(KeyCode.S)){moveDirZ--;}
if(Input.GetKey(KeyCode.D)){moveDirX++;}
if(Input.GetKey(KeyCode.A)){moveDirX--;}
if(Input.GetKey(i.getMoveForward())){moveDirZ++;}
if(Input.GetKey(i.getMoveBack())){moveDirZ--;}
if(Input.GetKey(i.getMoveRight())){moveDirX++;}
if(Input.GetKey(i.getMoveLeft())){moveDirX--;}
Vector3 moveDir = new Vector3(moveDirX, 0, moveDirZ);
moveDir.Normalize();
@@ -60,7 +58,7 @@ public class PlayerMoveScript : MonoBehaviour
public void jump()
{
if(Input.GetKey(KeyCode.Space))
if(Input.GetKey(i.getJump()))
{
if (Physics.CheckSphere(transform.position + Vector3.down * jumpPointDistance, jumpPointRadius, ground) )
{
@@ -101,22 +99,4 @@ public class PlayerMoveScript : MonoBehaviour
cam.transform.localRotation = Quaternion.Euler(newDirY, 0, 0);
transform.localRotation = Quaternion.Euler(0, newDirX, 0);
}
public void lockCursor()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if(!cursorIsLocked)
{
Cursor.lockState = CursorLockMode.Locked;
cursorIsLocked = true;
}
else
{
Cursor.lockState = CursorLockMode.None;
cursorIsLocked = false;
}
}
}
}