[Tutorial] AttachObjectToVehicle
#1

Introduction
Hey guys, my second tutorial. In this tutorial (i hope) you learn some things about AttachObjectToVehicle. This is a pretty cool function, that allow you to attach random objects to a vehicle.
I saw a people having problems with this, because they had no clue about how to get the coцrds of the objects.

What do we need
There isnt alot of things that you need, just the server package with your GM/FS in it, and Pawno. Also we need a program that can place objects. This could be MTA map editor, an in-game editor. In this tutorial i will be using "REAL Map editor" by JernejL.
The download link is down at the credits.

Functions
There are a few functions we will be using for this tutorial. Not just AttachObjectToVehicle.

AttachObjectToVehicle
This function will attach an object to any vehicle.
Код:
AttachObjectToVehicle(objectid, vehicleid, Float:fOffsetX, Float:fOffsetY, Float:fOffsetZ, Float:fRotX, Float:fRotY, Float:RotZ);
objectid - The object you want to attach
vehicleid - The vehicle you want to attach the object to
Float:fOffsetX - The distance between the vehicle, and the object (X direction)
Float:fOffsetY - The distance between the vehicle, and the object (Y direction)
Float:fOffsetZ - The distance between the vehicle, and the object (Z direction)
Float:fRotX - The X rotation between the object and the vehicle
Float:fRotY - The Y rotation between the object and the vehicle
Float:RotZ - The Z rotation between the object and the vehicle
CreateObject
This function creates a object, at the location you want.
Код:
CreateObject(modelid, Float:X, Float:Y, Float:Z, Float:rX, Float:rY, Float:rZ, Float:DrawDistance);
modelid - This is the id of the model you want to add. Example: Palm tree
Float:X - The X coordinate to create the object at
Float:Y - The Y coordinate to create the object at 
Float:Z - The Z coordinate to create the object at
Float:rX - The X rotation of the object
Float:rY - The Y rotation of the object
Float:rZ - The Z rotation of the object
Float:DrawDistance - The distance when you will see the object. This option is OPTIONAL, and if you dont use this, it will be at 300.0
DestroyObject
This will destroy an object that you created with CreateObject
Код:
DestroyObject(objectid);
objectid - The object you want to destroy. This can be done with a variable (easiest way)
Lets get started
To attach an object to a vehicle, we need to have the coцrds of them in the first palce. Otherwise we cant attach the object. Start up "REAL Map editor". Take a look at the controls when you first start up. If you know which keys you need to use a little bit, press the start button.
When it's fully loaded, fool around a bit with the camera (Hold the right mouse button, look around with with moving the mouse. Also moving the camera with Right mouse button + asdw). On the right of the window, you see 2 tabs. "Views" and "Objects".

If you are at the Objects tab, press the button "add". Go to the little inputtextbox "Search for..". Type the vehicle name you want to 'pimp', and after you got it: press Add again (but then the one with Cancel button beside it).
Now you see you have your vehicle in your Object Collection. In my case, it is a SandKing. Now press the vehicle picture in your collection, and move your mouse to the map, anywhere. Then press the button "Insert" on your keyboard. Press Ok, and now you see a red vehicle have been spawned in the map. Now click the vehicle once again, so its sure you have it selected.
Look down in the Objects tab, for the Coordinates. Those are the coцrds of the vehicle. Change them to "0, 0, 0". For the rotation the same, 0, 0, 0. Now you see the vehicle dissapeared, but thats not true. Its just moved away to the coцrdinates "0, 0, 0".
Move to the "View" tab, and look for the "Camera View". If there isnt 0,0,0 in it, type in 0,0,0 and press "Point camera at these coцrdinates". You see youre now at the vehicle position. Move a bit around with the camera, so you can see the vehicle better. Note: Its just under the normal ground, so dont go to high with the camera or you just see the ground.
Now go to the Objects tab again, and press add. Then search for some objects you want to attach to your vehicle. I will be using a cop's head (id 3012). Once again, select the object, move your mouse to the vehicle, and press insert. You see the object is now in-game, and you can move it around with your mouse. You can rotate the object down at the rotation coцrdinates. Move around the X/Y/Z angle. If you want to place the object higher/lower, press CTRL + arrow up/down. This can also be done by changing the last numbers (Z) in the Coordinates.
If you got the items at the right place, look for the taskbar above the screen. There is a button called "Show Code". Press that button, and you see there are some lines of codes in there (if you placed the objects). Do NOT close RME (Real map editor) untill you are done.

Now we got the coцrds of the object, we're gonna create a command that makes you a vehicle, and attach the objects to it. For the command i will use ZCMD.
pawn Код:
new VehicleObject[1];
/*
    Create this variable outside any callback. We will use this to create the object and attach it.
    IMPORTANT NOTE: Always keep the number 1 above your objects. Example: You got 5 objects, the variable will be: VehicleObject[6];
    If you dont do this, you will get the error "error 032: array index out of bounds (variable "VehiclePos")"
*/

COMMAND:vehicle(playerid, cmdtext[])
{
    new veh, Float:Pos[4]; // The variable veh we will create a vehicle at your position. The Float:Pos is just here, to hold your position.
    GetPlayerPos(playerid, Float:Pos[0], Float:Pos[1], Float:Pos[2]); // This will get your current position, and save them in the variables Float:Pos[#]
    GetPlayerFacingAngle(playerid, Float:Pos[3]); // This will get your current facing angle, so we can change the angle of the vehicle to this.
    VehicleObject[0] = CreateObject(3012, 0.18, 2.10, -0.01,   -12.00, -18.00, 18.00);
    /*
        Why do we create the object? This will create the object at the coцrdinates you see above, and after you created it with a variable, you can change the position anytime. If you didnt create it yet, and then try to move the object you didnt make, the object wont move!
        Note: This is just the object i made, do NOT use it (or you want the same vehicle as me)
    */

    veh = CreateVehicle(495, Float:Pos[0], Float:Pos[1], Float:Pos[2], Float:Pos[3], -1, -1, 50000); // Creates the vehicle in the variable 'veh', at your position, with a random color and a respawn delay of 50000 seconds.
    AttachObjectToVehicle(VehicleObject[0], veh, 0.18, 2.10, -0.01,   -12.00, -18.00, 18.00);
    /*
        The most important thing, attach the object to your vehicle!
        You see the first parameter is VehicleObject[0], this is the object we just created
        Second parameter, veh, This will be the vehicle you want to attach the object to
        The other 6 parameters, are the positions of the object you created. In my case those are "0.18, 2.10, -0.01,   -12.00, -18.00, 18.00". As you can see, those are the same coцrdinates as my object.
        Note: If you change one of those coцrdinates, the object wont show at the position you places!
    */

    PutPlayerInVehicle(playerid, veh, 0); // This will put yourself in the vehicle, if you dont want this, remove this!
    /*
        Also, change the line "veh = CreateVehicle(495, Float:Pos[0], Float:Pos[1], Float:Pos[2], Float:Pos[3], -1, -1, 50000);" to "veh = CreateVehicle(495, Float:Pos[0]+3, Float:Pos[1]+3, Float:Pos[2], Float:Pos[3], -1, -1, 50000);"
        This will make the vehicle not spawn on your head. Note: I will soon update this to GetXYZInFrontOfPlayer
    */

    SendClientMessage(playerid, 0xFFFFFF, "You just created an awesome vehicle, Thanks to Wesley221's tutorial! "); // This will show a message if you type the command "/vehicle"
    return 1;
}
Now we got the command, that will create the vehicle. After that, it attaches the object to your vehicle, and then our vehicle is 'pimped'!

Multiple objects attached
With this you can make multiple attached objects on your vehicle. It isnt that much harder then one object, just takes a little bit more time.
pawn Код:
// NOTE: The comments about what the things do are above, and if you dont know what they do, please look above!
new VehicleObject[2];
COMMAND:vehicle(playerid, cmdtext[])
{
    new veh, Float:Pos[4];
    GetPlayerPos(playerid, Float:Pos[0], Float:Pos[1], Float:Pos[2]);
    GetPlayerFacingAngle(playerid, Float:Pos[3]);
    VehicleObject[0] = CreateObject(3012, 0.18, 2.10, -0.01,   -12.00, -18.00, 18.00);
    VehicleObject[1] = CreateObject( .. , .. , .. , .. , .. , .. , .. ); // This will create a second object, which we will attach to the vehicle aswell
    // NOTE: I dont have coцrdinates of the second object, updating that aswell soon
    veh = CreateVehicle(495, Float:Pos[0], Float:Pos[1], Float:Pos[2], Float:Pos[3], -1, -1, 50000);
    AttachObjectToVehicle(VehicleObject[0], veh, 0.18, 2.10, -0.01,   -12.00, -18.00, 18.00);
    AttachObjectToVehicle(VehicleObject[1], veh, .. , .. , .. , .. , .. , .. ); // This will attach VehicleObject[1] to veh. And now, we got 2 objects attached to your vehicle!
    PutPlayerInVehicle(playerid, veh, 0);
    SendClientMessage(playerid, 0xFFFFFF, "You just created an awesome vehicle, Thanks to Wesley221's tutorial! ");
    return 1;
}
Well, this was my tutorial! I fully tested this, and it should be working.
Hope you learned something about this
If theres something you dont get, or something that could be done better, please post them.

~Wesley

NOTE: More pictures coming soon!

Credits
Wesley221 --> Making this tutorial
Zeex --> Making ZCMD
JernejL --> Making "REAL Map editor"

Download links
ZCMD
REAL Map editor
Reply
#2

Very nice explained
Reply
#3

Good job.. .
Reply
#4

nice ^^
Reply
#5

Cool
Reply
#6

Pretty nice
Reply
#7

Nice one man. Look forward to adding it to the script to test it out.
Reply
#8

If you want to convert many objects, here there is a website that will convert your createobject in AttachObjectToVehicle

http://agcsigntest.altervista.org/Pa...converter.html

Try to put this in the input box
Код:
CreateObject(3012, 0.18, 2.10, -0.01,   -12.00, -18.00, 18.00);
CreateObject(1012, 38, 14, 2,   2.00, 18.00, 0);
This will output
Код:
new objectid0 = CreateObject(3012, 0.18, 2.10, -0.01, -12.00, -18.00, 18.00) ;
AttachObjectToVehicle(objectid0,vehicleid, 0.18, 2.10, -0.01, -12.00, -18.00, 18.00);

new objectid1 = CreateObject(1012, 38, 14, 2, 2.00, 18.00, 0) ;
AttachObjectToVehicle(objectid1,vehicleid, 38, 14, 2, 2.00, 18.00, 0);
Credits: Me and Roban
Reply
#9

Well done.
Reply
#10

Thanks man great tutorial
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)