Jump to content

[solved] Raycast around object


TomaszFurca
 Share

Recommended Posts

  • 2 weeks later...

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);

 

Link to comment
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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...