How to combine the camera that follows the player and the camera shake effect in Unity?

Hi. I need to combine both camera that follows the player and the camera shake effect in the same moment. The problem is in that, if I make the shake effect for the camera, then the camera doesn’t follow the player and if I make the camera follow the player, then the shake effect doesn’t appear. The basic idea of my script is next:

Vector3 originalPos;
GameObject targetPlayer;
bool canShake;
void OnEnable()
	{
		originalPos = camTransform.localPosition;
	}
 
	void Update()
	{
            // Camera shakes  
		if (CanShake)
		{
			transform.localPosition = originalPos + Random.insideUnitSphere * shakeAmount;
		}
            // Camera follows the player
		else
		{
			transform.position = new Vector3(targetPlayer.transform.position.x, transform.position.y, transform.position.z;
		}
	}

Does anyone have ideas how to combine these two things (camera shake effect and camera that follows the player) at the same moment?

Hi mate,

I’m not able to test this but here’s my analysis:

  transform.localPosition = originalPos + Random.insideUnitSphere * shakeAmount

This does exactly what you’re seeing it does. When CanShake is true, the camera sits at original pos and shakes about. Just off the top of my head, what you might consider doing is putting it as an extra part of your script, so something like this:

    void OnEnable()     
    {  originalPos = camTransform.localPosition;     
    }       


    void Update()     
    {  
    transform.position = new Vector3(targetPlayer.transform.position.x, transform.position.y, transform.position.z);  //The camera is now always updating position.      

if (CanShake==true)         {         // The shaking occurs if CanShake is true'
    transform.localPosition = originalPos + Random.insideUnitSphere * shakeAmount;
    } 
}            

Sorry if this code appears a little dodgy ,but im on Lunch!

Hope this helps,

Popuppirate

What if for example …

Look that every cam shake script has a camera original position, maybe just in camshake script we
write a ref to a camera folow script and set up the cam shake original cam position to original position on cam follow script. But we also need to remember that in cam folow script we all make in late update func, so in cam shake if we have every frame that will be not logic. cam must know that she follow the player but her function to follow must breaks for a amount of sec for making cam shake function. It maybe like courutines, so we must create a IEnumerator func.
So in the end will look like : player equip a sword, than player press key to jump and attack ground from air, than we need an animation trigger in the momment when sword “collide” or touch the ground, this event will coll ienumerator that breaks for a “Ammount of seconds that we choose like CAM SHAKE DURATION” and after this cam follow will continue work… its only my logic im not too good at programing just 3 mounth work with unity. @bukka