Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How to I fix my code?

So I am brand new to coding and I started to follow along with the Brackeys How To Make A Game tutorial. I also decided to make the game more interesting and add some camera shake when I collided with an object. I also used a Brackeys tutorial to do this. The camera shake itself works fine, the problem is that the shake will always happen on a certain part of my screen even when my player has moved to the right or left. Also, when I go into a new scene, it is not referenced and unity won't allow me to reference it in my collision script. It would really help if someone could help me fix it and if you could explain it simply (I only started coding a month ago) that would be great. I'll leave my code below.


using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class CameraShake : MonoBehaviour

{


    public IEnumerator Shake (float duration, float magnitude)

            {

                Vector3 originalPos = transform.localPosition;


                float elapsed = 0.0f;


                while (elapsed < duration)

                {

                    float x = Random.Range(-1f, 1f) * magnitude;

                    float y = Random.Range(-1f, 1f) * magnitude;


                    transform.localPosition = new Vector3(x, y, originalPos.z);


                    elapsed += Time.deltaTime;


                    yield return null;

                }


                transform.localPosition = originalPos;

            }

}

Answers

Sign In or Register to comment.