my camera shake script always shakes it at the starting position and doesn’t go with the player can anyone help here is the script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraShake : MonoBehaviour
{
// public
[Range(0f, 2f)]
public float intensity;
// private
Transform _target;
Vector3 _initalPos;
// void
void Start()
{
_target = GetComponent<Transform>();
_initalPos = _target.localPosition;
}
float _pendingshakeDuration = 0f;
public void Shake(float duration)
{
if (duration > 0)
{
_pendingshakeDuration += duration;
}
}
bool _isShaking = false;
void Update()
{
if (_pendingshakeDuration > 0 && !_isShaking)
{
StartCoroutine(DoShake());
}
}
IEnumerator DoShake()
{
_isShaking = true;
var startTime = Time.realtimeSinceStartup;
while (Time.realtimeSinceStartup < startTime + _pendingshakeDuration)
{
var randomPoint = new Vector3(Random.Range(-1f, 1f) * intensity, Random.Range(-1f, 1f) * intensity, _initalPos.z);
_target.localPosition = randomPoint;
yield return null;
}
_pendingshakeDuration = 0f;
_target.localPosition = _initalPos;
_isShaking = false;
}
}