0
\$\begingroup\$

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?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

A simple solution would be to create and empty object( Camera container ) that follows the player and have the camera as a child. You can animated the shake independently however you want with the camera gameobject as long as it's a child of the container.

-- Container Object (Follow player)

└ Camera Object ( do animation )

\$\endgroup\$
5
  • \$\begingroup\$ Wow! This seems to be a very cool solution! Thank you very much! \$\endgroup\$
    – bukka.wh
    Oct 21, 2014 at 9:04
  • \$\begingroup\$ Glad you liked it, if you for really want to use scripting in the future for something similar, Transform.translate is probably what you need to use. \$\endgroup\$
    – Sohail05
    Oct 21, 2014 at 9:07
  • \$\begingroup\$ Do you mean using tranform.translate instead of using transform.localPosition = originalPos + Random.insideUnitSphere * shakeAmount? \$\endgroup\$
    – bukka.wh
    Oct 21, 2014 at 9:12
  • \$\begingroup\$ It would still be overly complicated (depending on what you need) since you need to keep 2 locations in mind, the camera follow location and the camera shake added over it. or this could work: ` if (CanShake) { transform.position = new Vector3(targetPlayer.transform.position.x, transform.position.y, transform.position.z; transform.translate = Random.insideUnitSphere * shakeAmount; } else { transform.position = new Vector3(targetPlayer.transform.position.x, transform.position.y, transform.position.z; }` \$\endgroup\$
    – Sohail05
    Oct 21, 2014 at 9:19
  • \$\begingroup\$ Aha! Yes, this is a very good idea! Thank you very much again! \$\endgroup\$
    – bukka.wh
    Oct 21, 2014 at 9:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .