[Help]AttachObjectToPlayer
#1

Hi!
I need: If the player type a command , an object(a droid) will follow the player. I need if the object will behind the player.
I have tryed to create it but it doesn't works ,because it isn't behind the player.

Please help me!

This is the code:
Код:
 stock AttachPlayerDroid(playerid,droidid)//its same that objectid
{
new Float:X,Float:Y,Float:X1,Float:Y1,Float:Z1,Float:a;
GetXYBehindPlayer(playerid, X, Y, 7);
GetPlayerPos(playerid,X1,Y1,Z1);
GetPlayerFacingAngle(playerid,a);
if(GetPlayerVehicleID(playerid))
{
GetVehicleZAngle(GetPlayerVehicleID(playerid), a);
}
new Float:x,Float:y,Float:x1,Float:y1;
x = X-X1;
y = Y-Y1;
if(x < 0)
{
x1 -= x;
}else{
x1 =x;
}
if(y < 0)
{
y1 -= y;
}else{
y1 = y;
}
AttachObjectToPlayer(droidid,playerid,x1,y1,0,0,0,a+180);
}
And here is the GetXYBehindPlayer function:
Код:
stock GetXYBehindPlayer(playerid, &Float:x2, &Float:y2, Float:distance)
{
new Float:a;
GetPlayerPos(playerid, x2, y2, a);
GetPlayerFacingAngle(playerid, a);
if(GetPlayerVehicleID(playerid))
{
GetVehicleZAngle(GetPlayerVehicleID(playerid), a);
}

x2 += (distance * floatsin(-a+180, degrees));
y2 += (distance * floatcos(-a+180, degrees));
}
Reply
#2

Cant you just do it like this?

new droid;
pawn Код:
stock AttachPlayerDroid(playerid,droidid)//its same that objectid
{
  if(IsPlayerInAnyVehicle(playerid)) AttachObjectToPlayer(droidid, playerid, 0, 5, 0, 0, 0, 0);
  if(!IsPlayerInAnyVehicle(playerid)) AttachObjectToPlayer(droidid, playerid, 0, 2, 0, 0, 0, 0);
  droid = droidid;
}

public OnPlayerExitVehicle(playerid)
{
  if(droid != 0)
  {
     AttachObjectToPlayer(droid, playerid, 0, 2, 0, 0, 0, 0);
  }
  return 1;
}
Reply
#3

Your GetXYBehindPlayer is wrong

pawn Код:
GetXYBehindPlayer(playerid, &Float:x, &Float:y, Float:distance)
{
    new Float:a;

    GetPlayerPos(playerid, x, y, a);
    GetPlayerFacingAngle(playerid, a);
    if (IsPlayerInAnyVehicle(playerid)) GetVehicleZAngle(GetPlayerVehicleID(playerid), a);
    x -= (distance * floatsin(-a, degrees));
    y -= (distance * floatcos(-a, degrees));
}
Reply
#4

Quote:
Originally Posted by [HiC
TheKiller [Stuntp.com] ]
Cant you just do it like this?

new droid;
pawn Код:
stock AttachPlayerDroid(playerid,droidid)//its same that objectid
{
  if(IsPlayerInAnyVehicle(playerid)) AttachObjectToPlayer(droidid, playerid, 0, 5, 0, 0, 0, 0);
  if(!IsPlayerInAnyVehicle(playerid)) AttachObjectToPlayer(droidid, playerid, 0, 2, 0, 0, 0, 0);
  droid = droidid;
}

public OnPlayerExitVehicle(playerid)
{
  if(droid != 0)
  {
     AttachObjectToPlayer(droid, playerid, 0, 2, 0, 0, 0, 0);
  }
  return 1;
}
Thank you to helping. I have an another question. Is there a function what Detachs the object what is attached to player?
Reply
#5

No, but this could work
pawn Код:
DetachObjectFromPlayer(droneid, dronemodelid)
{
    new Float:x, Float:y, Float:z,
        Float:rotx, Float:roty, Float:rotz;
    GetObjectPos(droneid, x, y, z);
    GetObjectRot(droneid, rotx, roty, rotz);
    DestroyObject(droneid);
    return CreateObject(dronemodelid, x, y, z, rotx, roty, rotz);
}
It returns a new droneid, so to use it correctly, you need to do like this:
pawn Код:
new drone1 = CreateObject();
drone1 = DetachObjectFromPlayer(drone1, 234);
if 234 is the object model id of the drone
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)