So I have been pouring over my code trying to figure something out involving the CameraShake.cs. When the camera shakes and the marine is moving, the camera stops to shake and its transform position is offset from the original position and no longer follows the marine properly. Further, this issue compounds as the marine takes more hits, sometimes he even moves off camera. I have tried to “refind” the marine’s position and resetting the camera to no avail.
I was hoping I could have some tips or advice sent my way, please?
Below is part of my attempt to reset the cameras position after a shake.
All of the comments are code that I added.
public class CameraShake : MonoBehaviour {
// public GameObject newPosition;
// private CameraMovement cameraMovement;
//private PlayerController playerController;
public float shakeDecay = 0.2f;
public float intensity = .3f;
private Vector3 originPosition;
private Quaternion originRotation;
private float shakeIntensity = 0;
/* private void Awake()
{
cameraMovement = GetComponent<CameraMovement>();
playerController = GetComponent<PlayerController>();
}*/
void Update() {
if (shakeIntensity > 0) {
transform.position = originPosition + Random.insideUnitSphere * shakeIntensity;
transform.rotation = new Quaternion(
originRotation.x + Random.Range(-shakeIntensity, shakeIntensity) * .02f,
originRotation.y + Random.Range(-shakeIntensity, shakeIntensity) * .02f,
originRotation.z + Random.Range(-shakeIntensity, shakeIntensity) * .02f,
originRotation.w + Random.Range(-shakeIntensity, shakeIntensity) * .02f);
shakeIntensity -= shakeDecay;
}
}
public void Shake() {
originPosition = transform.position;
originRotation = transform.rotation;
shakeIntensity = intensity;
//transform.position = Vector3.Lerp(transform.position, cameraMovement.followTarget.transform.position, Time.deltaTime * playerController.moveSpeed);
}
}