Controlevents are now working

This commit is contained in:
finnchen123
2026-01-25 13:23:03 +01:00
parent 93f02eee13
commit b657de5ea0
7 changed files with 75 additions and 33 deletions

View File

@@ -7,6 +7,7 @@ using UnityEngine.InputSystem;
using UnityEngine.UI;
using Assets.Scripts.Player;
using Assets.Scripts.InteractableObjects;
using System;
public class Controls : MonoBehaviour
{
@@ -55,34 +56,9 @@ public class Controls : MonoBehaviour
{
playerInput.SwitchCurrentActionMap("Menu");
}
if (!player.GetComponent<PlayerGameObject>().takeDamage(0))
if (uihandler.canPlayerMove())
{
if (!uihandler.isPlayerInFight())
{
if (uihandler.canPlayerRotate())
{
lookInput = Mouse.current.delta.ReadValue();
Vector2 targetRotation = lookInput * sensitivityMouse * Time.deltaTime;
currentRotation = Vector2.SmoothDamp(
currentRotation,
targetRotation,
ref rotationVelocity,
smoothTime
);
yaw += currentRotation.x;
pitch -= currentRotation.y;
pitch = Mathf.Clamp(pitch, minPitch, maxPitch);
// Apply rotations
transform.rotation = Quaternion.Euler(0f, yaw, 0f);
playerCam.GetComponent<PlayerCamera>().LookAround(pitch);
player.GetComponent<PlayerGameObject>().rotate(yaw);
}
if (uihandler.canPlayerMove())
{
player.GetComponent<PlayerGameObject>().move(input);
}
}
ControlEvents.Move(input);
}
}
@@ -105,9 +81,30 @@ public class Controls : MonoBehaviour
}
}
public void OnLooking(InputValue value)
public void LateUpdate()
{
//lookInput = value.Get<Vector2>();
//Stop execution if player is not created yet or if player is dead
if (player.GetComponent<PlayerGameObject>().takeDamage(0)) return;
//Stop execution if player is in fight
if (uihandler.isPlayerInFight()) return;
if (uihandler.canPlayerRotate())
{
lookInput = Mouse.current.delta.ReadValue();
Vector2 targetRotation = lookInput * sensitivityMouse * Time.deltaTime;
currentRotation = Vector2.SmoothDamp(
currentRotation,
targetRotation,
ref rotationVelocity,
smoothTime
);
yaw += currentRotation.x;
pitch -= currentRotation.y;
pitch = Mathf.Clamp(pitch, minPitch, maxPitch);
ControlEvents.Look(pitch, yaw);
}
}
public void OnMovement(InputValue value)

View File

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

View File

@@ -0,0 +1,17 @@
using UnityEngine;
public static class ControlEvents
{
public static event System.Action<float, float> OnLookingInput;
public static event System.Action<Vector3> OnMovingInput;
public static void Look(float pitch, float yaw)
{
OnLookingInput?.Invoke(pitch, yaw);
}
public static void Move(Vector3 force)
{
OnMovingInput?.Invoke(force);
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: f6f57f29eb0f62ddcbeda3faf4517459

View File

@@ -11,6 +11,16 @@ namespace Assets.Scripts.Player
UIHandler uihandler;
GameObject interact;
private void OnEnable()
{
ControlEvents.OnLookingInput += LookAround;
}
private void OnDisable()
{
ControlEvents.OnLookingInput -= LookAround;
}
// Start is called before the first frame update
void Start()
{
@@ -43,7 +53,7 @@ namespace Assets.Scripts.Player
return null;
}
public void LookAround(float pitch)
public void LookAround(float pitch, float yaw)
{
transform.localRotation = Quaternion.Euler(pitch, 0f, 0f);
}

View File

@@ -38,6 +38,14 @@ namespace Assets.Scripts.Player
#if UNITY_EDITOR
SceneHandler.switchGameToMenu();
#endif
ControlEvents.OnLookingInput += rotate;
ControlEvents.OnMovingInput += move;
}
private void OnDisable()
{
ControlEvents.OnLookingInput -= rotate;
ControlEvents.OnMovingInput -= move;
}
void Start()
@@ -179,7 +187,7 @@ namespace Assets.Scripts.Player
GameObject.Find("QuestLog").GetComponent<QuestLog>().updateQuests("explore", gameObject, 1);
}
public void rotate(float yaw){
public void rotate(float pitch, float yaw){
transform.rotation = Quaternion.Euler(0f, yaw, 0f);
}