SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 31/10/2019, 10:46 PM   #1
Unte99
Gangsta
 
Unte99's Avatar
 
Join Date: Jul 2010
Location: Lithuania
Posts: 629
Reputation: 65
Default [SOLVED] Tow truck AttachTrailerToVehicle

Problem soved. Had another script that was meant for trucks to detach trailers, because in sa-mp there is no default button to detach trailers for trucks. Everything is working fine.

I made a script for towing cars. Made some code to detect if the tow trucks rear is close/aligned to another vehicles front bumper. When I try to attach the car to the tow truck the other car just changes its z angle like it is aligned to the tow truck but it does not actually attach itself to the tow truck. The function does not work anymore in 0.3.DL-R1? I debugged the script the hell out of it and everything seems to be working as intended, it's only to do something with the AttachTrailerToVehicle function as it does not attach a car to the tow truck (525).

pawn Code:
if(PRESSED(KEY_SUBMISSION))
    {
        if(IsPlayerInAnyVehicle(playerid))
        {
            new towtruckid, trailerid, Float:atstumas, Float:mazatstumas;
            towtruckid=GetPlayerVehicleID(playerid);
            if(GetVehicleModel(towtruckid) == 525)
            {
                if(GetPlayerState(playerid) == PLAYER_STATE_DRIVER)
                {
                    if(IsTrailerAttachedToVehicle(towtruckid))
                    {
                        print("Atkabinimas suveikė");
                        DetachTrailerFromVehicle(towtruckid);
                    }
                    else if(!IsTrailerAttachedToVehicle(towtruckid))
                    {
                        new Float:masinosp[3], Float:masinosp2[3], Float:mdydis[3], Float:mdydis2[3], Float:bamperis[2];
                        mazatstumas=-1;
                        GetVehiclePosEx(towtruckid, masinosp[0], masinosp[1], masinosp[2], 0.0, -3.3, 0.36);
                        //GetPosBehindVehicle(towtruckid, masinosp[0], masinosp[1], masinosp[2], 3.2);
                        for(new i; i < MAX_VEHICLES; i++)
                        {
                            if(IsVehicleStreamedIn(i, playerid))
                            {
                                print("Suveikė 1");
                                if(towtruckid!=i)
                                {
                                    print("Suveikė 2");
                                    GetVehiclePos(i, masinosp2[0], masinosp2[1], masinosp2[2]);
                                    if(IsPointInRangeOfPoint(masinosp[0], masinosp[1], masinosp[2], masinosp2[0], masinosp2[1], masinosp2[2], 4.0))
                                    {
                                        print("Suveikė 3");
                                        GetVehicleModelInfo(GetVehicleModel(i), VEHICLE_MODEL_INFO_FRONT_BUMPER_Z, mdydis2[0], mdydis2[1], bamperis[0]);
                                        printf("mdydis2[0]=%f mdydis2[1]=%f bamperis[0]=%f", mdydis2[0], mdydis2[1], bamperis[0]);
                                        GetVehicleModelInfo(GetVehicleModel(i), VEHICLE_MODEL_INFO_SIZE, mdydis2[0], mdydis2[1], mdydis2[2]);
                                        printf("mdydis2[0]=%f mdydis2[1]=%f mdydis2[2]=%f", mdydis2[0], mdydis2[1], mdydis2[2]);
                                        mdydis2[1]=mdydis2[1]/2;
                                        printf("mdydis2[1]=%f", mdydis2[1]);
                                        GetVehiclePosEx(i, masinosp2[0], masinosp2[1], masinosp2[2], 0.0, 3.2, bamperis[0]);
                                        printf("masinosp2[0]=%f masinosp2[1]=%f masinosp2[2]=%f", masinosp2[0], masinosp2[1], masinosp2[2]);
                                        //CreateDynamicObject(16101, masinosp2[0], masinosp2[1], masinosp2[2], 0.0, 0.0, 0.0);
                                        Create3DTextLabel("Antra mašina", 0x008080FF, masinosp2[0], masinosp2[1], masinosp2[2], 40.0, 0, 0);
                                        GetVehicleModelInfo(GetVehicleModel(towtruckid), VEHICLE_MODEL_INFO_REAR_BUMPER_Z, mdydis[0], mdydis[1], bamperis[1]);
                                        printf("mdydis[0]=%f mdydis[1]=%f bamperis[1]=%f", mdydis[0], mdydis[1], bamperis[1]);
                                        GetVehicleModelInfo(GetVehicleModel(towtruckid), VEHICLE_MODEL_INFO_SIZE, mdydis[0], mdydis[1], mdydis[2]);
                                        printf("mdydis[0]=%f mdydis[1]=%f mdydis[2]=%f", mdydis[0], mdydis[1], mdydis[2]);
                                        mdydis[1]=mdydis[1]/2;
                                        printf("mdydis[1]=%f", mdydis[1]);
                                        GetVehiclePosEx(towtruckid, masinosp[0], masinosp[1], masinosp[2], 0.0, -3.2, bamperis[0]);
                                        printf("masinosp[0]=%f masinosp[1]=%f masinosp[2]=%f", masinosp[0], masinosp[1], masinosp[2]);
                                        //CreateDynamicObject(16101, masinosp[0], masinosp[1], masinosp[2], 0.0, 0.0, 0.0);
                                        Create3DTextLabel("Pirma mašina", 0x008080FF, masinosp[0], masinosp[1], masinosp[2], 40.0, 0, 0);
                                        atstumas=GetDistanceBetweenPoints(masinosp[0], masinosp[1], masinosp[2], masinosp2[0], masinosp2[1], masinosp2[2]);
                                        printf("atstumas=%f",atstumas);
                                        if(atstumas <= 4)
                                        {
                                            print("Suveikė 4");
                                            if(mazatstumas == -1)
                                            {
                                                print("Suveikė 5");
                                                mazatstumas=atstumas;
                                                trailerid=i;
                                            }
                                            if(mazatstumas > atstumas)
                                            {
                                                print("Suveikė 6");
                                                mazatstumas=atstumas;
                                                trailerid=i;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if(mazatstumas!=-1)
                    {
                        print("Suveikė prikabinimas");
                        AttachTrailerToVehicle(trailerid, towtruckid);
                    }
                }
            }
        }
    }

pawn Code:
stock GetVehiclePosEx(vid, &Float:px, &Float:py, &Float:pz, Float:offsetx = 0.0, Float:offsety = 0.0, Float:offsetz = 0.0)
{
    new
        Float:rx, Float:ry, Float:rz,
        Float:sx, Float:sy, Float:sz,
        Float:cy, Float:cx, Float:cz;
    GetVehiclePos(vid, px, py, pz);
    GetVehicleRotation(vid, rx, ry, rz);
    sx = floatsin(rx, degrees),
    sy = floatsin(ry, degrees),
    sz = floatsin(rz, degrees),
    cx = floatcos(rx, degrees),
    cy = floatcos(ry, degrees),
    cz = floatcos(rz, degrees);
    if (offsetx)
    {
        px = px + offsetx * (cy * cz - sx * sy * sz);
        py = py + offsetx * (cz * sx * sy + cy * sz);
        pz = pz - offsetx * (cx * sy);
    }
    if (offsety)
    {
        px = px - offsety * (cx * sz);
        py = py + offsety * (cx * cz);
        pz = pz + offsety * (sx);
    }
    if (offsetz)
    {
        px = px + offsetz * (cz * sy + cy * sx * sz);
        py = py - offsetz * (cy * cz * sx + sy * sz);
        pz = pz + offsetz * (cx * cy);
    }
    return 1;
}

Last edited by Unte99; 05/11/2019 at 12:50 PM.
Unte99 is offline   Reply With Quote
Old 31/10/2019, 11:01 PM   #2
MP2
Godfather
 
MP2's Avatar
 
Join Date: Jan 2011
Location: England
Posts: 6,230
Reputation: 993
Default Re: Tow truck AttachTrailerToVehicle

Is there a driver in the to-be-towed vehicle? You cannot tow a vehicle with a driver.

If not please show the code.
__________________
Click banner above for more info. View trailers here.
Enter referral code SAMPFORUM after registering for a $5000 starting bonus!
MP2 is offline   Reply With Quote
Old 31/10/2019, 11:22 PM   #3
Unte99
Gangsta
 
Unte99's Avatar
 
Join Date: Jul 2010
Location: Lithuania
Posts: 629
Reputation: 65
Default Re: Tow truck AttachTrailerToVehicle

Quote:
Originally Posted by MP2 View Post
Is there a driver in the to-be-towed vehicle? You cannot tow a vehicle with a driver.

If not please show the code.
Updated original post. I'm testing this on an empty server. So no. The vehicle is empty.
Unte99 is offline   Reply With Quote
Old 02/11/2019, 07:58 PM   #4
Unte99
Gangsta
 
Unte99's Avatar
 
Join Date: Jul 2010
Location: Lithuania
Posts: 629
Reputation: 65
Default Re: Tow truck AttachTrailerToVehicle

Can anyone test this code on their server and check if the towing works? And if it works, tell me what server version you are using.
Unte99 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
AttachTrailerToVehicle Karolukas123 Scripting Help 1 26/11/2015 10:28 PM
AttachTrailerToVehicle - how to ? arretdefil Scripting Help 6 25/09/2015 10:24 PM
AttachTrailerToVehicle Buzzbomb Scripting Help 1 06/12/2012 07:59 PM
Need help at AttachTrailerToVehicle. CoaPsyFactor Help Archive 5 02/01/2011 03:25 PM
AttachTrailerToVehicle zanakinz Help Archive 9 27/03/2009 11:05 PM


All times are GMT. The time now is 07:41 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.