Camera Shake Effect (Constant)?

I need a camera shake effect that is continuous and doesn’t just shake once, but every single answer to this question out there only offers a one-time shake function. As if that weren’t annoying enough, it’s always the same exact script. (Why is there always such an infuriating lack of solutions for the most basic and widely used game features when it comes to Unity?) I’ve tried taking the infamous “if (shake > 0)” script and just getting rid of the line that decreases the “shake” variable every frame, but then as the camera shakes, it gets farther and farther away from where it’s supposed to be, until it’s completely out of bounds. I need to keep track of where the camera is supposed to be, and KEEP IT THERE, not let it bounce around constantly getting a new reference point in 3D space that’s progressively more and more offset from the player. Does anybody know a good way to do this (preferably in JS)?

The script that offsets the camera:

var shakeTime = false;
var shakeAmount : float;

function Update() {
     
	if (shakeTime == true)
	{ 
		Camera.main.transform.localPosition = Random.insideUnitSphere * shakeAmount;
	}
}

The community is fine, they probably wont answer because you behave like a pretentious jerk. Camera script shake is based on modifying transform and rotation by random values, If you want to keep it not going away you either reset the position to origin ( store its transform.position at start to vector 3 variable). and than if it goes too far (check if the position moved to much) or after a time, you just set the transform position back to origin. Alternatively you could do a loop animation of the camera shake but it will be hard to achieve the same effect as a random script.

BTW you have gathered many badges here it means you have been here for a while, why you keep coming back if the community is so bad ?

I apoligise for my previous comments, but the main reason why I was upset was from the first comment you posted on your own question.

Now back to the question at hand.
There is nothing inherently wrong with the script you provided, it looks like it works well.
I presume that you have parented another object to the camera? If so then after the shaking is done (shaketime == false), you can just put the localPosition of the camera back to Vector3.zero.

This parented object will actually be the reference point of where the camera is supposed to be in this case. As such, when wanting to change the camera’s position, change the parented object’s position instead.

I’m not sure if this was really the answer that you were looking for tough, so feel free to ask more questions if needed.

I figured out how to make it work, thanks to Razacx for making a rather obvious suggestion that I’m not sure why I didn’t think of. For anyone who might have a similar problem and happen stumble upon this in the future, basically, I ended up using this code for the shaking:

Camera.main.transform.localPosition.x = Random.insideUnitSphere.x * shakeAmount;
Camera.main.transform.localPosition.y = Random.insideUnitSphere.y * shakeAmount + 1.78;

I thought the code I posted was the code that was causing the offsetting of the camera, but I had tried more than one solution to no avail, so I guess I got them mixed up. Either way, the code I posted still had a big side effect - it was resetting my camera’s Y position back to the center of the Character Controller object it was parented to, as it was staying within the limits of shakeAmount. To counter this I simply added a float which was just under the Y axis of my camera’s local position.

I also wasn’t even aware that one could specify individual axes with “Random.insideUnitSphere”, but I thankfully figured that out and was able to get the shaking just how I want it.

For some reason I had initially thought that resetting the position at all would prevent the shaking from working properly, so it’s a good thing I decided to just go ahead and try it anyway. Again, Razacx’s simple suggestion is what brought me to this solution, so thanks for that, Razacx.

If you put your object under a parent that sets the world position, you can offset the localPosition to give it shake and when your offset goes to zero your object will return to the neutral position. You can keep moving the parent around as needed, but the child can keep applying the shake.

Please don’t use random values to cause camera shake! Use a continuous function like perlin noise or sine to get smooth movement.

Here’s a C# script ShakeIn2D that can shake objects or camera as shown in these videos. It has a continous mode that applies nonstop shake.