I’m trying to do a raycast from a cube. I’m using the function raycastFirst() so my line of code is:
var result = this.app.systems.rigidbody.raycastFirst(this.entity.getPosition(), pc.Vec3.DOWN);
It’s supposed to return a plane that it’s directly under the cube (actually it’s surrounded by planes in every direction for testing so it should hit any of them) but it always returns null. I already applied the rigidbody component to the planes.
I forgot that. I’ve just applied the collision component and configured their sizes too, but It still returns null. I only find tutorials that talks about doing raycasts from the mouse position and none from an entity, maybe my problem is that I’m using wrong raycast, and it only works with screen or sth.
Ok, I’ve just seen what’s the problem. I had those planes inside an entity I made earlier that it’s called Scene. I only made that entity for management because I didn’t want to have 100 or more planes in the hierarchy. It seems the raycast is hitting first this entity and it returns null, but I want to reach the planes below. Maybe I should try by changing the origin of the raycast
So changing your raycasts to something like this will work:
var resultDown = this.app.systems.rigidbody.raycastFirst(from, new pc.Vec3(from.x, from.y - 10, from.z));
Just note that normally you should initialize an empty pc.Vec3 at the start of your project and reuse that, instead of allocating a new Vec3 object for each raycast as I do here. This will avoid unnecessary calls to the garbage collector.
Omg, I can’t believe I didn’t notice that
I tried sth like that because at first I thought it was another point (end point) and not a direction, but since it wasn’t working changed my mind and I thought it was just the direction it goes.
Now the 3 planes change correctly, thank you so much for the help