Last active
September 25, 2024 14:51
-
-
Save MattOstgard/37717fd33461e8c4392493d3e491805f to your computer and use it in GitHub Desktop.
Orient to Movement Direction - Rotation Script Controller (3ds max, MaxScript)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Orient to Movement Direction - Rotation Script Controller | |
-- What it does: | |
-- - Orients a target object to a source object's movement direction and blends rotation between the source object's rotation and its | |
-- movement direction. | |
-- To use: | |
-- - Create the source object that you will animate position and rotation. | |
-- - Add some keyframes of the source object moving in different directions. | |
-- - Create the target object that will be controlled by the script. | |
-- - With the target object selected, go to the Motion tab in the Command Panel and assign Rotation to 'Rotation Script' | |
-- - The 'Script Controller' dialog should open, if not then right click on the 'Rotation controller' and click 'Properties' | |
-- - Create `o` variable: | |
-- - In the dialog in the 'Create Variable' section set the 'Name' text field to `o` and click 'Create' | |
-- - Click 'Assign Node' at the bottom of the dialog and a dialog will open. Find your source object under the 'Objects' section. | |
-- - Paste this script into the 'Expression' field and press evaluate. | |
-- - Change the `lerpMaxSpeed` variable in the script to a value that makes sense for your animations speed in units per second. | |
-- - Drag the time slider to see the target rotate. | |
-- Notes: | |
-- - Output axis is Maya/Unity which is: x = right, y = up, z = forward. | |
-- CHANGE THESE VALUES -- | |
-- At what speed per second should the target object be fully rotating toward the movement direction. | |
lerpMaxSpeed = 200.0 | |
-- Source object axis indexes to use for each direction | |
-- These will be remapped to unity style: | |
-- right = x, y = up, fwd = z | |
oFwd = 1 | |
oLeft = 2 | |
oUp = 3 | |
-- ACTUAL SCRIPT -- | |
vel = (at time F o.pos) - (at time (F - 1) o.pos) | |
speed = (length vel) * (framerate as float) | |
temp_x = (in coordsys world o.transform[oLeft]) | |
if (speed < lerpMaxSpeed) then | |
( | |
-- Speed is low so lerp between | |
-- velocity and target's forward vec | |
o_dir = (in coordsys world o.transform[oFwd]) | |
lerpTerm = speed / lerpMaxSpeed | |
vel = (vel - o_dir) * lerpTerm + o_dir | |
) | |
z_fwd = normalize vel | |
y_up = normalize (cross z_fwd temp_x) | |
x_left = normalize (cross y_up z_fwd) | |
o_pos = (in coordsys world o.pos) | |
tm = matrix3 x_left y_up z_fwd o_pos | |
-- RETURN VALUE | |
tm.rotation |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment