- Home /
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?
Answer by popuppirate · Oct 21, 2014 at 12:10 PM
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
Thank you very much for answer! I'll try to rewrite my code according to your recommendations and then I'll post here my progress))
Thank you for a good idea again. I have modified my script and now everything is working cool!!!
No probs mate- basically for future reference, imagine that everything I want to happen all the time sits outside any condition in the update. I Basically like this:
void Update(){
//Stuff I do here happens every frame, so it's good for a camera because the player might move at any given moment.
if(some_condition==true){
//Stuff I do here happens ONLY if some_condition is true;
//if you put in stuff that you want to keep happening all the time here,
//like updating the camera position, it'll only work when some_condition //is true.
} else{
//The same is to be said with the opposite-all the stuff I put in //here will ONLY happen when some_condition!= true (is not true)
}
}
Hope this helps-don't $$anonymous$$d me if I'm oversimplifying- hope it all works out for you matey!
Cheers,
Popuppirate
Answer by And1love · Sep 13, 2018 at 08:20 AM
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... it`s only my logic i`m not too good at programing just 3 mounth work with unity. @bukka
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Shake camera up and down when running? 0 Answers
Camera Movement (One Position to Another) 1 Answer
Positioning Textures on screen 0 Answers
Camera follow help? 1 Answer