How to do camera shake (to simulate heavy object landing)?

Hey, can anyone help me figure how to simulate a heavy object landing? I am very new to coding, so I haven’t quite figured out the logic behind the effect. The idea is, my game has random object spawning from above ground, and they have varying weight; when they spawn they will fall from the sky on to the ground, and I want the ground to “rumble/ shake” slightly when it lands. I think there are other ways to simulate weight (so if you know how to, please show me). As of now I chose to do the camera shake on collision, though haven’t qutie got that down. Below is a camera shake code I have written using a tutorial found online.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraShake : MonoBehaviour {

    public Camera mainCam;

    float shakeAmount = 0;

    void Awake()
    {
        if (mainCam == null)
            mainCam = Camera.main;
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.T))
        {
            Shake(0.1f, 0.2f);
        }
    }

    public void Shake(float amt, float length)
    {
        shakeAmount = amt;
        InvokeRepeating("DoShake", 0, 0.01f);
        Invoke("StopShake", length);
    }

    void DoShake()
    {
        if (shakeAmount > 0)
        {
            Vector3 camPos = mainCam.transform.position;
           
            //float offsetX = Random.value * shakeAmount * 2 - shakeAmount;
            float offsetY = Random.value * shakeAmount * 2 - shakeAmount;
            //camPos.x += offsetX;
            camPos.y += offsetY;

            mainCam.transform.position = camPos;
        }
    }

    void StopShake()
    {
        CancelInvoke("DoShake");
        mainCam.transform.localPosition = Vector3.zero;

    }
}

I put this code in my “Game Master” object. I then created another script to call the camera shake when colliding with the ground in one of the object that will be spawning from the sky (that means I will have several of this script placed in every object I choose to spawn. I tried calling it using this code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ShakeOnCollision : MonoBehaviour {

    public float camShakeAmt = 0.1f;
    public float camShakeLength = 0.1f;
    CameraShake camShake;

    void Start()
    {
        camShake = GameMaster.gm.GetComponent<CameraShake>();
        if (camShake == null)
        {
            Debug.LogError("No CameraShake found in gm object");
        }
    }

    void OnTriggerEnter(Collider other)
    {
        camShake.Shake(camShakeAmt, camShakeLength);
    }
}

The first error is “No CameraShake found in gm object”. That was in the log, though I don’t understand why, since I made sure that my GameMaster object is “callable”.

The second problem is “OnTriggerEnter” doesn’t seem to work/ or correctly written.

Please help me,

Thanks.

How?

Is GameMaster static?
Is gm a reference to a gameobject with the CameraShake script attached?

Yup gm is a reference to a gameobject with the CameraShake script attached. I even gave the GameMaster object a tag.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameMaster : MonoBehaviour {

    public static GameMaster gm;

    // Use this for initialization
    void Awake () {
        if (gm == null)
        {
            gm = GameObject.FindGameObjectWithTag("GM").GetComponent<GameMaster>();
        }
    }
   
    // Update is called once per frame
    void Update () {
       
    }
}

It looks like GameMaster is a script and not a GameObject
, you’r trying to get a component from a script?

If so, make it public and attach a reference in GameMaster to the CameraShake thing.

1 Like

oh okay, thanks so much