mehr sachen
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class Door_Rotate : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private float openAngle = 90f;
|
||||
[SerializeField] private float rotationSpeed = 2f;
|
||||
[SerializeField] private float interactionDistance = 5f;
|
||||
[SerializeField] private Vector3 rotationOffset = Vector3.zero;
|
||||
|
||||
private float targetRotation = 0f;
|
||||
private float currentRotation = 0f;
|
||||
private bool isOpen = false;
|
||||
private Camera playerCamera;
|
||||
private Vector3 pivotPoint;
|
||||
|
||||
|
||||
void Start()
|
||||
{
|
||||
playerCamera = Camera.main;
|
||||
currentRotation = transform.localEulerAngles.y;
|
||||
targetRotation = currentRotation;
|
||||
|
||||
// Calculate pivot point from rotation offset
|
||||
pivotPoint = transform.position + rotationOffset;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (Input.GetKeyDown(KeyCode.E) && IsPlayerLooking())
|
||||
{
|
||||
ToggleDoor();
|
||||
}
|
||||
|
||||
SmoothRotateDoor();
|
||||
}
|
||||
|
||||
private void ToggleDoor()
|
||||
{
|
||||
isOpen = !isOpen;
|
||||
targetRotation = isOpen ? currentRotation + openAngle : currentRotation - openAngle;
|
||||
}
|
||||
|
||||
private void SmoothRotateDoor()
|
||||
{
|
||||
currentRotation = Mathf.Lerp(currentRotation, targetRotation, Time.deltaTime * rotationSpeed);
|
||||
|
||||
// Rotate around the pivot point
|
||||
transform.RotateAround(pivotPoint, Vector3.up, (currentRotation - transform.localEulerAngles.y));
|
||||
transform.localEulerAngles = new Vector3(
|
||||
transform.localEulerAngles.x,
|
||||
currentRotation,
|
||||
transform.localEulerAngles.z
|
||||
);
|
||||
}
|
||||
|
||||
private bool IsPlayerLooking()
|
||||
{
|
||||
if (playerCamera == null) return false;
|
||||
|
||||
Ray ray = new Ray(playerCamera.transform.position, playerCamera.transform.forward);
|
||||
float distanceToPlayer = Vector3.Distance(playerCamera.transform.position, transform.position);
|
||||
|
||||
if (distanceToPlayer > interactionDistance) return false;
|
||||
|
||||
if (Physics.Raycast(ray, out RaycastHit hit, interactionDistance))
|
||||
{
|
||||
return hit.collider.gameObject == gameObject;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 06852f09d119f7647b099917df7e799a
|
||||
@@ -1,4 +1,4 @@
|
||||
//Oliver
|
||||
//Oliver&Morten
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user