For some reason whenever I fire the rpg (which has the temp model of a grenade launcher), the game fails to place an object at the result point. This only occurs sometimes, and it only occurs when the entity has the explosion script.
This is the rocket code:
var Rocket = pc.createScript('rocket');
Rocket.attributes.add("speed", {
type: "number",
description: "Speed of bullet in meters per second, can be any speed imaginable as the bullet will always hit",
});
Rocket.attributes.add("buffer", {
type: "number",
description: "Buffer distance that the lastpos entity will translate backwards away from the bullet every check, this makes sure the raycast is large enough to hit, usually leave at 0.1 or something like that",
});
Rocket.attributes.add("lastpos", {
type: "entity",
description: "Empty entity used to calculate distance between current bullet position and last bullet position, make it sibling of this entity",
});
Rocket.attributes.add("explosion", {
type: "entity",
description: "Entity that will be cloned and placed at hit position, remove this attribute if you are not going to use it",
});
Rocket.attributes.add("avoidtag", {
type: "string",
description: "If the bullet hits an entity with this tag it will ignore it, usually set to player to prevent blowing up inside player collision",
});
Rocket.prototype.initialize = function() {
this.timer = 0;
this.lastpos.setLocalPosition(this.entity.getPosition())
};
Rocket.prototype.update = function(dt) {
this.timer += dt;
this.entity.translateLocal(0*dt, this.speed*dt, 0*dt);
result = this.app.systems.rigidbody.raycastFirst(this.lastpos.getPosition(), this.entity.getPosition());
if(result)
{
console.log("HIT " + result.entity.name)
if(result.entity.tags.has(this.avoidtag))
{
Junkvar = 0;
}
else
{
this.splodey = this.explosion.clone();
this.splodey.reparent(this.app.root);
this.splodey.setPosition(result.point);
this.entity.parent.destroy();
//this.lastpos.destroy();
}
}
this.lastpos.setPosition(this.entity.getPosition());
this.lastpos.translateLocal(0, -0.1, 0);
if (this.timer >= 3)
{
this.splodey = this.explosion.clone();
this.splodey.reparent(this.app.root);
this.splodey.setPosition(this.entity.getPosition());
this.entity.parent.destroy();
console.log("HIT NOTHING")
}
};
This is the explosion code:
var Explosion = pc.createScript('explosion');
Explosion.attributes.add("damage", {
type: "number",
});
Explosion.attributes.add("soundplayer", {
type: "entity",
});
Explosion.attributes.add("pellets", {
type: "number",
});
Explosion.attributes.add("enemytag", {
type: "string",
});
Explosion.attributes.add("range", {
type: "number",
});
Explosion.prototype.initialize = function() {
this.killme = 0;
for(i=0;i<this.pellets;i++)
{
this.entity.setLocalEulerAngles((Math.random() * 360), (Math.random() * 360), (Math.random() * 360));
anglePos = this.entity.forward.clone();
anglePos.scale(this.range);
from = this.entity.getPosition();
result = this.app.systems.rigidbody.raycastFirst(from,anglePos.add(from));
if(result)
{
if(result.entity.tags.has(this.enemytag))
{
result.entity.name = ((parseFloat(result.entity.name)) - this.damage);
Globaldamage += this.damage;
};
};
}
this.soundplayer.sound.play("sound");
};
Explosion.prototype.update = function(dt) {
if (this.killme > 0)
{
this.entity.parent.destroy();
}
this.killme += 1;
};
Remember that error is the rocket code not reading the properties of the point while placing the entity, but only when the explosion code is enabled. I just disable it on the entity and it works 100%. Why?