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;
}
}