[HELP] Determine if a player is near a specific object
#1

Hello there, I've been struggling about how to determine if a player is near a specific object. For example:

If Player A plants a landmine(OBJECT ID: 1) on the ground, and Player B walks above the landmine, then the landmine explodes. The point is that how do you know when Player B is close enough to the landmine that might cause the explosion?

Thank you for your patience and answers!
Reply
#2

If you use dynamic objects you can use this function to check if a player is near of an dynamic object (mine)

PHP код:
stock IsPlayerNearDynamicObject(playeridobjectidfloat:radius)
{
    new 
Float:oXFloat:oYFloat:oZ;
    
    
GetDynamicObjectPos(objectidoXoYoZ);
    
    return 
IsPlayerInRangeOfPoint(playeridradiusoXoYoZ);

Reply
#3

Objectid is just and id, and integer. You cannot get anything from that. You can create variables to store coordinates of and object like:
new Float: objectx[1000];
new Float: objecty[1000];
new Float: objectz[1000];

But disclaimer! Don't use so much space for arrays. Use for how many objects you are gonna use in your script.
After creating object just save its coordinates in variables that are going to hold those coordinates.
Reply
#4

Quote:
Originally Posted by N3cromancer
Посмотреть сообщение
If you use dynamic objects you can use this function to check if a player is near of an dynamic object (mine)

PHP код:
stock IsPlayerNearDynamicObject(playeridobjectidfloat:radius)
{
    new 
Float:oXFloat:oYFloat:oZ;
    
    
GetDynamicObjectPos(objectidoXoYoZ);
    
    return 
IsPlayerInRangeOfPoint(playeridradiusoXoYoZ);

Thank you for your quick response,

How do you know if that DynamicObject is a landline, not a random object?
Reply
#5

You have to store its ID into variable so you cannot get random object.
Reply
#6

Quote:
Originally Posted by antixgaming
Посмотреть сообщение
You have to store its ID into variable so you cannot get random object.
Hi! Could you please show an example? I am a bit of unsure about how can you store an object into variables.
Reply
#7

Ok.
So when you create object (landmine in your case), you have to put it's id into variable.

Example:

PHP код:
new oid[0] = CreateObject(objectidFloat:xFloat:yFloat:zFloat:rXFloat:rYFloat:rZ); // this is for example your code for landmine object

new Float:ox[1000],Float:oy[1000],Float:oz[1000];//declaring new variables to store coordinates
GetObjectPos(oid[0], ox[0], oy[0], oz[0]);//geting the coordinates and storing them into variables


stock IsPlayerNearObject(playeridfloat:radiusfloat:oxfloat:oyfloat:oz
    return 
IsPlayerInRangeOfPoint(playeridradiusoxoyoz);//getting the coordinates and checking if player is near 
But that is just like IsPlayerInRangeOfPoint, because you cannot check if player is near an object by it's id.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)