Jump to content

Is there a way to find out if the player is facing at....


kevin433

Recommended Posts

I guess isElementOnScreen would be useful for you to find out if a player is facing some element.

As for the limits, there is an overall limit for the number of elements (vehicles, objects, peds, etc. all together) but i doubt you will ever reach this limit as its over 65000 elements. But there are also some other limits like number of objects in one area (cant remember the numbers) and a number of vehicle models in one area (if i remember right it was about 120 different models, but you are not limited in number of vehicles as long as the number of models does not get over this limit).

So practically you are free to have as many elements as you want as long as they are spread over the map well.

Link to comment

:shock: 65000 Elements. I'm never going to reach that :)

I tried "isElementOnScreen", but it gives me an error:

attempt to call global 'isElementOnScreen' (a nil value)

That's my code:

local x,y,z = getElementPosition(player)
local colsphere = createColSphere(x,y,z,5)
local vehicles = getElementsWithinColShape(colsphere, "vehicle")
for i,vehicles2 in ipairs(vehicles) do
if(isElementOnScreen(vehicles2)) then
outputChatBox(getElementID(vehicles2))
end
end

There's more above it in my script, but in that part is the error. I'm using a commandhandler to call the function. When I take out "if(isElementOnScreen(vehicles2)) then" everything works fine.

EDIT: Found one wrong thing, I called the function "isElementOnScreen" in a Server-side file #-o

Edited by Guest
Link to comment

I made it now Client-side, but it's still not working :?

function OpenCar()
local x,y,z = getElementPosition(getLocalPlayer())
local newcolsphere = createColSphere(x,y,z,5)
local vehicles = getElementsWithinColShape(newcolsphere, "vehicle")
for theKey,vehicles2 in ipairs(vehicles) do
if(isElementOnScreen(vehicles2)) then
outputChatBox(getElementID(vehicles2))
end
end
end

BTW: I'm still learning the generic for loop.

Link to comment
Please note that for legacy reasons, a colshape created on the client does not collide with elements already existing at that location until they first move

Maybe you should retrieve this vehicles table server side, then triggerClientEvent() and send it to client side, then proceed check with loop and isElementOnScreen()

Link to comment

The only problem is then that I can't get the Player who triggered the event, because I triggered the "OpenCar" function with a GUI button , but a button doesn't "send" the player with it. So I would have to do this: Press Button -> triggerSeverEvent -> vehicle loop -> triggerClientEvent -> isElementOnScreen. But as I said I can't get the player who triggered the event, or can I?

Link to comment

That's how far I came:

GUI Button -> triggerServerEvent -> get elementID's near the player -> ???

I didn't came further than in the example above.

Client side:

-- Player click a button, then OpenCar gets triggered
function OpenCar()
triggerServerEvent("CVehicles", getLocalPlayer())
end

Server-side:

function CheckVehiclesNear()
local x,y,z = getElementPosition(source)
local newcolshape = createColSphere(x, y, z,5)
local vehicles = getElementsWithinColShape(newcolshape, "vehicle")
for theKey,vehicles2 in ipairs(vehicles) do
outputChatBox(getElementID(vehicles2))
end
destroyElement(newcolshape)
triggerClientEvent(source,"COnScreen", getRootElement(), vehicles2)
end
addEvent("CVehicles", true)
addEventHandler("CVehicles", getRootElement(), CheckVehiclesNear)

Client-side. tried it, but it didn't work:

function CarOnScreen(vehicles)
for theKey,vehicles2 in ipairs(vehicles) do
if(isElementOnScreen(vehicles2)) then
outputChatBox(getElementID(vehicles2))
end
end
end
addEvent("COnScreen", true)
addEventHandler("COnScreen", getRootElement(), CarOnScreen)

Should I maybe set the "triggerClientEvent in the loop in the Server-side script?

Link to comment

You dont need all the fancy server-client event calls, just get the closest vehicles looping through all of them (getElementsByType("vehicle")) and comparing distance between the vehicle and local player(getDistanceBetweenPoints3D), if its close, check if its on screen.

Link to comment

Have this now:

function OpenCar()
local x,y,z = getElementPosition(getLocalPlayer())
for i,vehicle in ipairs(getElementsByType("vehicle")) do
local vx,vy,vz = getElementPosition(vehicle)
if(getDistanceBetweenPoints3D(x,y,z,vx,vy,vz) <= 4) then
if(isElementOnScreen(vehicle)) then
outputChatBox(getElementID(vehicle))
end
end
end
end

It works!

Problem now: I want to get the ID of the Vehicle the player is looking at, because, when I stand between 2 vehicles and both are <=4 then I get both ID's, but I just want to get the one which I'm looking at, that's why I added "isElementOnScreen", but I get both ID's when I stand between them and I just look at one of them while the other car is "behind" my camer.

Link to comment

Strange it hasnt passed the vehicles you dont see. But anyway. You could drop the loop at all and instead shoot a process line of sight in the direction the player is facing for the distance you want.

local x, y, z = getElementPosition ( player )
local rot = getPedRotation ( player )
local radius = 4
local lx = x + math.sin ( math.rad ( -rot ) ) * radius
local ly = y + math.cos ( math.rad ( -rot ) ) * radius
local hit, hx, hy, hy, element = processLineOfSight ( x, y, z, lx, ly, z )
if ( hit and getElementType ( element ) = "vehicle" ) then
-- 'element' is the vehicle you need
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...