Jump to content

Blender -> Camera follow object, and custom movement


malkomalko
 Share

Recommended Posts

Hello Forum!

 

Babylon, Blender, and Gamedev are all new to me, but I'm soaking up a lot and taking it all in stride.  I've been playing around with all 3 and learning a bunch from reading code (I'm mostly a developer/designer), but I was wondering if somebody could assist me in answering a few questions.

 

I figured out how to make a simple scene in Blender and assign physics to certain objects (camera included), so that I could load up the scene and walk around.  A few questions came out of this simple experiment that I'd love to try to figure out.

 

1) What's the best way to have the camera follow an object?  Would it be something like:  When you move the camera, also move the object by x/y/z depending on the direction of the movement.

 

2) How do you alter/override the default controls of the camera?  I find that the movement is a little to loose for my like and would like to alter the speed and play around with getting a feel of my character movement.  Would it be something like: Grab access to my camera objects, and then alter some combination of attributes?

 

Thank you very much, really really loving this stuff.

Link to comment
Share on other sites

You can attach a camera to a character with 'relative' in order for the camera to follow.

 

//Exemplecamera.parent = actor;
For the speed of the camera you can use speed.

 

camera.speed = 2;// The default speed is 1
Or if your camera is attached to the character, you can advance your character more quickly and the camera will follow.

 

Here is the code to move a character in all directions

var keys={letft:0,right:0,arriere:0,forward:0};window.addEventListener('keydown',function(event){    var ch = String.fromCharCode(event.keyCode);    if (ch == "Q") keys.left=1;     if (ch == "D") keys.right=1;    if (ch == "S") keys.arriere=1;    if (ch == "Z") keys.forward=1;}); window.addEventListener('keyup',function(event){    var ch = String.fromCharCode(event.keyCode);    if (ch == "Q") keys.left=0;    if (ch == "D") keys.right=0;    if (ch == "S") keys.arriere=0;    if (ch == "Z") keys.forward=0;}); scene.registerBeforeRender(function(){   if (keys.forward==1){	var posX = Math.sin(cube.rotation.y);	var posZ = Math.cos(cube.rotation.y);				cube.position.x += posX;	cube.position.z += posZ;			   }	   if (keys.arriere==1){	var posX = Math.sin(cube.rotation.y);	var posZ = Math.cos(cube.rotation.y);				cube.position.x -= posX;	cube.position.z -= posZ;			    }});
Link to comment
Share on other sites

Following both your suggestions I got an object moving.  Two more follow up questions since I couldn't figure it out while playing.

 

1) How do you turn off the default controls of the camera (mouse/left/right/up/down).

2) Now I need to figure out how to make my cube (player) have collision detection.  It says it does but it doesn't seem to work.

 

Either way, this is fun!

Link to comment
Share on other sites

1)You probably have the below some where in your code, just comment it out or remove it.  That will stop the mouse/left/right/up/down controls.

  scene.activeCamera.attachControl(canvas);

 

2)Are you using the below?

meshObject.intersectsMesh(otherMeshObject, true)

I am guessing you were looking at this tutorial for collisions: https://github.com/BabylonJS/Babylon.js/wiki/10-Object-collisions

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