so i am making an Fps game and I want there to be a mystery box you can buy items from.How do I make it so that when it is pressed and you have 950 points it does the random selection.So far I have this…
Trigger.prototype.onTriggerEnter = function(entity){
if(entity.tags.has('player')){
if(this.app.keyboard.isPressed(pc.KEY_F)){
this.Model.animation.play('Open');
var value = pc.math.random(11, 15).toString().slice(0, 2);
if (value == '11') {
this.Medkit.enabled = true;
}else
if (value == '12') {
this.EMP.enabled = true;
}else
if (value == '13') {
this.Milk.enabled = true;
}else
if (value == '14') {
this.Banana.enabled = true;
}else {
this.Medkit.enabled = false;
this.Banana.enabled = false;
this.Milk.enabled = false;
this.EMP.enabled = false;}
}}};
Hi @Connor_Briggs,
In the future, for easier readability, could you please paste your code inside of three backquotes like this
.
I see a lot of things going on here. I think it’s really important to think about when everything happens in your scripts. The first thing I see is that you are using the this.app.keyboard.isPressed()
function.
This function is really designed to be checked every frame. To get that portion of your code to work, you will want to move it to the update function. Since I don’t know exactly how you have your triggers set up, or how you’re firing them, the best I could recommend would be to have a boolean that flips between true and false when entering and exiting the trigger. Then in your update function you can check if that boolean is in the right place when the appropriate key is pressed before executing your item code.
I hope this is helpful!
1 Like
okie
okie thx i’ll try that
One question i don’t know how to make a boolean function
Trigger.prototype.onTriggerEnter = function(entity){
if(entity.tags.has('player')){
if(this.app.keyboard.isPressed(pc.KEY_F)){
this.Model.animation.play('Open');
var value = pc.math.random(11, 15).toString().slice(0, 2);
if (value == '11') {
this.Medkit.enabled = true;
}else
if (value == '12') {
this.EMP.enabled = true;
}else
if (value == '13') {
this.Milk.enabled = true;
}else
if (value == '14') {
this.Banana.enabled = true;
}else {
this.Medkit.enabled = false;
this.Banana.enabled = false;
this.Milk.enabled = false;
this.EMP.enabled = false;}
}}};
Hi @Connor_Briggs,
A boolean is not a function it’s just a type of object that is either true or false. Think about whenever you enable or disable something in one of your other projects:
this.entity.enabled = false;
The false
part is the boolean. The same is true for true
. Those two values are known together as a boolean. So when I say make a boolean, I mean something like:
var TestScript = pc.createScript('testScript');
TestScript.prototype.initialize = function() {
this.inTrigger = false;
this.entity.collision.on('triggerenter', this.onTriggerEnter, this);
this.entity.collision.on('triggerleave', this.onTriggerLeave, this);
};
TestScript.prototype.update = function(dt) {
//if this.inTrigger is true
if(this.inTrigger) {
//do stuff
}
}
TestScript.prototype.onTriggerEnter = function() {
this.inTrigger = true;
};
TestScript.prototype.onTriggerLeave = function() {
this.inTrigger = false;
}
You can see how I flip my boolean between true and false whenever something enters or exits the trigger.
I hope that’s helpful!
1 Like
ok thanks i’ll try that
So far this is really helpful