Hello everyone,
I am trying to create a platform that when touched, boosts the player upwards x amount. I have looked at other posts as well as the documentation here and here.
Here is my code:
var Bouncing = pc.createScript('bouncing');
// initialize code called once per entity
Bouncing.prototype.initialize = function() {
this.entity.collision.on('collisionstart', this.onCollisionStart, this);
};
// update code called every frame
Bouncing.prototype.update = function(dt) {
if (result.other.rigidbody) {
this.entity.rigidbody.applyImpulse(0, 100 ,0);
}
};
// uncomment the swap method to enable hot-reloading for this script
// update the method body to copy state from the old instance
// Bouncing.prototype.swap = function(old) { };
// learn more about scripting here:
// https://developer.playcanvas.com/user-manual/scripting/
At line 10 (the if statement) I am getting a reference error saying that “result” is not defined. I am using the same syntax as the two documentation links, so I do not know what could be causing this error. Here is my project: https://playcanvas.com/project/1315187/overview/- (The scene I am referencing is “normal_arena” or 2192409). Any help is appreciated, thank you!
In this segment of code,
// update code called every frame
Bouncing.prototype.update = function(dt) {
if (result.other.rigidbody) {
this.entity.rigidbody.applyImpulse(0, 100 ,0);
}
};
you have the block of code set in the Update function, meaning it runs every frame. Firstly, replace update with onCollisionStart and secondly replace the dt in the function parameters with result and it will work.
3 Likes
Thanks so much! There is no longer an error, however now even after messing around with the parameters of the force, I am not noticing any effect. I thought I might have collisions and rigidbodies set up incorrectly, but I am pretty sure that everything is good. If there are any thoughts as to why that might be I would be super thankful.
Hi @CRAYPHiSHQUESTIONS!
Sharing your updated code or perhaps the editor link of your project will make it easier for users to help.
Silly mistake, sorry.
Here is the editor: https://playcanvas.com/editor/scene/2192409
and the updated code:
var Bouncing = pc.createScript('bouncing');
// initialize code called once per entity
Bouncing.prototype.initialize = function(result) {
this.entity.collision.on('collisionstart', this.onCollisionStart, this);
};
// update code called every frame
Bouncing.prototype.onCollisionStart = function(result) {
if (result.other.rigidbody) {
this.entity.rigidbody.applyImpulse(0, 500 ,0);
}
};
// uncomment the swap method to enable hot-reloading for this script
// update the method body to copy state from the old instance
// Bouncing.prototype.swap = function(old) { };
// learn more about scripting here:
// https://developer.playcanvas.com/user-manual/scripting/```
Looking at your scene, I guess your script should look like below. (You probably want to apply the impulse on the rigidbody of the player, which is result.other
, as this.entity
is the entity with the script).
var Bouncing = pc.createScript('bouncing');
Bouncing.prototype.initialize = function(result) {
this.entity.collision.on('collisionstart', this.onCollisionStart, this);
};
Bouncing.prototype.onCollisionStart = function(result) {
if (result.other.rigidbody) {
result.other.rigidbody.applyImpulse(0, 500 ,0);
}
};