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());
//console.log(gridCube);
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));
};
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.
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
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().
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));
}