Jump to content

[HELP]Custom Sniper Scope


Trilon

Recommended Posts

Hello!

I'm trying to do a custom sniper scope, for my mod but setCameraMatrix() is bugging for me. I tried many ways but i don't really knows how to do it.

My Code : 


 

function aim_test ()
    if getControlState("aim_weapon") == true and getPedWeapon(localPlayer) == 34 then
    local x,y,z = getPedTargetStart(localPlayer)
    local x2,y2,z2 = getPedTargetEnd(localPlayer)
    if not x2 then
        return
    end            
    local x3,y3,z3 = getPedTargetCollision(localPlayer)
    if x3 then
        setCameraMatrix(x,y,z,x3,y3,z3)
    else
        setCameraMatrix(x,y,z,x2,y2,z2)
    end
    else
        if not getCameraTarget(localPlayer) then
            setCameraTarget(localPlayer)
        end
    end
end
addEventHandler("onClientPreRender",root,aim_test)

I don't know why it's always moving foward.

Link to comment
  • Administrators

The "getPedTargetStart" function will retrieve the starting point of the target range. This target range is probably linked to the camera position (I'm not entirely sure though), which would explain why your camera is "always moving forward"

Let's say the target start range is always 1 unit in front of the camera. Your camera position is set to 0. Then you set the camera position to the start range (1), and the start range then subsequently changes to 2. This will happen forever whilst you are zoomed in.

You should store the initial target start position, instead of getting it on each frame. This can be done quite easily.

local targetStartPos = false

function aim_test ()
    if getControlState("aim_weapon") == true and getPedWeapon(localPlayer) == 34 then
		if not targetStartPos then
			local startX, startY, startZ = getPedTargetStart(localPlayer)
			targetStartPos = {x = startX, y = startY, z = startZ}
		end
		
		local x, y, z = targetStartPos.x, targetStartPos.y, targetStartPos.z
		
		local x2,y2,z2 = getPedTargetEnd(localPlayer)
		
		if not x2 then
			return false
		end            
		
		local x3,y3,z3 = getPedTargetCollision(localPlayer)
		
		if x3 then
			setCameraMatrix(x,y,z,x3,y3,z3)
		else
			setCameraMatrix(x,y,z,x2,y2,z2)
		end
    else
        if not getCameraTarget(localPlayer) then
            targetStartPos = false
            setCameraTarget(localPlayer)
        end
    end
end
addEventHandler("onClientPreRender",root,aim_test)

Haven't tested, but I'm fairly certain that should fix your issue.

Edited by LopSided_
  • Like 2
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...