Learn how to use a spring camera to create a 3rd person view in Away3D.
In the last tutorial we saw how to use the hover camera to get a view of a static object. Here we will look at the spring camera, which is useful when you want to follow a moving object. It allows a 3rd person view of a target, much like the view you would see in World Of Warcraft.
Player.as
In order to show off the spring camera we need a player mesh that can move around. For this we will make use of the MD2 mesh from this tutorial, terrain rendering from this tutorial, and the terrain following using the ElevationReader class from this tutorial. The Player class puts all these aspects together to create an avatar that can run around on some terrain.
First we load and texture the MD2 mesh. Note that we supply the scale to the MD2 parse, instead of using the scaleX, scaleY and scaleZ properties of the mesh that is returned. This is because scaling the mesh after it has been parsed modifies the transformation matrix, which is used by the spring camera. So if the mesh was scaled by 0.1, all the values passed to the spring camera would have to be 10 times larger to compensate. Having the mesh returned already scaled avoids these issues. [code]
We then supply the mesh to the base startupObject3DMeshObject function. [code]
The other functions, like setAnimation and keyDown/keyUp are only slight modifications of the same functions in the previous tutorials.
The initial animation is set to “stand”. [code]
We setup the ElevationReader. [code]
Now we create the spring camera. [code]
Unlike most Away3D classes, the spring camera does not read values from an init object passed into the constructor. This is why each property is set individually.
The positionOffset property defines the ideal position of the camera, relative to the object it is targeting. Here we set the camera to view the mesh from slightly above and to the right. [code]
The dampening property defines how “springy” the camera will be. [code]
The stiffness property defines how hard the camera is to move. The higher the value the more closely the camera will stick to the target. [code]
The target property defines which object the camera will follow. We set this to the MD2 mesh we loaded above. [code]
Finally the spring camera is set at the default camera by assigning it to the view.camera property. [code]
The spring camera allows you to create a fluid 3rd person camera, which is ideal for games like RPGs and adventure games. Combined with terrain, terrain following and animated models you have a good basis for creating your own games.
Go back to Away3D Tutorials