Jump to content

Gta v or iv camera


JCL

Recommended Posts

Hi guys

Some friends asked me about the code for gta v camera or at least similar one, so , here is the code for it :

local camera_pan;
local camera_tilt;
local camera_temp_x;
local camera_temp_y;
local camera_temp_z;
local camera_offset_x;
local camera_offset_y;
local camera_offset_z;
local player_game_x;
local player_game_y;
local player_game_z;
local first_enabled     = 0;
local mouse_move_x      = 0;
local mouse_move_y      = 0;
local camera_speed      = 0.8;
local enable_sidecamera = 1;
localplayer             = getLocalPlayer ();
local fps               = false;


local function updateFPS(msSinceLastFrame)
    fps = (1 / msSinceLastFrame) * 1000;
end
addEventHandler("onClientPreRender", root, updateFPS)

function check_gui_elements_visible()
    local windows = getElementsByType("gui-window") 
    for i,window in pairs(windows) do
	    if(guiGetVisible(window) == true) then
		    return true
		end
	end
	return false;
end

function isPedAiming ( thePedToCheck )
	if isElement(thePedToCheck) then
		if getElementType(thePedToCheck) == "player" or getElementType(thePedToCheck) == "ped" then
			if getPedTask(thePedToCheck, "secondary", 0) == "TASK_SIMPLE_USE_GUN" then
				return true;
			end
		end
	end
	return false;
end

function attach_camera(distance_x,distance_y)
    local is_any_gui_visible = check_gui_elements_visible();
    if(is_any_gui_visible == true) then mouse_move_x = 0;mouse_move_y = 0;end
	if(isCursorShowing()) or (isConsoleActive()) or (isMainMenuActive()) or (isMTAWindowActive()) then
	   mouse_move_x = 0;mouse_move_y = 0;
	end
	
    player_game_x   ,player_game_y   ,player_game_z    = getElementPosition(localplayer);

    local time_step = 1;
	if(fps) then  time_step = 1/fps; end
	
	camera_pan  = camera_pan  + 1 * mouse_move_x * time_step * 50;
	camera_tilt = camera_tilt + 1 * mouse_move_y * time_step * 50; 
	
	if(camera_tilt > 0.8) then camera_tilt = 0.8; end
	if(camera_tilt < -2) then camera_tilt = -2; end
	
	local offset_x      = player_game_x - distance_y * math.cos(camera_pan+180);
	local offset_y      = player_game_y - distance_y * math.sin(camera_pan+180);
	
	local camera_x      = offset_x - distance_x * math.cos(camera_pan);
	local camera_y      = offset_y - distance_x * math.sin(camera_pan);
	local camera_z      = player_game_z + 1 * -camera_tilt;
	
	camera_temp_x ,camera_temp_y,camera_temp_z       = interpolateBetween (camera_temp_x,camera_temp_y,camera_temp_z,camera_x,camera_y,camera_z, camera_speed, "Linear");
	camera_offset_x ,camera_offset_y,camera_offset_z = interpolateBetween (camera_offset_x,camera_offset_y,camera_offset_z,offset_x,offset_y,0, camera_speed, "Linear");
	
	setCameraMatrix (camera_temp_x,camera_temp_y,camera_temp_z, camera_offset_x, camera_offset_y, player_game_z+0.5 ,0,100);

end

function camera_activation()
	if(enable_sidecamera == 1) then
	    if(first_enabled == 0) then
		    camera_pan = getPedCameraRotation(localPlayer)+180;
			first_enabled = 1;
		end
	    local v = getPedOccupiedVehicle(localPlayer)
		if(v) then
	        if(getCameraTarget () ~= v) then
	            setCameraTarget (localPlayer);
		    end
		else
		    if(isPedAiming (localPlayer) == true) then
	            if(getCameraTarget () ~= localPlayer) then
	                setCameraTarget (localPlayer);
		        end
			else
                attach_camera(1.5,-1);
			end
		end
	    
	else
	    first_enabled = 0;
	    local v = getPedOccupiedVehicle(localPlayer)
		if(v) then
	        if(getCameraTarget () ~= v) then
	            setCameraTarget (v);
		    end
		else
	        if(getCameraTarget () ~= localPlayer) then
	            setCameraTarget (localPlayer);
		    end
		end
	end
end


addEventHandler ("onClientPreRender", getRootElement(), camera_activation);

addEventHandler( "onClientCursorMove", getRootElement( ),
    function (x, y,_,_ )
	    local mouse_move_x2 = (0.5 - x) * 100;
	    local mouse_move_y2 = (0.5 - y) * 100;
		mouse_move_x ,mouse_move_y = interpolateBetween (0,0, 0,mouse_move_x2,mouse_move_y2,0, 0.1, "Linear");
    end
);

 

Example

 

Feel free to ask any question about the code.

 

 

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...