Hi, so I have got this far with making the camera shake when I die in my game:
app.on('game:cShake', function (x,y) {
this.item = app.root.findByName('Camera');
var rnd = Math.random(-0.0005, 0.0005);
this.item
.tween(this.item.getLocalPosition())
.to(new pc.Vec3(rnd / 100, rnd / 100, 1), 0.05, pc.Linear)
.repeat(5)
.yoyo(true)
.start()
.complete = true;
}, this);
But I want to make this where it will shake randomly between -0.005
and 0.005
to make a more real effect, instead of just yoyoing between 0.005
and back
Thanks for any help
Oh, I managed to do it. I used a different script and applied it to when I die
var CameraShake = pc.createScript('cameraShake');
CameraShake.attributes.add("shakeInterval", {type: "number", default: 0.1, title: "Camera Shake Interval"});
CameraShake.attributes.add("maxShakeDistance", {type: "number", default: 0.1, title: "Max Shake Distance"});
CameraShake.attributes.add("duration", {type: "number", default: 1, title: "Duration"});
// initialize code called once per entity
CameraShake.prototype.initialize = function() {
this.time = 0;
this.timeSinceLastShake = 0;
this.startPosition = this.entity.getPosition().clone();
// Listen to the event that will trigger the camera shake
this.app.on('camera:shake', this.onStartShake, this);
};
// update code called every frame
CameraShake.prototype.update = function(dt) {
this.time += dt;
if (this.time < this.duration) {
this.timeSinceLastShake += dt;
if (this.timeSinceLastShake >= this.shakeInterval) {
// Use this to reduce the maximum shake distance over the duration of the effect
var v = 1 - pc.math.clamp(this.time / this.duration, 0, 1);
// Find a point in a disc to offset the camera by
// Taken from http://stackoverflow.com/questions/5837572/generate-a-random-point-within-a-circle-uniformly
var t = 2 * Math.PI * pc.math.random(0, 1);
var u = pc.math.random(0, this.maxShakeDistance) * v + pc.math.random(0, this.maxShakeDistance) * v;
var r = u > 1 ? 2-u : u;
var x = r * Math.cos(t);
var y = r * Math.sin(t);
this.entity.setLocalPosition(this.startPosition.x + x, this.startPosition.y + y, this.startPosition.z);
this.timeSinceLastShake -= this.shakeInterval;
}
}
};
CameraShake.prototype.onStartShake = function () {
this.time = 0;
this.duration = 0.45;
};
3 Likes