[SOLVED] Ray Cast issue on a FPS project

i’m not using the bullet with the current shooting system, i was using it with rigidbody before. And no, bullet wasn’t moving with the weapon, it was an independent entity, i was instantiating it with the code. The bullet entity you see on the hierarchy was like a prefab in Unity. I was using to instantiate bullets from the target entity under each weapon. This is not how i’m gonna do now that i implemented the ray cast system

so this was designed to cast rays where you click with the mouse cursor on the screen? Ok then how can i adjust it to cast the rays where i point the crosshair? Instead of mouse cursor i will shoot rays with the crosshair

Then you can use something like @Tirk182 already shared, to use the center of the screen.

// get center of screen
var centerScreenX = this.app.graphicsDevice.width / 2;
var centerScreenY = this.app.graphicsDevice.height / 2;

// get start and end point
var start = this.camera.camera.screenToWorld(centerScreenX, centerScreenY, this.camera.camera.nearClip);
var end = this.camera.camera.screenToWorld(centerScreenX, centerScreenY, this.camera.camera.farClip);
    
// raycast between the two points and return the closest hit result
var result = this.app.systems.rigidbody.raycastFirst(start, end);
1 Like

How do you know it’s shooting to the ground?

if(result) {

        console.log("You Hit: " + result.entity.name);
        this.handleImpact(result.entity,result.point,result.normal);
 
    } else {

        console.log("Nothing Hit");
    }

};

ok, when i move too close to an object i can hit it. I hit this box when i shoot a cm to the left of it. But i have to be at certain distance to it. Not very far away

Is the collision size of the enemy correct?

yes

The crosshairs is in the center of the screen and you use the code I shared?

yes it works now but only when the object is not moving. I’ll look into that tomorrow. Thanks a lot for the help both of you @ Tirk182

1 Like

@Ronin I think that you will want to change the rigid body of the moving item to kinematic so the collision and rigid follow? @Albertos

2 Likes

Yes, if the enemy is not moved with forces, the rigidbody of it should be kinematic. Otherwise the rigidbody stays on the same place and is on another position than the visible enemy.

2 Likes

yepp, it worked. Thanks again!!

1 Like