Size of the platform

Hi,
I’m attempting to dynamically create a platform with infinite movement. I’ve been following the Infinity Runner Tutorial, and you can find the project here. The project is functioning as intended.

While going through the code, I noticed a part where the platform is removed when it reaches a certain point, and then it’s added to the start again using the following line:

x = this.parent.children[this.childCount - 1].getLocalPosition().x + 2;

Instead of adding a fixed value of 2 to the code, I’d like to add it to the size of the entity so that I can use the same code for a much wider platform.

Note: The model of platforms cannot be accessed using this.entity.model;

Thank you.

Hi @Kaverappa and welcome!

It sounds like you want to replace the fixed 2 with something like below.

platformEntity.getLocalScale().x

Not sure what you exactly mean with this.

platformEntity.getLocalScale().x

This actually shows the scale not the size, scale of different entity can be 1 but their size changes.

I have seen few post in forum where they use bounding box as in Calculate bounding box in a model.

This is not working for you?

No. this.entity.model is undefined

Maybe you use the new render component on your entities? If that’s the case you need to replace model with render in the script.

How do I use this to replace 2 in x = this.parent.children[this.childCount - 1].getLocalPosition().x + 2;?

If you could show me how to utilize that code for figuring out the entity’s size, that would be awesome.

When you have a bounding box, I expect it should be something like aabb.halfExtents.x.

1 Like

If you take a look at the project, you’ll notice that I’ve attached a script to the ground entity, which serves as the template. The code I use to obtain its size is as follows:

var meshInstances = this.entity.children[0].render.meshInstances;
if (meshInstances.length > 0) {
    var bbox = new pc.BoundingBox();
    bbox.copy(meshInstances[0].aabb);
    for (var i = 1; i < meshInstances.length; i++) {
        bbox.add(meshInstances[i].aabb);
    }
    this.size = bbox.halfExtents.x;
}

This approach may vary if the platform structure is different, such as when the render component is in the 2nd child or parent component. Is there a way to address this variation? Can I directly retrieve the size of the template?

A quick search on the forum brings me to the topic below.

I think this topic is very useful.

Thank you. I’ll give it a try.