Sign in to follow this  
TomaszFurca

[solved] Raycast around object

Recommended Posts

Hi, i want create visibility area of my enemies, when he saw any player he go to player. I choose raycast, is good choice? But i have problem with raycast arround enemy object, how can i do that?

Tom

Share this post


Link to post
Share on other sites

Its done and works perfect:

let mainMesh = BABYLON.MeshBuilder.CreateBox('enemy', { width: 0.5, size: 0.7}, game.scene);

mainMesh.position = new BABYLON.Vector3(Game.randomNumber(3,-10), 1.1, Game.randomNumber(-10,-16));
        mainMesh.visibility = false;
        mainMesh.physicsImpostor = new BABYLON.PhysicsImpostor(mainMesh, BABYLON.PhysicsImpostor.BoxImpostor, { mass:1, friction:0.01, restitution:0.2}, game.scene);

        let attackArea = BABYLON.MeshBuilder.CreateBox('enemy_attackArea', { width: 1.7, height: 0.1, size: 1.7}, game.scene);
        attackArea.parent = mainMesh;
        attackArea.visibility = false;

        let visivilityArea = BABYLON.MeshBuilder.CreateBox('enemy_visivilityArea', { width: 12, height: 0.1, size: 12}, game.scene);
        visivilityArea.parent = mainMesh;
        visivilityArea.visibility = false;

and afterRender in variable to unregister, after dispose object.

this.afterRender = function() {
            if(game.player) {
                if (self.visibilityArea.intersectsMesh(game.player.character.mesh, false)) {
                    self.character.mesh.lookAt(game.player.character.mesh.position);

                    var playerMesh = game.player.character.mesh.getChildMeshes()[0];
                    if(self.attackArea.intersectsMesh(playerMesh, false)) {
                        self.character.runAnimationHit();

                        if (self.character.items.weapon.intersectsMesh(game.player.character.mesh, false)) {
                            playerMesh.material.emissiveColor = new BABYLON.Color4(1, 0, 0, 1);

                            var value = game.guiElements.hpBar.getValue();

                            game.guiElements.hpBar.updateValue(value - 0.2);

                            if(value-0.1 < 0) {
                                window.location.reload();
                            }
                        } else {
                            playerMesh.material.emissiveColor = new BABYLON.Color4(0, 0, 0, 0);
                        }

                    } else {
                        var velocity = BABYLON.Vector3.TransformNormal(new BABYLON.Vector3(0, 0, -0.041), self.character.mesh.computeWorldMatrix());
                        self.character.mesh.moveWithCollisions(velocity);
                        self.character.runAnimationWalk();

                    }
                }
            }
        };
        game.scene.registerAfterRender(this.afterRender);

 

Share this post


Link to post
Share on other sites

Excellent solve, thx for sharing.

I found another post about shooting a ray from wandering enemy -> player, and if it hits a wall, then enemy cannot "see" player.  Some links to pathfinding systems are there, too.

http://www.html5gamedevs.com/topic/27889-solved-solutions-for-virtual-joystick-camera/?do=findComment&comment=160588

Not sure if you need this or not.  Might be fun feature to add to game.

Be sure to mark this thread as [solved] if you wish (edit 1st post, change title).  But if you are still waiting for more replies, no need to mark as solved, of course.

I just wanted to pass-on something that might interest you.  Be well.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.