59 lines
No EOL
1.8 KiB
C#
59 lines
No EOL
1.8 KiB
C#
using UnityEngine;
|
|
|
|
public class CameraOrbit : MonoBehaviour
|
|
{
|
|
[Header("Target Settings")]
|
|
public Transform target; // Das Objekt, um das sich die Kamera drehen soll
|
|
public float distance = 5.0f; // Abstand zur Kamera
|
|
|
|
[Header("Rotation Settings")]
|
|
public float xSpeed = 120.0f; // Geschwindigkeit für horizontale Drehung
|
|
public float ySpeed = 120.0f; // Geschwindigkeit für vertikale Drehung
|
|
public float yMinLimit = -20f; // Min. vertikaler Winkel
|
|
public float yMaxLimit = 80f; // Max. vertikaler Winkel
|
|
|
|
private float x = 0.0f;
|
|
private float y = 0.0f;
|
|
|
|
void Start()
|
|
{
|
|
if (target == null)
|
|
{
|
|
Debug.LogWarning("Kein Target gesetzt! Bitte ein Objekt in 'target' zuweisen.");
|
|
enabled = false;
|
|
return;
|
|
}
|
|
|
|
Vector3 angles = transform.eulerAngles;
|
|
x = angles.y;
|
|
y = angles.x;
|
|
|
|
// Cursor frei beweglich lassen
|
|
Cursor.lockState = CursorLockMode.None;
|
|
Cursor.visible = true;
|
|
}
|
|
|
|
void LateUpdate()
|
|
{
|
|
if (target == null) return;
|
|
|
|
if (Input.GetMouseButton(1)) // Rechte Maustaste gedrückt
|
|
{
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
Cursor.visible = false;
|
|
x += Input.GetAxis("Mouse X") * xSpeed * Time.deltaTime;
|
|
y -= Input.GetAxis("Mouse Y") * ySpeed * Time.deltaTime;
|
|
y = Mathf.Clamp(y, yMinLimit, yMaxLimit);
|
|
} else
|
|
{
|
|
Cursor.lockState = CursorLockMode.None;
|
|
Cursor.visible = true;
|
|
}
|
|
|
|
Quaternion rotation = Quaternion.Euler(y, x, 0);
|
|
Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position;
|
|
|
|
transform.rotation = rotation;
|
|
transform.position = position;
|
|
}
|
|
} |