Moving a model over a contoured surface
-- move along terrain
-- create reference for terrain (ground)
terrain = p3Dmember.model("Terrain")
-- store character's position
charPos = pCharacter.worldPosition
-- cast a ray down
collisData = \
-- if model is picked up on ray
if collisData.count then
-- store total no of models detected by the ray
totalCount = collisData.count
repeat with modelNo = 1 to totalCount
-- check if over the terrain model
if (collisData[modelNo].model = terrain) then
terrainPos = collisData[modelNo].isectPosition
-- find out the distance the character should move
-- up or down
diff = (terrainPos.z - \
pCharacter.worldPosition.z) + 45
-- move the character
In the above, we used the modelsUnderRay command to send a ray down and check the distance between the character and the ground. Since there could be other models on the ground, we do a check through the list of models to make sure that we only move the character if the terrain model is on the collision list.
2. Play the movie and test what you just created.
You will notice that when you try walk through the wall on the flat part of the ground, you are prevented from doing so. However, if you walk up the mound, you can walk through the wall. The reason for this is that the ray being cast forward is missing the wall (is going over it).
You can download the completed movie from here.