Spieler kann sich umgucken + Cursor ist gelockt

This commit is contained in:
Jan Breitkreuz 2024-06-14 10:30:48 +02:00
parent 132d6bf1b3
commit f831f7e310
2 changed files with 29 additions and 4 deletions

View File

@ -658,8 +658,8 @@ MonoBehaviour:
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
speed: 2 speed: 2
xSensitivity: 200 xSensitivity: 250
ySensitivity: 200 ySensitivity: 250
cam: {fileID: 1132902784} cam: {fileID: 1132902784}
--- !u!1 &1771553239 --- !u!1 &1771553239
GameObject: GameObject:

View File

@ -1,5 +1,6 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading;
using UnityEngine; using UnityEngine;
public class PlayerMoveScript : MonoBehaviour public class PlayerMoveScript : MonoBehaviour
@ -11,9 +12,13 @@ public class PlayerMoveScript : MonoBehaviour
private Rigidbody rb; private Rigidbody rb;
public Camera cam; public Camera cam;
private bool cursorIsLocked;
void Start() void Start()
{ {
rb = GetComponent<Rigidbody>(); rb = GetComponent<Rigidbody>();
Cursor.lockState = CursorLockMode.Locked;
cursorIsLocked = true;
} }
// Update is called once per frame // Update is called once per frame
@ -35,6 +40,8 @@ public class PlayerMoveScript : MonoBehaviour
look(); look();
lockCursor();
} }
public void look() public void look()
@ -47,7 +54,25 @@ public class PlayerMoveScript : MonoBehaviour
float newDirX = currentRotation.y + xMouseMovement; float newDirX = currentRotation.y + xMouseMovement;
float newDirY = currentRotation.x - yMouseMovement; float newDirY = currentRotation.x - yMouseMovement;
cam.transform.rotation = Quaternion.Euler(0, newDirX, 0); cam.transform.localRotation = Quaternion.Euler(newDirY, 0, 0);
transform.rotation = Quaternion.Euler(newDirY, 0, 0); transform.rotation = 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;
}
}
} }
} }