If the distance to the door is less than 5. If I press the E key, the door will rotate to a certain value.
How would I do that? It will also play a sound.
You can do that by checking in the update function if a key has been pressed and if it has how far you are from the door:
here is an example.
var DoorOpener = pc.createScript('doorOpener');
DoorOpener.attributes.add('playerEntity', {type: 'entity'});
DoorOpener.attributes.add('timeToOpen', {type: 'number', default: 1.0});
// update code called every frame
DoorOpener.prototype.update = function(dt) {
if (this.app.keyboard.wasPressed(pc.KEY_E)){
let door_position = this.entity.getPosition();
let player_position = this.playerEntity.getPosition();
let distance = door_position.distance(player_position);
if(distance < 5.0){
this.is_opening = true;
this.opening_anim_time = 0.0;
}
}
if (this.is_opening == true){
if(this.opening_anim_time/this.timeToOpen > 1.0){
this.is_opening = false;
}
let opening_angle = pc.math.lerp(0, 90, this.opening_anim_time / this.timeToOpen);
this.entity.setLocalEulerAngles(0, opening_angle, 0);
this.opening_anim_time += dt;
}
};
how would you make it so if it is open and the E key is pressed it would close?
Never mind here is the script
var DoorOpener = pc.createScript('doorOpener');
DoorOpener.attributes.add('playerEntity', {type: 'entity'});
DoorOpener.attributes.add('timeToOpen', {type: 'number', default: 1.0});
var open;
// update code called every frame
DoorOpener.prototype.update = function(dt) {
if (this.app.keyboard.wasPressed(pc.KEY_E)){
let door_position = this.entity.getPosition();
let player_position = this.playerEntity.getPosition();
let distance = door_position.distance(player_position);
if(distance < 5.0){
this.is_opening = true;
this.opening_anim_time = 0.0;
}
}
if (this.is_opening == true){
if(this.opening_anim_time/this.timeToOpen > 1.0){
this.is_opening = false;
}
let opening_angle = pc.math.lerp(0, 90, this.opening_anim_time / this.timeToOpen);
console.log(opening_angle);
this.entity.setLocalEulerAngles(0, opening_angle, 0);
this.opening_anim_time += dt;
}
if(this.app.keyboard.isPressed(pc.KEY_E)){
this.open = true;
}
if(this.open == true && this.app.keyboard.isPressed(pc.KEY_F)){
let opening_angle = pc.math.lerp(0, 0, this.opening_anim_time / this.timeToOpen);
console.log(opening_angle);
this.entity.setLocalEulerAngles(0, opening_angle, 0);
this.opening_anim_time += dt;
}
};
When the player is a certain distance from the door in this case 5, if the E key is pressed the door will rotate to a given value. If the door is open though and the E key is pressed the door would close.
Ok so when I am a distance of 5 from the door entity, when I press E, the door opens. if the door though is open and I press E again the door would close.
You shared the script for this above as far I can see, so what’s your question at the moment?
Error so far:
I can close the door from any distance when I need to between a distance of 1 and 5 from the door
When I try to close the door it does not animate to close. It just sets it’s rotation I want it to animate shut
I want to be able to use E also to close not just open the door
I don’t see a distance check for closing the door, so this is expected. You can use the same method as for opening the door.
I guess this.opening_anim_time
is not reset to 0
. You can add some console.log('check 1');
and check the console of your browser to see if your script is working as intended.
var DoorOpener = pc.createScript('doorOpener');
DoorOpener.attributes.add('playerEntity', {type: 'entity'});
DoorOpener.attributes.add('timeToOpen', {type: 'number', default: 1.0});
var open;
// update code called every frame
DoorOpener.prototype.update = function(dt) {
if (this.app.keyboard.wasPressed(pc.KEY_E)){
let door_position = this.entity.getPosition();
let player_position = this.playerEntity.getPosition();
let distance = door_position.distance(player_position);
if(distance < 5.0){
this.is_opening = true;
this.opening_anim_time = 0.0;
}
}
if (this.is_opening == true){
if(this.opening_anim_time/this.timeToOpen > 1.0){
this.is_opening = false;
}
let opening_angle = pc.math.lerp(0, 90, this.opening_anim_time / this.timeToOpen);
console.log(opening_angle);
this.entity.setLocalEulerAngles(0, opening_angle, 0);
this.opening_anim_time += dt;
}
if(this.app.keyboard.isPressed(pc.KEY_E)){
this.open = true;
}
if(this.open == true && this.app.keyboard.isPressed(pc.KEY_F)){
let opening_angle = pc.math.lerp(0, 0, this.opening_anim_time / this.timeToOpen);
console.log(opening_angle);
this.entity.setLocalEulerAngles(0, opening_angle, 0);
this.opening_time = 0;
}
};
this good
What is the difference?
at the bottom I set the rotation time to 0
this.opening_time = 0;
Where is it used in your script? Shouldn’t that be this.opening_anim_time = 0
?
You need to try to read your code to be able to apply it on the right place.
at the very bottom in the script given above.
Yes, I see you set this.opening_time
to 0
at the bottom of your script, but where is this.opening_time
used in your script?
the middle of it
Can you show it please? I can’t find it.
It is at the top of the script on the 4th line of code also I made a mistake it is opening_anim_time
let opening_angle = pc.math.lerp(0, 90, this.opening_anim_time / this.timeToOpen);
console.log(opening_angle);
this.entity.setLocalEulerAngles(0, opening_angle, 0);
this.opening_anim_time += dt;
}
if(this.app.keyboard.isPressed(pc.KEY_E)){
this.open = true;
}
if(this.open == true && this.app.keyboard.isPressed(pc.KEY_F)){
let opening_angle = pc.math.lerp(0, 0, this.opening_anim_time / this.timeToOpen);
console.log(opening_angle);
this.entity.setLocalEulerAngles(0, opening_angle, 0);
this.opening_time = 0;
}
};
That’s what I already mentioned a couple of post above. You need to read your code carefully to be able to create a working script.
Did you already tried this?