Bobblehead Wars Camera Shake

So I have been pouring over my code trying to figure something out involving the CameraShake.cs. When the camera shakes and the marine is moving, the camera stops to shake and its transform position is offset from the original position and no longer follows the marine properly. Further, this issue compounds as the marine takes more hits, sometimes he even moves off camera. I have tried to “refind” the marine’s position and resetting the camera to no avail.

I was hoping I could have some tips or advice sent my way, please?

Below is part of my attempt to reset the cameras position after a shake.
All of the comments are code that I added.

 public class CameraShake : MonoBehaviour {
   // public GameObject newPosition;
   // private CameraMovement cameraMovement;
    //private PlayerController playerController;

  public float shakeDecay = 0.2f;
  public float intensity = .3f;

    private Vector3 originPosition;
  private Quaternion originRotation;
  private float shakeIntensity = 0;

   /* private void Awake()
    {
       cameraMovement = GetComponent<CameraMovement>();
        playerController = GetComponent<PlayerController>();
    }*/

    void Update() {
    if (shakeIntensity > 0) {
      transform.position = originPosition + Random.insideUnitSphere * shakeIntensity;
      transform.rotation = new Quaternion(
      originRotation.x + Random.Range(-shakeIntensity, shakeIntensity) * .02f,
      originRotation.y + Random.Range(-shakeIntensity, shakeIntensity) * .02f,
      originRotation.z + Random.Range(-shakeIntensity, shakeIntensity) * .02f,
      originRotation.w + Random.Range(-shakeIntensity, shakeIntensity) * .02f);
      shakeIntensity -= shakeDecay;
    }
  }

  public void Shake() {
        originPosition = transform.position;
        originRotation = transform.rotation;
        shakeIntensity = intensity;
        //transform.position = Vector3.Lerp(transform.position, cameraMovement.followTarget.transform.position, Time.deltaTime * playerController.moveSpeed);
    }
}

Also please advise on how to post code.

Thank you.

Hi @jrb187,
First about how to post code, if you want inline, you can simply use the backticks the character before the 1 key (on american keyboards) that looks like a reversed single quote → `
If you want to format code that is multiline, you need to first have a line space between the line of text and first line of code and then pad everything with 4 spaces, like so

test

however the next line if it does not have the spacing will read like normal text

cheers,

Jayant

1 Like

Thank you, @jayantvarma for your advisement on the formatting!

I’ve tested this project in numerous iterations and I’ve never ran into the issue that you described. My advice is to download the completed project and first verify that the behavior doesn’t exist. Next, compare your hierarchy with the hierarchy in the completed project. My guess is that you have a misplaced GameObject. Give it a shot and let me know how it turns out. Cheers!

1 Like

Thank you for the reply. I could not figure out where I went wrong. I moved forward onto the other projects but my plan is to circle back and try it again with a fresh start.

This topic was automatically closed after 166 days. New replies are no longer allowed.