I have created a Shake script which will shake whatever transform it is attached to. However, it only shakes along a single plane. That is e.g. XZ plane.
I have a orthographic camera which is rotated 90 deg downwards to look at the XZ plane. When I attached the script to the Camera it behaves in an unexpected way. Instead of shaking in the XZ plane is shakes in the XY or ZY plane. This was weird. I realized that it is due to the camera rotation. So I fixed it by adding the camera to an empty GameObject which had no rotation.
This fixed the issue. But I would like to know if I could do this without involving another GameObject. Here is my code
Unless I misunderstood the question, the important line in the script is this one:
Vector3 newPos = new Vector3(Random.Range(-Magnitude,Magnitude), 0.0f, Random.Range(-Magnitude,Magnitude));// XZ Plane
It’s creating a new Vector3 with random values (within a specified range) for the X and Z components. I’d say you could just change that to randomize the appropriate components of the vector (whatever those are) to get what you need.
Well, if you already has a script that modify the camera position like a following script, you can fuse the two script and activate shaking only when you wants, or else, the camera position will be overwritten by the following script for example.
I tried all the components but still seem to be moving on the WORLDs XY plane (.I.e. Up and down) (Fix was to add the camera to another GameObject.
–EDIT–
And add the script to this GameObject
– END EDIT–
I just need to know what is the reason behind this.
Thanks guys…! But my question is not about how to do a Cam Shake. But why does the cam shakes in the world XY plane (After a rotation is applied) when instead of XZ plane is given as the components in the Vector3?
@Kurt Dekke: Thanks for the code snippet. I am new to vector math (more or less incompetent). Can you explain why you do a rotation and then add it to position? Thanks again