Troubles disabling script on event:


#1

Hello i cant seem to stop my weapon script that gets my gun to shoot. what i want to happen is when my character is reloading i want him not be able to shoot while reloading here is what i was trying to do:

    var AudioEntity = this.app.root.findByName('reloadSound');
  if (this.app.keyboard.wasPressed(pc.KEY_R)) {
      this.setState('reload');
      AudioEntity.sound.play('reloadSound');
  }
    if (this.state === true) {
        this.script.gun.enabled = false;
    }

#2

I may not know what I’m talking about here, but don’t you have to write the functions setState yourself? I don’t think that’s an internal PlayCanvas function/method/feature.

Further, shouldn’t you be testing for this.state === ‘reload’ and not for true. Is this.state a boolean?

Anyway, I may not understand how you are using setState or this.state correctly, but for sure I’d toss a console statement into your if (this.stat === true) condition to see if I’m actually getting to that condition and if the script status has actually changed.

 if (this.state === true) {
        this.script.gun.enabled = false;
        console.log ('this.script.gun status', this.script.gun.enabled)
    }

#3

I agree with @wturber. Write setState yourself. Also, use a boolean variable to determine when the gun is shooting, and turn it to false when it is reloading. Which script is this is in the project? I’ll take a look…


#4

fortunately it is its own function @wturber here is the code:
https://playcanvas.com/editor/code/612040?tabs=19066251
if you look on lines 18-23 this is what you will see this:

if (this.app.keyboard.wasPressed(pc.KEY_R)) {
      this.setState('reload');
      isReloading = true;
      AudioEntity.sound.play('reloadSound');
      //this.script.gun.enabled = false;
  } 

so i looked around i created this isReloading = true in the AnimationBlending. But im calling on the gun script seen here
this is the code i implanted:

var isReloading = false;
 if(fireable && !isReloading){
                this.shoot();

                fireable = false;

            }

now it doesnt shoot when reloading but also doesnt shoot anymore after its done reloading


#5

Does !is Reloading work in ES5? Try is Reloading === true


#6

i believe it works because it did work but i need it to update to say that im done reloading but ill try what you said


#7

And did you get the laser to look at the player?


#8

No i cant seem to figure it out at all everytime i think i had it the laser still just changed its own position


#9

Let’s do the remaining two levels today. After that, we can both work on the shooter.


#10

Ok im fine with that i really want to see what happens when we enter this into the game jam


#11

Meet you in 10 minutes


#12

Most of the games are 2d and Pygame, so we have a good chance.


#13

I dont know about ten minutes im about to go somewhere for i dont know about 45 mins ill be back at about 11:05 US East time (am) and ill complete it then


#14

Sure, see u then Nathan


lookAt API question
#15

This is the super simple PointAt script I created for the purpose of having a pointer that could point at different parts of an item. This is a mod of the script used in the Hotspot demo project. The only complication/new thing is that the pointer also stretches to reach the item it was pointing. You should be able to apply this to any object and it will point the negative Z to whatever you specified. You may want to disable the scaling feature … though it can make it easier to see what is pointing to what because it will stretch the negative Z toward the target.

Maybe put this or the pointing script you are using into a simple project and toy with that a bit.

var PointAt = pc.createScript('pointAt');

PointAt.attributes.add("targetEntity", {type: "entity", title: "Target Entity"});


// initialize code called once per entity
PointAt.prototype.initialize = function() {
    
    this.defaultForwardDirection = this.entity.forward.clone();
    this.directionToTarget = new pc.Vec3();
};

// update code called every frame
PointAt.prototype.update = function(dt) {
    var targetPosition = this.targetEntity.getPosition();
    var pointerPosition = this.entity.getPosition();
    var pointerScale = this.entity.getLocalScale();

    var distance = pointerPosition.sub(targetPosition).length();
    pointerScale.z = distance;
    this.entity.setLocalScale(pointerScale);
    
    // Always face the target postition
    this.entity.lookAt(targetPosition);
        
   
};

#16

Thanks, we’ll try this. If it doesn’t work, could you please help us fix it? This is pretty urgent.


#17

That script probably contains the sum total of my knowledge on the subject. :^)


#18

No, I didn’t mean help us script it.

I meant could you please help us do this👆?


#19

I’ve made a super simple project called Gun Project that illustrates both methods.

I’ve made a Box that has a rectangular “barrel” oriented in the proper -Z direction but that also has a round “barrel” oriented in the wrong +X direction. Each of these boxes is parented to an “empty” or Null Entity.

I applied the pointAt.js to the box named “Box Proper -Z” and I’ve also applied a shifter.js to it so that you can move the box around with a right-press and drag of the mouse. I have set the pointAt Target to the conical target entity.

For the identical box and barrels labeled “Box Fixed with NULL” I have applied the same scripts to its master Null instead. I’ve then rotated “Box Fixed with NULL” by 90 degrees on the Y axis in its initial Editor settings. So the Null is still pointing its -Z axis at the cone Target because that’s what the lookAt function does. But because the box has been rotated 90 degrees, the round barrel that was pointing in the wrong direction is now pointing at the cone Target. This is the adjustment that fixes the “wrong” initial object orientation.

Right-click and drage and you can see that the boxes will point at the Target. You can change the targets to the Camera in Editor and see that the boxes and their barrels re-orient to point toward the camera. You can raise and lower the boxes and see that they stay pointing at the camera. This is hard to see because this orientation causes very subtle changes in perspective. But that is evidence that they are pointing at the camera as is the fact that the shadows shift as well.

This should be enough to illustrate the two methods, correct orientation and using a Null to adjust for an incorrect orientation.

https://playcanvas.com/project/612692/overview/gun-project

Note: I disabled the scaling feature of the pointAt.js for this demonstration project.


#20

Thanks a lot, you put our project back on track. @Nathan_Hawkins1, take a look.