Startpunkten
This commit is contained in:
40
Assets/Scripts/CameraController.cs
Normal file
40
Assets/Scripts/CameraController.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class CameraController : MonoBehaviour {
|
||||
public Transform player; // Dra in din Player i Inspector
|
||||
public float smoothSpeed = 0.125f; // Hur mjukt kameran följer
|
||||
public Vector3 offset = new Vector3(0, 0, -10); // Kamerans position bakom spelaren
|
||||
|
||||
// Begränsningar för kamerans rörelse
|
||||
private float minX = -16.4f;
|
||||
private float maxX = 16.4f;
|
||||
private float minY = -15f;
|
||||
private float maxY = 15f;
|
||||
|
||||
// Dead zone (hur långt spelaren måste röra sig innan kameran följer)
|
||||
public float deadZoneX = 0.5f;
|
||||
public float deadZoneY = 0.3f;
|
||||
|
||||
void LateUpdate() {
|
||||
Vector3 targetPosition = player.position + offset;
|
||||
|
||||
// Dead zone: kameran rör sig bara om spelaren är utanför zonen
|
||||
float deltaX = targetPosition.x - transform.position.x;
|
||||
float deltaY = targetPosition.y - transform.position.y;
|
||||
|
||||
if (Mathf.Abs(deltaX) < deadZoneX) {
|
||||
targetPosition.x = transform.position.x;
|
||||
}
|
||||
|
||||
if (Mathf.Abs(deltaY) < deadZoneY) {
|
||||
targetPosition.y = transform.position.y;
|
||||
}
|
||||
|
||||
// Begränsa kameran inom banans gränser
|
||||
targetPosition.x = Mathf.Clamp(targetPosition.x, minX, maxX);
|
||||
targetPosition.y = Mathf.Clamp(targetPosition.y, minY, maxY);
|
||||
|
||||
// Mjuk rörelse
|
||||
transform.position = Vector3.Lerp(transform.position, targetPosition, smoothSpeed);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user