Coding Adventure – Golem Player Controller Con’t

Camera Shake

I wanted the camera to shake slightly when the player bumps into objects, especially other players. This will ultimately be a gameplay mechanic where the player uses their collected moss to hit others to shake off some of the opponents at the slight cost of their own.

I found a script on GitHub by Andersonaddo, and with a few tweaks, it was working well.

To keep the camera from snapping back to a set position, I modified the script so that it reset back to position and rotation that it was prior to adding the shake effect.

RestPositionOffset = transform.position;
RestRotationOffset = new Vector3(0f, transform.rotation.eulerAngles.y, 0f);

The cubes in the scene are tagged as Players. The shake effect occurs when the collider of the Player detects a collision with an object of a certain tag.

// hits another player
if (collision.gameObject.tag == "Player") {
  CC.HitByPlayer();
  CameraShaker.Instance.ShakeOnce(4f * RB.velocity.magnitude, 4f, .1f, 
                                  1f * RB.velocity.magnitude / 2);
}

The shake duration and magnitude are multiplied by the Player’s velocity magnitude so that the fast the Player is moving, the harder and longer the shake will be.

Project Links

The EZ-Camera-Shake repository can be found here: https://github.com/andersonaddo/EZ-Camera-Shake-Unity

You can follow my project on GitHub here: https://github.com/Scottin3d/GolemPlayground