64 lines
1.5 KiB
C#
64 lines
1.5 KiB
C#
using UnityEngine;
|
|
|
|
public class IntroParallaxLayer : MonoBehaviour
|
|
{
|
|
[Header("Reference")]
|
|
public Transform cameraTransform;
|
|
|
|
[Header("Parallax Strength")]
|
|
public float parallaxX = 0.02f;
|
|
public float parallaxY = 0.01f;
|
|
|
|
[Header("Smoothing")]
|
|
public float smoothSpeed = 4f;
|
|
|
|
private Vector3 startLocalPosition;
|
|
private Quaternion startCameraRotation;
|
|
|
|
private void Start()
|
|
{
|
|
startLocalPosition = transform.localPosition;
|
|
|
|
if (cameraTransform == null && Camera.main != null)
|
|
{
|
|
cameraTransform = Camera.main.transform;
|
|
}
|
|
|
|
if (cameraTransform != null)
|
|
{
|
|
startCameraRotation = cameraTransform.rotation;
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (cameraTransform == null)
|
|
return;
|
|
|
|
Quaternion deltaRotation = Quaternion.Inverse(startCameraRotation) * cameraTransform.rotation;
|
|
Vector3 deltaEuler = deltaRotation.eulerAngles;
|
|
|
|
float yaw = NormalizeAngle(deltaEuler.y);
|
|
float pitch = NormalizeAngle(deltaEuler.x);
|
|
|
|
Vector3 targetPosition = startLocalPosition + new Vector3(
|
|
-yaw * parallaxX,
|
|
pitch * parallaxY,
|
|
0f
|
|
);
|
|
|
|
transform.localPosition = Vector3.Lerp(
|
|
transform.localPosition,
|
|
targetPosition,
|
|
Time.deltaTime * smoothSpeed
|
|
);
|
|
}
|
|
|
|
private float NormalizeAngle(float angle)
|
|
{
|
|
if (angle > 180f)
|
|
angle -= 360f;
|
|
|
|
return angle;
|
|
}
|
|
} |