[SOLVED] Cannot read properties of point

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?

Wild guess. The result of your raycasts in both scripts is a global variable? Are they clashing?

Is it a global variable? I thought global variables were blue and had uppercase letters in the beginning.

You can see by typing result in the browser devtools console. You should see that result or window.result is there. In your code try let result =

I used this.result, but now the raycasts from the explosion absolutely refuse to detect the targets. They detect everything else tho.

Did you write this.result in all the locations where you previously has just result? But it’s more correct to write let result.

I mean it detects all the walls and stuff, just completely ignores the actual targest and idk why that would be the case.

ALUCARDS LETHAL MUNITIONS EXPO | Editor

Here is the editor.

Is it maybe the same issue as in my test project?

Shoot at the enemy and you will see that the impacts are placed inside the enemy instead of where I expect the hit point to be.

No, because no matter where it hits it just never seems to contact the correct object. Could you perhaps give me some more information on that drawline stuff, or at least how to make the line thicker

It’s just a line between two points, like a raycast. You can change the color, but not the thickness.

I cant seem to see any line, am I doing something wrong or is it just really thin or something?

Gun.prototype.Shooty = function() {
    for(i=0;i<this.pellets;i++)
    {
        if (this.app.mouse.isPressed(pc.MOUSEBUTTON_RIGHT))
        {
            this.bulletentity.setLocalEulerAngles((Math.random() * this.accuracy) - (this.accuracy / 2), (Math.random() * this.accuracy) - (this.accuracy / 2), (Math.random() * this.accuracy) - (this.accuracy / 2));
        }
        else
        {
            this.bulletentity.setLocalEulerAngles((Math.random() * this.inaccuracy) - (this.inaccuracy / 2), (Math.random() * this.inaccuracy) - (this.inaccuracy / 2), (Math.random() * this.inaccuracy) - (this.inaccuracy / 2));
        }
        this.flash.children.forEach((enabloid) => {
            enabloid.enabled = true;
            enabloid.setLocalEulerAngles(0, 0, Math.random()*360)
        });
        anglePos = this.bulletentity.forward.clone();
        anglePos.scale(this.range);
        from = this.bulletentity.getPosition();
        this.app.drawLine(from,anglePos.add(from), pc.Color.WHITE, true, this.app.scene.layers.getLayerById(pc.LAYERID_WORLD));
		result = this.app.systems.rigidbody.raycastFirst(from,anglePos.add(from));
		if(result)
        {
            this.buleff = this.bulleteffect.clone();
            this.buleff.enabled = true;
            this.buleff.setPosition(result.point);
            Utils.setEntityDirection(this.buleff, result.normal);
            this.buleff.reparent(this.app.root);
		    if(result.entity.tags.has(this.enemytag))
            {
                    result.entity.name = ((parseFloat(result.entity.name)) - this.damage);
                    Globaldamage += this.damage;
                    //Grams += ((Math.floor(Math.random() * 6)) + 1);
		    };
        };
    }
};

Not sure if you do something wrong, but I suggest to try something like below.

var start = this.bulletentity.getPosition();
var end = this.bulletentity.forward.clone().scale(this.range).add(start);
this.app.drawLine(start, end);

How long does the line stay for, one frame or forever?

That depends on where you place it in code, so if you place it in the update function without any statement it will stay as long the script is alive.

So do you have any idea whatsoever why the rays are not interacting with my targets?

I just set the range to be 3 and moved very far away from every entity yet i somehow am still hitting entities names wall despite the fact I am nowhere near the walls. The explosion entity must be in the wrong place.

LIFE ADVICE FOR EVERONE:
Reparent or enable the entity AFTER moving it and NOT BEFORE or else BAD THINGS.

The problem can be everywhere.

If you want a result for a specific function only, make sure you create a temporary variable for it, like @Kulodo133 already mentioned.

Correct:

var result = this.app.systems.rigidbody.raycastFirst(start, end);

Wrong:

result = this.app.systems.rigidbody.raycastFirst(start, end);

I usually just use this.x

I disagree on this, because it depends on your use case.