
In this lesson we will glue the camera to the "avatar".
First thing: we build an object (we call it aux) and glue the camera to it. It's created using:
aux.myworld.new model("aux")
We make it invisible:
aux.visibility = #noneIn the loop, we glue aux to the avatar. And the camera to aux. When the player moves the avatar (we saw how to do this in the previous lesson) he moves the camera.
It's important to note that we are talking about "avatar" but our world is not multiplayer and doesn't exist an avatar : only a camera.
To make the glues we use a Director method:
obj1.interpolate(obj2.getworldtransform(),lag)
This method moves a model (obj1) from its current position to the position of another object (obj2). lag is the time to move. If we use lag=100 the obj1 seems to be strongly glued. If we use lag=10 this gives a fluity to the movement of the camera.
We make the avatar invisible too.
In the new world of this lesson, the graphic part is similar and the script setupscript is the same of the previous lesson.
The difference in the script avatarobj is a line to make the "avatar" invisible:
myworld.model("avatar").visibility = #none
The script of the loop runs a new part of the script cameraobj:
on prepareframe me global avatarobj, cameraobj control avatarobj control cameraobj end on exitFrame me go the frame end
And the script cameraobj has many new lines:
property mycam
property aux
property cameraoffset
global avatarobj,myworld
on new me
aux = myworld.newmodel("aux")
mycam = myworld.camera[1]
aux.visibility = #none
mycam.projectionangle = 65
cameraoffset = [5,-50]
return me
end
on control me
aux.transform.interpolateto(myworld.model("avatar").getworldtransform(), 100)
camvect = (avatarobj.upvect*cameraoffset[1]+ avatarobj.fwvect*cameraoffset[2])
aux.translate(camvect, #world)
aux.pointat(myworld.model("avatar").worldposition )
lag = 100
mycam.transform.interpolateto(aux.getworldtransform(), lag)
end
Save, publish etc. etc. You can play the demo using the arrow keys.
The height (defined by cameraoffset = [5,-50]) and velocity of avatar/camera can be changed.
But we have two big problems. Walking, if you go throught the plaque with the logo, the avatar is like a ghost: he doesn't collide!. The second problem is that: if you try to go up stairs the avatar doesn't go up! He doesn't follow the "terrain". We will see how to fix this in next lessons.
PREVIOUS LESSON NEXT LESSON T. CONTENTS HOMEPAGE
