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.
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ā¦
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:
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
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);
};
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.