Jump to content

PERSPECTIVE SCRIPT - NEARLY FINISHED!


Maurize

Recommended Posts

so im trying to create a script, that allows the players to move with mouseclick( with walking to the targetpoint animation ) & rotate camera with "a" and "d" ( ped & camera )...

But now I'm unable to continue working on it, cause i don't know how to continue... Maybe some help, hints or something? Thanks..

  
multiData = getRootElement() 
singleData = getLocalPlayer() 
  
addEventHandler("onClientResourceStart", resourceRoot, 
function() 
    triggerServerEvent( "onActorSpawn", singleData ) -- for spawning player! 
    showCursor( true ) 
  
    addEventHandler( "onClientRender", multiData, 
function() 
    local px, py, pz = getElementPosition( singleData ) 
    local rx, ry, rz = getElementRotation( singleData ) 
    setCameraMatrix( px, py, pz + 35, px, py, pz, rz ) 
    end ) 
     
    bindKey( "a", "down", 
function() 
    local rx, ry, rz = getElementRotation( singleData ) 
    setElementRotation( singleData, rx - 90, ry, rz ) 
    end ) 
     
    bindKey( "d", "down", 
function() 
    local rx, ry, rz = getElementRotation( singleData ) 
    setElementRotation( singleData, rx, ry - 90, rz ) 
    end ) 
  
    bindKey( "mouse1", "down", 
function() 
    local sx, sy, wx, wy, wz = getCursorPosition" class="kw4">getCursorPosition" class="kw4">getCursorPosition() 
    setElementPosition( singleData, wx, wy, wz ) 
    end ) 
  
end ) 
  
  
  
  
 

Edited by Guest
Link to comment

i used getKeyState for each frame instead, because bind key only executes once if you hold it down.

local rotSpeed = 5 
  
addEventHandler("onClientPreRender", getRootElement(), 
function() 
    local rot = getPedRotation(singleData) 
    -- if you pressed on 'a' button 
    if(getKeyState("a"))then 
        rot = rot - rotSpeed 
    end 
    if(getKeyState("d"))then 
        rot = rot + rotSpeed 
    end 
    local px, py, pz = getElementPosition(singleData) 
    -- arguments: element, distanceX, distanceY, distanceZ 
    local dx, dy, dz = getTransformedPosition(singleData, 0, -20, 1) 
    setCameraMatrix(dx, dy, dz, px, py, pz) 
    setPedRotation(singleData, rot) 
    -- if you want to make the player walk towards the way he is facing, then its needed. 
    setPedCameraRotation(singleData, rot) 
end) 
  
function getTransformedPosition(element, distX, distY, distZ) 
    local matrix = getElementMatrix(element) 
    local offX = distX * matrix[1][1] + distY * matrix[2][1] * distZ * matrix[3][1] + 1 * matrix[4][1] 
    local offY = distX * matrix[1][2] + distY * matrix[2][2] * distZ * matrix[3][2] + 1 * matrix[4][2] 
    local offZ = distX * matrix[1][3] + distY * matrix[2][3] * distZ * matrix[3][3] + 1 * matrix[4][3] 
    -- returns the position related to position, and rotation of the element 
    return offX, offY, offZ 
end 

Link to comment

nah, i just wrote something wrong. I tried this and it works:

function getTransformedPosition(element, distX, distY, distZ) 
    local matrix = getElementMatrix(element) 
    local offX = distX * matrix[1][1] + distY * matrix[2][1] + distZ * matrix[3][1] + 1 * matrix[4][1] 
    local offY = distX * matrix[1][2] + distY * matrix[2][2] + distZ * matrix[3][2] + 1 * matrix[4][2] 
    local offZ = distX * matrix[1][3] + distY * matrix[2][3] + distZ * matrix[3][3] + 1 * matrix[4][3] 
    -- returns the position related to position, and rotation of the element 
    return offX, offY, offZ 
end 

replace it with the function u have.

i wrote it fast so i used multiplication where u needed to use addition

Link to comment

awesomee. really working :> .. now only problem is the movement with the mouseclick thing.. i don't know how to get and how much the ped must run ..

i tried with getKeyState too..

  
if ( getKeyState( "w" ) )then 
    local x, y, z = getElementPosition( singleData ) 
    setElementPosition( singleData, x + 0.25, y + 0.25, z ) -- rotation -.- 
    end 
  

Link to comment

since you are using setPedCameraRotation, then you can use setPedControlState to make ped go to destination

something like this... (didn't test)

local destX, destY, destZ = 0, 0, 0 
local STOP_DIST = 0.2 
  
-- when player presses with mouse, linked to the gta world, so we get world position 
addEventHandler("onClientClick", getRootElement(), 
function(button, state, absX, absY, worldX, worldY, worldZ, element) 
    if(button ~= "left" or state ~= "down")then return end 
    destX, destY, destZ = worldX, worldY, worldZ 
    addEventHandler("onClientRender", getRootElement(), movePed) 
end) 
  
function movePed() 
    -- make ped go forward all the time until he is close enough to the destinated position 
     
    setPedControlState(singleData, "forwards", true) 
    local x, y, z = getElementPosition(singleData) 
    -- use 2d distance, because you will need a more advanced AI for 3D 
    local dist = getDistanceBetweenPoints2D(destX, destY, x, y) 
    if(dist <= STOP_DIST)then 
        removeEventHandler("onClientRender", getRootElement(), movePed) 
    end 
end 

Link to comment

hm he still doesnt move, i tried for my self and simply setPedControlState with addCommandHandler, doen't work, i think cause the cursor which is activated..

//

so debugscript tells me that he can't removeEvenhandler because the ped isn't moving.. ControlState doens't work -.-

  
addEventHandler( "onClientClick", multiData, 
function( button, state, absX, absY, worldX, worldY, worldZ, element ) 
if ( button == "left" and state == "down" ) then 
    nx, ny, nz = worldX, worldY, worldZ 
    outputChatBox( "Ped is going" ) 
    addEventHandler( "onClientRender", multiData, setTransformedPosition ) 
    end 
end ) 
  
function setTransformedPosition() 
    local x, y, z = getElementPosition( singleData ) 
    local dist = getDistanceBetweenPoints2D( nx, ny, x, y ) 
    setPedControlState( singleData, "forwards", true ) 
if ( dist <= 0.2 )then 
    removeEventHandler( "onClientRender", multiData, setTransformedPosition ) 
    outputChatBox( "Ped is gone" ) 
    end 
end 
  

Link to comment

are you sure you are not the ped, that you are trying to change control state for?

i didn't make the exact same as you wanted it, but almost and it works:

local ped = nil 
  
  
    local destX, destY, destZ = 0, 0, 0 
    local STOP_DIST = 0.2 
      
     showCursor(true, true) 
    -- when player presses with mouse, linked to the gta world, so we get world position 
    addEventHandler("onClientClick", getRootElement(), 
    function(button, state, absX, absY, worldX, worldY, worldZ, element) 
        if(not ped or button ~= "left" or state ~= "up")then return end 
        outputDebugString("PED: camera rotation changed.") 
        destX, destY, destZ = worldX, worldY, worldZ 
        local x, y, z = getElementPosition(ped) 
        local rot = -math.deg(math.atan2(x-destX, y-destY)) - 180 
        setPedCameraRotation(ped, rot) 
        setPedRotation(ped, rot) 
        addEventHandler("onClientRender", getRootElement(), movePed) 
    end) 
      
    function movePed() 
        -- make ped go forward all the time until he is close enough to the destinated position 
        
        setPedControlState(ped, "forwards", true) 
        local x, y, z = getElementPosition(ped) 
        -- use 2d distance, because you will need a more advanced AI for 3D 
        local dist = getDistanceBetweenPoints2D(destX, destY, x, y) 
        if(dist <= STOP_DIST)then 
            removeEventHandler("onClientRender", getRootElement(), movePed) 
            setPedControlState(ped, "forwards", false) 
        end 
    end 
     
    bindKey("F3", "up",  
    function() 
        local x, y, z = getElementPosition(getLocalPlayer()) 
        ped = createPed(7, x, y + 10, z) 
    end) 
  

Link to comment

Okay, working but with bugs( ped starts running but in wrong direction and endless .. ) .. check yourself:

  
multiData = getRootElement() 
singleData = getLocalPlayer() 
element = createPed( 0, 2504.8, -1669.4, 13.4 ) 
  
addEventHandler("onClientResourceStart", resourceRoot, 
function() 
    showCursor( true, true ) 
    setCameraTarget( singleData, element ) 
    fadeCamera( true, 1.0 ) 
end ) 
  
addEventHandler( "onClientPreRender", multiData, 
function() 
    local rot = getPedRotation( element ) 
if ( getKeyState( "a" ) ) then 
    rot = rot + 3 
    end 
if ( getKeyState( "d" ) )then 
    rot = rot - 3 
    end 
    local px, py, pz = getElementPosition( element ) 
    local dx, dy, dz = getTransformedPosition( element, 0, -10, 10 ) 
    setCameraMatrix( dx, dy, dz, px, py, pz ) 
    setPedRotation( element, rot ) 
    setPedCameraRotation( element, rot ) 
end ) 
  
local destX, destY, destZ = 0, 0, 0 
addEventHandler( "onClientClick", multiData, 
function( button, state, absX, absY, worldX, worldY, worldZ, element ) 
if ( button == "left" ) and ( state == "down" )then 
    destX, destY, destZ = worldX, worldY, worldZ 
    local px, py, pz = getElementPosition( element ) 
    local rot = - math.deg( math.atan2( px - destX, py - destY ) ) - 180 
    setPedCameraRotation( element, rot ) 
    setPedRotation( element, rot ) 
    addEventHandler("onClientRender", multiData, setTransformedPosition ) 
    end 
end ) 
  
function setTransformedPosition() 
    setPedControlState( element, "forwards", true ) 
    local x, y, z = getElementPosition( element ) 
    local dist = getDistanceBetweenPoints2D( destX, destY, x, y ) 
if( dist <= 0.2 )then 
    removeEventHandler( "onClientRender", multiData, setTransformedPosition ) 
    setPedControlState( element, "forwards", false ) 
    end 
end 
     
function getTransformedPosition( element, distX, distY, distZ ) 
    local matrix = getElementMatrix( element ) 
    local offX = distX * matrix[1][1] + distY * matrix[2][1] + distZ * matrix[3][1] + 1 * matrix[4][1] 
    local offY = distX * matrix[1][2] + distY * matrix[2][2] + distZ * matrix[3][2] + 1 * matrix[4][2] 
    local offZ = distX * matrix[1][3] + distY * matrix[2][3] + distZ * matrix[3][3] + 1 * matrix[4][3] 
    return offX, offY, offZ 
end 
  

Link to comment

the reason for that could be that you change ped camera rotation when you press a or d, and whats the points of that if you want point and walk??

if you want the ped to ignore your keyboard input (for whatever reason you made one), then in your setTransformedPosition calculate rotation from ped to destination and setPedCameraRotation

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