Page 1232 - Kitab3DsMax
P. 1232
Part XII: MAXScript and Plug-Ins
The script also creates a variable called relPos, which you use to refer to the relative position of the fish
with respect to the dummy object. If you have several fish in the scene, you don’t want them all in the exact
same spot, so this is an easy way to position each one.
The next block of MAXScript is new: You’re using the animate on construct. This tells Max to generate
keyframes for your animation. It’s the same as if you had pressed Max’s Animation button, run our script,
and then shut Animation off. So any MAXScript inside the animate on parentheses creates animation key-
frames. These parentheses define a section of the script you call a block.
Inside the animation block, you have a loop that counts from 1 to 100 (corresponding to each frame of our
animation). On the end of the loop line, you have at time t, which tells Max that for each time through the
loop, you want all the variables to have whatever values they’ll have at that time. For example, if you want the
fish to follow the dummy object, you have to know the position of the object at each point in time instead of
just at the beginning; so each time through the loop, Max figures out for you where the dummy object will be.
Inside the loop, you set the fish’s object to be that of the dummy object (at that point in time) and then
adjust the fish’s position by relPos.
Part 2: Adding body rotation and tail animation
Next you’ll make that fish look a little more lifelike by animating its tail and having it rotate its body to
actually follow the path. Also, you’ll add a little unpredictability to its motion so that when you add other
fish, they aren’t exact copies of each other.
To improve the fish’s animation, follow these steps:
1. Type the revised version of the script (the new lines are in bold):
pathObj = $Dummy01
fishObj = $Fish1/FishBody
fishTail = $Fish1/FishBody/FishTail
relPos = [0,-150,-50] -- How close the fish is to the path
fishTail.bend.axis = 0 -- 0 is the x-axis
zadd = 4 -- vertical movement at each step
tailFlapOffset = (random 0 100)
tailFlapRate = 25 + (random 0 25)
animate on
(
for t = 0 to 100 do at time t
(
fishObj.position = pathObj.position + relPos
fishObj.position.z = relPos.z
relPos.z += zadd
-- let’s say that there’s a 10% chance that the fish will
-- change directions vertically
if ((random 1 100) > 90) then
(
zadd = -zadd
)
fishTail.bend.angle = 50 * sin (t * tailFlapRate +
tailFlapOffset)
1184