Smooth Camera Follow

Software: Away3D 4.x

Alex Bogartz, Sr. Member
Posted: 13 October 2011 11:17 PM   Total Posts: 216

Ok, it took me a while to get this working, but I thought I’d share a simple script a have your camera follow the character with easing from behind him.  I’ll just post the basic function, and you can fill in the constants.

private function moveCamera()
  
{
   
if (!cameraTarget)
    return;
   var 
desiredDist:Number 700;
   var 
radians:Number;
   
radians getRadians(cameraTarget.rotationY 180);
   var 
targetX:Number cameraTarget.position.Math.sin(radians) * radius;
   if (
cameraTarget.forwardVector.<0{
    radians 
getRadians(cameraTarget.rotationY );
   
}
   
var targetZ:Number cameraTarget.position.Math.cos(radians) * radius;   
   var 
dx:Number targetX camera.position.x;
   var 
dy:Number = (cameraTarget.position.1000) - camera.position.y;
   var 
dz:Number = (targetZ) - camera.position.z;
   var 
vx:Number dx CAMERA_SPEED 2;//this is set to .05
   
var vy:Number dy CAMERA_SPEED;
   var 
vz:Number dz CAMERA_SPEED 2;
   
   if (
vx MAX_CAMERA_SPEED || vx < -MAX_CAMERA_SPEED)
   
{
    vx 
vx ? -MAX_CAMERA_SPEED MAX_CAMERA_SPEED//max speed is 40
   
}
   
   
if (vy MAX_CAMERA_SPEED || vy < -MAX_CAMERA_SPEED)
   
{
    vy 
vy ? -MAX_CAMERA_SPEED MAX_CAMERA_SPEED;
   
}
   
   
if (vz MAX_CAMERA_SPEED || vz < -MAX_CAMERA_SPEED)
   
{
    vz 
vz ? -MAX_CAMERA_SPEED MAX_CAMERA_SPEED;
   
}
   
   camera
.position = new Vector3D(camera.position.vxcamera.position.vycamera.position.vz)
   
camera.lookAt(cameraTarget.position);
  

Let me know if this helps anyone!

   

Alex Bogartz, Sr. Member
Posted: 14 October 2011 05:06 PM   Total Posts: 216   [ # 1 ]

Refactored this a bit.  Now it extends the LookatController.  You can view it here:

http://code.google.com/p/away3d-smooth-follow-camera-controller/source/browse/#svn/trunk

Haven’t had time to fill out the Google Code page, but this should be enough to try it out!

   

Alex Bogartz, Sr. Member
Posted: 15 October 2011 12:19 AM   Total Posts: 216   [ # 2 ]

One last update.

I posted an example here (no preloader)

http://stage.makaimedia.com/away3d/bear/

It uses the polar bear demo, but with the smooth follow camera.

The code is all here:

http://code.google.com/p/away3d-smooth-follow-camera-controller/downloads/list

   

3dNewb, Sr. Member
Posted: 27 October 2011 10:03 AM   Total Posts: 105   [ # 3 ]

What is the “LookAtController” I cannot seem to find any reference to it in the API documentations.

   

Alex Bogartz, Sr. Member
Posted: 27 October 2011 03:59 PM   Total Posts: 216   [ # 4 ]

It can be found here in the github repo:

https://github.com/away3d/away3d-core-fp11/tree/master/src/away3d/controllers

   

3dNewb, Sr. Member
Posted: 27 October 2011 05:21 PM   Total Posts: 105   [ # 5 ]

Ah I see thanks for that link. Can I ask you maybe to help me understand this a bit? The Controller package is not in the Away3D that I downloaded from the Downloads section, so I must have been added later on. How do I get up to speed with new packages and classes (like these Controller classes)? Or should I just check out the git more often and look for changes?

   

Alex Bogartz, Sr. Member
Posted: 27 October 2011 05:26 PM   Total Posts: 216   [ # 6 ]

Yeah, that’s what I do.  I notice ... to my annoyance grin ... that the classes are refactored quite often.  So it’s best to grab that github repo or at least subscribe to the RSS feed to track major changes.

   

3dNewb, Sr. Member
Posted: 27 October 2011 05:36 PM   Total Posts: 105   [ # 7 ]

I’ll try and do that then, thanks smile

   

Hector, Sr. Member
Posted: 03 November 2011 11:36 AM   Total Posts: 137   [ # 8 ]

Thanks for sharing Alex, I probably use it in the future.

   

cosmicwiz, Newbie
Posted: 20 February 2012 06:57 AM   Total Posts: 6   [ # 9 ]

Hi, I tried to use this code for my own third person view but somehow I always end up with my scenery upside down. Please note that my scenery has Z-axis pointing up (so previously I used to do camera.rotationX = 90 for the plain FPS view). I can’t seem to figure out how to rotate the camera so that I get the z-axis back pointing up (not down).

I am total newbie and this 3d stuff is driving me nuts. Is there a good tutorial I can follow to understand how lookAt is supposed to behave?

Thanks!

   

Alex Bogartz, Sr. Member
Posted: 20 February 2012 09:34 PM   Total Posts: 216   [ # 10 ]

If your models are loading in rotated, I think you may want to rotate them all correctly as soon as they load or in your 3D software.  For the lookAt function, I think it just rotates the camera to face whatever Vector3D you specify.  So if your object’s position is not in the center of the object (if it is offset in your model) this will throw things off.

I am using Blender, and I am very careful about applying all transforms and centering things before I export and this seems to solve a lot of problems.

   

cosmicwiz, Newbie
Posted: 21 February 2012 03:12 AM   Total Posts: 6   [ # 11 ]

Thanks Alex. After some debugging, I replaced “super.update()” with “targetObject.lookAt(lookAtObject.position, Vector3D.Z_AXIS);” in the CharacterFollowController’s update function and now things work like a charm wink

   

Alex Bogartz, Sr. Member
Posted: 21 February 2012 03:59 PM   Total Posts: 216   [ # 12 ]

Nice!  When you get something going, please post a link so I can see what you did with it!

   

Pagsli, Newbie
Posted: 07 November 2013 09:54 AM   Total Posts: 4   [ # 13 ]

Hi!
Unfortunatly this extension doesn’t work with new Away version.

There is a problem in line 34:
override public function update():void

I’ve fixed with:
override public function update(interpolate:Boolean = false):void

But the Controller doesn’t work yet! : (

   
   
‹‹ hello

X

Away3D Forum

Member Login

Username

Password

Remember_me



X