Camera Shake Issues

This is my first time posting on this forum so I hope I am not posting in the wrong way or in the wrong place.

I am having an issue with a camera shake script I found for a 2D game.
The Camera appears to follow through with the animation nice and easy but when it reaches the loop where everything is reset, the camera reverts to the 0, 0, 0 ; position where my current camera position is 0,0,-1; Thus pulling the view behind all of my 2D assets.

Here is what I have.

using UnityEngine;
using System.Collections;

public class CamShakeSimple : MonoBehaviour

{ //CHANGE THE VECTOR3 to -z the vector 3
    Vector3 originalCameraPosition;
 
    float shakeAmt = 0;
    //float originalCameraPosition = (0f, 0f, -1f); TRYING TO SET THE ORIGINALCAMERAPOSITION TO 0,0,-1
 
    public Camera mainCamera;


    void OnCollisionEnter2D(Collision2D coll)
    {
     
        shakeAmt = coll.relativeVelocity.magnitude * .0050f;
        InvokeRepeating("CameraShake", 0, .01f);
        Invoke("StopShaking", 0.3f);

    }
 
    void CameraShake()
    {
        if(shakeAmt>0)
        {
            float quakeAmt = Random.value*shakeAmt*2 - shakeAmt;
            Vector3 pp = mainCamera.transform.position;
            pp.y += quakeAmt; // can also add to x and/or z
            mainCamera.transform.position = pp;

        }
    }
 
    void StopShaking()
    {
        CancelInvoke("CameraShake");
        mainCamera.transform.position = originalCameraPosition.z = -1f;



    }


}

Love to help Please format your code and use the Insert code option for readability.

How do I format it?

Does this line compile for you?

mainCamera.transform.position = originalCameraPosition.z = -1f;

Where would I put this? I have that line of code in the StopShaking step.

Yes, it is in your code, but as far as I can see it, it is not valid code and doesn’t even compile.

Im getting an error saying the newVector3 does not exist in the current context

Sorry i was kinda side tracked when i gave you that poor excuse for an answer. Im in a good mood today so happy birthday.

using UnityEngine;
using System.Collections;

public class CameraShake : MonoBehaviour {

    public float duration = 0.5f;
    public float speed = 1.0f;
    public float magnitude = 0.1f;
  
    void Update() {
        //Press F on the keyboard to simulate the effect
        if(Input.GetKeyDown(KeyCode.F)) {
            PlayShake();
        }
    }
  
    //This function is used outside (or inside) the script
    public void PlayShake() {
        StopAllCoroutines();
        StartCoroutine("Shake");
    }
  
    private IEnumerator Shake() {
        float elapsed = 0.0f;
      
        Vector3 originalCamPos = transform.position;
        float randomStart = Random.Range(-1000.0f, 1000.0f);
      
        while (elapsed < duration) {
            elapsed += Time.deltaTime;          
          
            float percentComplete = elapsed / duration;          
          
            float damper = 1.0f - Mathf.Clamp(1.5f * percentComplete - 1.0f, 0.0f, 1.0f);
            float alpha = randomStart + speed * percentComplete;
          
            float x = Mathf.PerlinNoise(alpha, 0.0f) * 2.0f - 1.0f;
            float y = Mathf.PerlinNoise(0.0f, alpha) * 2.0f - 1.0f;
          
            x *= magnitude * damper;
            y *= magnitude * damper;
          
            transform.position = new Vector3(x, y, originalCamPos.z);
          
            yield return 0;
        }
      
        transform.position = Vector3.Lerp(transform.position, originalCamPos, Time.deltaTime * 5f);
    }
}

You could also use Mathf.UnitSphere for randomizing the shaking but PerlinNoise is my fav.