Troubles disabling script on event:

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;
    }

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)
    }

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ā€¦

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

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

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

1 Like

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

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

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

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

1 Like

Meet you in 10 minutes

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

1 Like

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

Sure, see u then Nathan

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);
        
   
};
1 Like

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

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

No, I didnā€™t mean help us script it.

I meant could you please help us do thisšŸ‘†?

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.

1 Like

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