[CLOSED] Collision not working on cloned entitites

I have this entity that I’m cloning to make new cubes. I position them and give them a material, all that works fine. But when I have to do a raycast from their position to other positions, the raycast doesn’t hit them, so I end up getting other results. I want to know if I have to add something else to the cloned entities for them to work.

The first one doesn’t count since it’s the one I’m cloning to generate the new ones.
In the position of y 0, it works fine, there’s nothing below the cube so the raycast hits the tile. But the cube in 4 1 49 (xyz) is hitting too the tile when it has a cube below. RaycastFirst only returns the first entity it hits. So I don’t get why it doesn’t return the cube when it has collisions (I checked too).

The code I’m doing for the raycast is

Builder.prototype.recolorTiles = function() {

    var gridCubes = this.app.root.findByTag("gridCube");
    for (let gridCube of gridCubes) {
        console.log('GridCube Position: ', gridCube.getPosition());
        let from = gridCube.getPosition();
        let to = new pc.Vec3(from.x, from.y - 8, from.z);

        var result = this.app.systems.rigidbody.raycastFirst(from, to);
        if (result) {
            console.log('From: ', from);
            console.log('To: ', to);
            console.log('Result from raycast: ', result.entity);
    // To make sure the first cube colors its tiles.
    this.colorTiles(this.grid[0][0][0].id, new pc.Vec3(5, 0, -1));

Hi @Visama,

Raycasting using rigidbodies is quite reliable in my experience. I don’t see anything wrong in your code, as long as your math is sound (from/to positions) and your entities have a collision and rigidbody component then it should work.

If you are still unable to resolve this, try sharing a sample project url to troubleshoot.

Hi Leonidas, this is the project url

If you find something that’s wrong it would help. I was trying with a new function I saw in documentation called raycastAll, but I got an error of undefined

Hi @Visama,

What are the steps to reproduce your issue? I am going to have a quick look.

Maybe you’ll have to clone them yourself by code because the game is played in an angular application that gives the game some specific data. This angular application also calls the start function so it each level it’ll go to builder class and call the method createLevel().

Hmm, you will have to provide me a quick way to reproduce your issue, sadly I don’t have the time to study all of your codebase.

I launch the project and I am here:

That’s why I said that you should clone then manually, like, fork the project if you can, and try to manually clone the gridCube.

The game doesn’t work on playcanvas, only in the angular application.

Ok, I cloned manually three grid cubes, what’s next? How do I test your recolorTiles() method?

One thing that may help with debugging raycasts to make sure the positions are correct, is the renderLine method of pc.Application. Something in the lines of:

// in the recolorTiles
this.from = from;
this.to = to;

// in update:
Builder.prototype.update = function() {
    this.app.renderLine(this.from, this.to, new pc.Color(1, 1, 0, 1));
1 Like

Might be worth trying my devtools script that can be used to render the collision volumes to ensure that they are where you think they are https://github.com/yaustar/yaustar.github.io/tree/master/playcanvas-devtools

1 Like