Jump to content

Camera attach to an element


Recommended Posts

Hey dear community, Im having a weard issue.

Well, my point is that I want to create a vehicle moving by x cordenates to x cordenates.

Well, that is ok, I did it, but I would like to set the camera like if I would be driving the car, I would like to move the camera with the mouse, as If i would be driving a simple car, but in this case the car is moving alone, I just need the camera function for it.

I already tried

setCameraTarget 
setCameraMatrix 
setCameraViewMode 

Well, I wont post all the code, just the part that I need.

local handled = false 
car = createVehicle(411,0,0,0,0,0,0) 
  
function whatEver() 
    -- Here is the code of the Vehicle moving automatically 
    ------------------------------------------------------------ 
    -- The following one is to call the camera 
    if not handled then 
        turnCamera() 
        handled = true 
        outputChatBox("Handled") 
    end 
end 
  
function turnCamera() 
    setCameraTarget(car) 
end 

Thanks for reading.

Regards.

Link to comment

But I wont follow the camera of other player, I want to follow the cam of my vehicle (Car variable) which is being moved automatically using some cordenates that I set. It moves perfectly, but I want a camera function to looks like If I would be driving it.

Link to comment

I know but if u read it it says the problem is that you cannot set a vehicle as a camera target. So that s why you cannot do it.

Edit: Maybe you can get a ped into that vehicle, set it s alpha to 0, so it looks like noone is driving. I don't know if u can use the setCameraTarget on peds though.

Link to comment
  • Discord Moderators

Try using these functions (I didn't test them myself):

getCamera -- To get the camera element 
attachElements -- To attach the camera element to the car (while hopefully allowing mouse free look) 

If they don't work, you will have to use maths to script the camera mouse look when the camera moves, like freecam resources do.

Link to comment
Try using these functions (I didn't test them myself):
getCamera -- To get the camera element 
attachElements -- To attach the camera element to the car (while hopefully allowing mouse free look) 

If they don't work, you will have to use maths to script the camera mouse look when the camera moves, like freecam resources do.

Already done, It works, but Im not looking for this. When the camera is attached, I cant move it from left to right or whaterver using the mouse ...

My goal is move the mouse as If I would be driving a vehicle ...

Link to comment

If you still need it, I made a custom one using as base a code by ZoRRoM^ and this other code from gta forums(http://gtaforums.com/topic/835347-rotating-camera/?p=1068327127).

local rotX, rotY, rotRadius, maxRadius = 0, -math.pi/2, 10, 20 
local isCustomCamera = false 
local cameraTarget = nil 
local sX, sY = guiGetScreenSize() 
  
addEventHandler("onClientPreRender", root, 
    function() 
        if isCustomCamera and isElement(cameraTarget) then 
  
            local x, y, z = getElementPosition( cameraTarget ) 
            local cx, cy, cz 
             
            cx = x + rotRadius * math.sin(rotY) * math.cos(rotX) 
            cy = y + rotRadius * math.sin(rotY) * math.sin(rotX) 
            cz = z + rotRadius * math.cos(rotY) 
             
            local hit, hitX, hitY, hitZ = processLineOfSight(x, y, z, cx, cy, cz, _, false, _, _, _, _, _, _, cameraTarget) 
            if hit then 
                cx, cy, cz = hitX, hitY, hitZ 
            end 
  
            setCameraMatrix( cx, cy, cz, x, y, z ) 
        end 
    end 
) 
  
addEventHandler("onClientKey", root, 
    function(button) 
        if button == "mouse_wheel_up" then 
            rotRadius = math.max(2, rotRadius - 1) 
        elseif button == "mouse_wheel_down" then 
            rotRadius = math.min(maxRadius, rotRadius + 1) 
        end 
    end 
) 
  
addEventHandler("onClientCursorMove", root, 
    function(cX,cY,aX,aY) 
        if isCursorShowing() or isMTAWindowActive() then return end 
                 
        aX = aX - sX/2  
        aY = aY - sY/2 
  
        rotX = rotX - aX * 0.01745 * 0.3 
        rotY = math.min(-0.02, math.max( rotY + aY * 0.01745 * 0.3, -3.11 ) ) 
         
    end 
) 
  
function setCustomCameraTarget(element) 
    if isElement(element) then 
        cameraTarget = element 
        isCustomCamera = true 
        return true 
    else 
        cameraTarget = nil 
        isCustomCamera = false 
    end 
end 

Use setCustomCameraTarget function, for example:

setCustomCameraTarget(localPlayer) 

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