Shaking Camera Script

From Mount&Blade Modding Wiki
Jump to: navigation, search

Contents

Description

This is a script written for both Warband, SP & MP. It makes the camera shake (change rotation along x/z direction randomly) every 0.01 second and generates an effect similar to explosions, earthquakes etc...

Knowledge/Tools Required

Script

Addition to Module_scripts

Place the below anywhere in module_scripts,

	("shake_camera",
	[
		(store_script_param_1,":n"),
		(store_script_param,":loc_shake_x",2),
		(store_script_param,":loc_shake_z",3),
		(assign, "$shake", ":n"),
		(assign, "$shake_x", ":loc_shake_x"),
		(assign, "$shake_z", ":loc_shake_z"),
	]),

Addition to Module_mission_templates

Place the below trigger in the required mission_templates,

	### Camera Shake ###
        # Part 1 - rotate the camera a random value from 0 to shake_x/z
	(0.01, 0, 0, [],[ # Changes angle every 0.01 second, value of 0.01 can be changed 
                          # for different effects
		(try_begin),
			(ge, "$shake", 1),
			(store_mul,":neg_shake_x",-1,"$shake_x"),
			(store_mul,":neg_shake_z",-1,"$shake_z"),
			(store_random_in_range,":drx",":neg_shake_x","$shake_x"),
			(store_random_in_range,":drz",":neg_shake_z","$shake_z"),			
			(assign, "$cam_drx", ":drx"),
			(assign, "$cam_drz", ":drz"),
			(try_begin),
				(eq, "$shake", 1),
				(mission_cam_set_mode, 0),
			(try_end),
			(val_sub, "$shake", 1),
		(try_end),
	]),
        # Part 2 - make camera follow changed rotation every frame
	(0, 0, 0, [],[
		(try_begin),
			(gt, "$shake", 0),
			(mission_cam_set_mode, 1),
			(get_player_agent_no, ":p_agent"),
			(agent_get_look_position, pos0, ":p_agent"),	
			(agent_get_horse,":horse",":p_agent"),
			(try_begin),
				(eq, ":horse", -1),
				(position_move_z,pos0,208,1),
			(else_try),
				(position_move_z,pos0,287,1),
			(try_end),			
			(position_move_y,pos0,-130),
			(position_rotate_x, pos0, "$cam_drx"),
			(position_rotate_z, pos0, "$cam_drz"),
			(mission_cam_set_position, pos0),			
		(try_end),
	]),

Trigger the camera script

Add the following line where appropriate, i.e. after grenade explodes etc...

(call_script, "script_shake_camera", 100, 5, 12), # no. of shakes, max rotate x, max rotate z

Credits

By Sonkidd.

Open source; use and edit at will. Credits given where appropriate would be appreciated, but not necessary.

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox