Jump to content

How to move a character in a scenario


hit2501
 Share

Recommended Posts

I just want to ask if anyone knows where can I find a tutorial or something similar to make a character move within a stage.

 

I have already made the stage and the character is imported from blender with animation (walking) and I can make him walk but stays in the same place.

 

I tried to follow the advices in:


 

but is advanced for me.

 

 

Thank you very much.

Link to comment
Share on other sites

And here:  http://playground.babylonjs.com/#2CPTBX  (wasd keys)  You might need to click on the canvas, first. 

This is a job for the world famous mesh.moveWithCollisions() function.  :)  You'll see it a couple times, in the code.  But I'm missing a transform after the rotations.  SOMETHING like this:

var invMatrix = new BABYLON.Matrix();
box.getWorldMatrix().invertToRef(invMatrix);
var direction = BABYLON.Vector3.TransformNormal(new BABYLON.Vector3(0, 0, 1), invMatrix);
direction.normalize();

That's not correct, but its possibly in the ballpark.  After a rotation, a new forward/backward direction must be established and applied.  I don't know how, yet.  And even once I do have a direction, I don't know what the hell to do with it.  :)

As you can see in the demo... after a rotation, the forward/backward translations are still worldSpace +/- z-axis.  They should probably be LOCALspace +/- z-axis.  There are others nearby that can fix my demo in a heartbeat, and then you'll be ready to do some character walking!  yay!

Link to comment
Share on other sites

I tried with the example of Wingut and works fine with animations imported from Blender but I only can move the character and after that the animation runs, is there any way to do both at once?

 

Here's the code Im using (I put the animations in case "W" to go forward and case "S" to move back):

BABYLON.SceneLoader.ImportMesh("", "Babylon/", "policewoman01.babylon", scene, function (newMeshes, particleSystems, skeletons) {                var policewoman = scene.getMeshByName("policewoman");                //policewoman.position.y = .51;                policewoman.position = new BABYLON.Vector3(0, 0, 0);                // Our built-in 'ground' shape. Params: name, width, depth, subdivs, scene                var ground = BABYLON.Mesh.CreateGround("ground1", 6, 6, 2, scene);                //camera.position = policewoman.position;                var moveVector = new BABYLON.Vector3(0, 0, 0);                 var movestep = .05;                var rotstep = .05;                var onKeyDown = function(event) {                    var key = event.keyCode;                    var ch = String.fromCharCode(key);                    switch (ch) {                        case "W":                            moveVector.z = movestep;                            policewoman.moveWithCollisions(moveVector);                            scene.beginAnimation(skeletons[0], 0, 30, false, 1.0);                            break;                        case "A":                            policewoman.rotate(BABYLON.Axis.Y, -rotstep, BABYLON.Space.LOCAL);                            break;                        case "S":                            moveVector.z = -movestep;                            policewoman.moveWithCollisions(moveVector);                            scene.beginAnimation(skeletons[0], 31, 60, false, 1.0);                            break;                        case "D":                            policewoman.rotate(BABYLON.Axis.Y, rotstep, BABYLON.Space.LOCAL);                            break;                    }                };                var onKeyUp = function(event) {        /*            var key = event.keyCode;            var ch = String.fromCharCode(key);            switch (key) {                case 16:                    speed = 5;                    break;            }        */                };                document.addEventListener("keydown", onKeyDown, false);                document.addEventListener("keyup", onKeyUp, false);        });        scene.registerBeforeRender(function() {        /*            moveVector.z += step;            box.moveWithCollisions(moveVector);            if (box.position.z > 2 || box.position.z < -2) {                step *=-1;            }        */        });        return scene;    };                var scene = createScene();        engine.runRenderLoop(function () {            scene.render();        });        window.addEventListener("resize", function () {            engine.resize();        });    </script>

Sorry for the inconvenience, this is something that interests me a lot. :)

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...