What is this? I searched the docs and only found one mention that didn’t really explain what it is and nothing in the forums.
What I’d like to do is create some -visible- indication on the screen when a collision occurs. I’m playing around with the “noise image effect” and am wondering if there is a way to trigger this momentarily (change it’s “grain size” for a second or two) and add some momentary shake to the camera. While searching around for info I ran into this “Window Shake” and wondered what it was. Anyone?
It’s the coolest thing. It reads the change in the window position of the Unity player (or Dashboard Widget). Download either the original WidgetMonkey or the WidgetGiraffe from widgetmonkeys.com to get a feel for it.
Yes, that is very cool indeed. I’ve never seen that capability in any game engine before, or any application of any type for that matter.
But it’s not what I was looking for. I’m guessing the best approach will be to script a bit of horizontal movement in the camera and at the same time boost the noise about 4x to simulate a video camera that’s been bumped. I’ll see what I can come up with…
Especially the one about animating position with perlin noise.
I think with some work you could package that up and tweak it into some very realistic camera shake, then add in the post processing noise on top of that using more perlin noise.
Hmm… sounds a bit more sophisticated that what I think I need (although I may feel differently after giving it a try). All I want to do is make the camera wobble for a second (maybe less) in response to a collision. Right now I’m triggering a sound, but I’d like to experiment with “bumping” the camera and adding a couple seconds of “video static”.
The camera is a child of the vehicle (I know this is not usually the right way to do it, but in this case it works nicely), so maybe just a canned animation? I’ll have to experiment a bit…
Seemed like a good idea so I gave it a try. After setting the spring and damper settings -all- the way up (over 1 million each) the effect came close to what I’m looking for, but it still wasn’t right. If you’ve seen a video camera when it’s bumped, the image is disoriented and the “signal” becomes broken for a fraction of a second.
Perhaps it’s my lack of understanding of the spring joint, but the other problem I had was that the camera wouldn’t turn with my main object (tried every “anchor X, Y, Z” setting I could think of).
I’m looking at the “head bobbing” script to get an idea what is involved in just -moving- the camera with script. Fortunately, it’s written in Javascript so I’m able to follow what it’s doing.
I also tried the “noise image effect” on one camera. I think the entire effect might be done with this, perhaps with a very large “scratch”, but the script drops the framerate by about 8-10 fps… ouch. I’m not sure why the frame rate drops so much, even if all the variable settings are set to zero?
There’s still other things I can try. I might try “blanking” the camera or “flashing” by rapidly changing the camera background color.
Was the Connected Body of the camera’s spring joint set to the main object? If it’s None, then it’s connected to the world. The Anchor X, Y, Z stuff is for what axis the spring joint rotates around. That said, I was trying something like this a week or two ago, and eventually gave up and went with scripting instead. Maybe you’ll have better luck though. I ended up not being sure if I actually want this effect, but here’s what I came up with so far:
private var isHit : boolean = false; // If camera has been hit or not
private var hitTime : float = 0; // Used for timer reference point
private var tiltHitTime : float = 0; // Used for timer reference point...not reset each cycle
private var originalPosition : float = 0; // Used for X axis position reference point
private var originalTilt : float = 0; // Used for z axis rotation reference point
var startingWobbleDistance : float = .8; // Total distance the object travels over a wobble cycle, in meters
private var wobbleDistance : float = 0; // Wobble distance for a given cycle
var distanceDecrease : float = .5; // The percentage of wobble distance for each cycle compared to the previous one
var wobbleSpeed : float = 55; // Increase to make wobbling faster
var numberOfWobbles : int = 3; // How many times the object wobbles, total
private var wobbleNumber : int = 0; // Keeps track of current wobble number for the entire sequence
var tiltAmplifier : float = 1.8; // Value to multiply the camera tilt by
function Update () {
if (Input.GetButtonDown("Fire2") ) {
isHit = true;
hitTime = Time.time; // Get the current time so we can have a reference point later
tiltHitTime = Time.time;
originalPosition = transform.localPosition.x; // Also get current position rotation for a reference point
originalTilt = transform.eulerAngles.z;
wobbleNumber = numberOfWobbles;
wobbleDistance = startingWobbleDistance;
}
if (isHit) {
// Make timers always start at 0
var timer : float = (Time.time - hitTime) * wobbleSpeed;
var tiltTimer : float = (Time.time - tiltHitTime) * wobbleSpeed;
transform.localPosition.x = originalPosition + Mathf.Sin(timer) * wobbleDistance;
// Make camera tilt only do 1/2 sine wave cycle over entire animation
transform.eulerAngles.z = originalTilt + Mathf.Sin( tiltTimer / (numberOfWobbles * 2) ) * tiltAmplifier;
// See if we've gone through an entire sine wave cycle, reset distance timer if so and do less distance next cycle
if (timer > Mathf.PI * 2) {
hitTime = Time.time;
wobbleDistance *= distanceDecrease;
wobbleNumber--;
// If we've done all the wobbles, then stop it with the wobbling already,
// and make sure we go back to the exact position we started in
if (!wobbleNumber) {
isHit = false;
transform.localPosition.x = originalPosition;
transform.eulerAngles.z = originalTilt;
}
}
}
}
Right now you see the effect by right-clicking…obviously that should be changed to something useful. It needs some work, but maybe somebody can use it for something. For example, the camera tilt only goes in one direction, and ideally it would go one way or the other depending on which side hit. That actually would be fairly simple to implement, but like I said I’m not sure if I’m going to use the effect at all so i stopped working on it.
You could always put an alpha vertex lit plane right it front of the camera, with a noise texture on it (like TV snow) at something like 50% transparency, then animate the noise by putting a random value in the X and Y texture offset every frame. Should result in a similar effect without any large performance hit.
You’re a genius! That first script is exactly what I was trying to achieve. Very nicely done! Thanks!!
Yup, for some reason I wasn’t able to get the Anchor X, Y, Z settings to restrict rotation around the Y axis no matter what values I used.
Yes, this was exactly what I was thinking also, although I hadn’t thought about animating the texture’s offsets. I was thinking of just animating the opacity and perhaps flip through a couple of different images.
This is beginning to be just -too- much fun! Thanks!
Oh, question: does anyone know what units the window shake is measured in? Same as the mouse, I presume, but what does that translate to, on the screen? I’m trying to make a windowed environment which can be shaken, but I’d really like to map window movement perfectly to viewport movement. And idea how I could accomplish this?
It’s pixel based window movement multiplied by sensitivity of the input axis.
The mouse delta however is physical movement of the mouse gotten from HID. (normal pixel based mouse delta is accelerated and lower precision). The unaccelerated mouse delta is high precision and feels much nicer. This is very visible in first person shooters.
To make things more compilcated, there are people who have installed USB overdrive, which hijacks the hid mouse input and eats it, so it doesn’t come through to Unity anymore. We worked around that by falling back to pixel based mouse delta. (We tried to match the two very closely, but it is slightly different)
So mouse delta is not always the same on every machine and it depends on if usb overdrive is installed.
To sum up, if you want to match window movement and mouse delta 100% exactly, then your best shot is calculating mouse delta yourself by the difference of this and the last frames mousePosition. If it doesnt need to be 100%, then simply adjusting the sensitivity of the window shake input axis should get you pretty close.
Cool. It’s good to know these things. Is it in the documentation anywhere?
Also, I’m more concerned with matching the game world to the window, pixel for pixel. I’m using an orthographic camera, and only two dimensions. The idea is that I want to have window movement emulate moving the game world. I’m actually just making a demo for myself with a window full of objects that I can shake around. In order for it to feel right, the movement has to be matched perfectly.
Before I go and make all sorts of conversion calculations I don’t need to, what is the most straightforward way I can accomplish this? Right now I’m using this:
• The window doesn’t map directly to object location. Not only is the scale wrong, but moving the window around and putting it exactly where it started does not cause the object to come back to where it started. Note that this is without a rigidbody attached.
• Movement is, of course, instantaneous. This means that the object does not impart force because it never has any velocity. How can I get around this? I tried adding a rigidbody and making the window shake axes affect rigidbody.velocity directly, but that was very laggy and worsened the above problem of mapping.
You need to tweak things depending on the scale, and I quadrupled the physics update to reduce objects moving through each other. It looks pretty good.