Jump to content

Camera System Like GTA 5


Grozz

Recommended Posts

Hello Guys, I just want to write a camera system like in GTA 5. (The Camera is closer to the player, etc)
So i can aim with any weapon, but not rotating the ped. Cannot look around, cannot look up, or down when aiming. Any Ideas?

 

local camera = {} 
local currentCameraView = 1 
local cameraView = {} 
cameraView[1] = 1.2 --SimpleControl
cameraView[2] = 6.5 --InVehicle

camera.dist = cameraView[currentCameraView] 
camera.speed = 4
camera.x = math.rad(60) 
camera.y = math.rad(60) 
camera.z = math.rad(15) 
camera.maxZ = math.rad(89) 
camera.minZ = math.rad(-45)
  
addEventHandler("onClientPreRender", getRootElement(), 
function()
		 x, y, z = getElementPosition(localPlayer) 
		z = z + 0.2 
		local camDist = camera.dist 
		local cosZ = math.cos(camera.z) 
		 camX = x + math.cos(camera.x)*camDist*cosZ 
		 camY = y + math.sin(camera.y)*camDist*cosZ 
		 camZ = z + math.sin(camera.z)*camDist 
		setCameraMatrix(camX, camY, camZ + 0.2, x + 0.1, y + 0.1, z + 0.2)
end)

addEventHandler("onClientCursorMove", getRootElement(), 
function(curX, curY, absX, absY)
	
		local diffX = curX - 0.5 
		local diffY = curY - 0.5 
		local camX = camera.x - diffX*camera.speed 
		local camY = camera.y - diffX*camera.speed 
		local camZ = camera.z + (diffY*camera.speed)/math.pi 
		if(camZ > camera.maxZ)then 
			camZ = camera.maxZ 
		end 
		if(camZ < camera.minZ)then 
			camZ = camera.minZ 
		end 
		camera.x = camX 
		camera.y = camY 
		camera.z = camZ
end) 
 
function isItInVeh()
	isitin = isPedInVehicle(localPlayer) 
	if isitin == true then
		currentCameraView = 2
	else
		currentCameraView = 1
	end

    camera.dist = cameraView[currentCameraView] 
end
addEventHandler("onClientPreRender", getRootElement(), isItInVeh)

bindKey("mouse_wheel_down", "down", 
function() 
    cameraView[1] = cameraView[1] + 0.1
    if(cameraView[1] >= 2.5)then 
        cameraView[1] = 2.5
    end 
    camera.dist = cameraView[currentCameraView] 
end)

bindKey("mouse_wheel_up", "down", 
function() 
    cameraView[1] = cameraView[1] - 0.1
    if(cameraView[1] <= 1.2)then 
        cameraView[1] = 1.2 
    end 
    camera.dist = cameraView[currentCameraView] 
end)


bindKey("mouse_wheel_down", "down", 
function() 
    cameraView[2] = cameraView[2] + 0.4
    if(cameraView[2] >= 12.5)then 
        cameraView[2] = 12.5
    end 
    camera.dist = cameraView[currentCameraView] 
end)

bindKey("mouse_wheel_up", "down", 
function() 
    cameraView[2] = cameraView[2] - 0.4
    if(cameraView[2] <= 6)then 
        cameraView[2] = 6 
    end 
    camera.dist = cameraView[currentCameraView] 
end)

addEventHandler("onClientRender", getRootElement(), 
function()
		
	if isCursorShowing() then
		camera.speed = 0
	elseif isChatBoxInputActive() then
		camera.speed = 0
	else
		camera.speed = 4
	end
end)
-----------------------------The Aiming Magic
bindKey("aim_weapon", "down", 
function()
 outputChatBox("Aiming")
end)

bindKey("aim_weapon", "up", 
function() 
    outputChatBox("Not Aiming")
end)

 

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...