Quantcast
Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
    1. Welcome to GTAForums!

    2. News

    1. Red Dead Redemption 2

      1. Gameplay
      2. Missions
      3. Help & Support
    2. Red Dead Online

      1. Gameplay
    1. GTA Online

      1. After Hours
      2. Find Lobbies & Players
      3. Guides & Strategies
      4. Vehicles
      5. Content Creator
      6. Help & Support
    2. Crews

      1. Events
      2. Recruitment
    1. Grand Theft Auto Series

    2. GTA Next

    3. GTA V

      1. PC
      2. Guides & Strategies
      3. Help & Support
    4. GTA IV

      1. Episodes from Liberty City
      2. Multiplayer
      3. Guides & Strategies
      4. Help & Support
      5. GTA Mods
    5. GTA Chinatown Wars

    6. GTA Vice City Stories

    7. GTA Liberty City Stories

    8. GTA San Andreas

      1. Guides & Strategies
      2. Help & Support
      3. GTA Mods
    9. GTA Vice City

      1. Guides & Strategies
      2. Help & Support
      3. GTA Mods
    10. GTA III

      1. Guides & Strategies
      2. Help & Support
      3. GTA Mods
    11. Top Down Games

      1. GTA Advance
      2. GTA 2
      3. GTA
    12. Wiki

      1. Merchandising
    1. GTA Modding

      1. GTA V
      2. GTA IV
      3. GTA III, VC & SA
      4. Tutorials
    2. Mod Showroom

      1. Scripts & Plugins
      2. Maps
      3. Total Conversions
      4. Vehicles
      5. Textures
      6. Characters
      7. Tools
      8. Other
      9. Workshop
    3. Featured Mods

      1. DYOM
      2. OpenIV
      3. GTA: Underground
      4. GTA: Liberty City
      5. GTA: State of Liberty
    1. Red Dead Redemption

    2. Rockstar Games

    1. Off-Topic

      1. General Chat
      2. Gaming
      3. Technology
      4. Programming
      5. Movies & TV
      6. Music
      7. Sports
      8. Vehicles
    2. Expression

      1. Graphics / Visual Arts
      2. GFX Requests & Tutorials
      3. Writers' Discussion
      4. Debates & Discussion
    1. Forum Support

    2. Site Suggestions

tonyism

Trying to get the nearest vehicle in front of player.

Recommended Posts

tonyism

I cant seem to figure out how to return the vehicle in front of mine. Is there something built in I can use? Trailers need to find the vehicle in front of them. Any help would really be appreciated.

Share this post


Link to post
Share on other sites
NoNameSet

couldn't you try raycasting from the trailer and see if the vehicle hit can be attached?

Share this post


Link to post
Share on other sites
LeeC2202

As far as I know, you can't drive a trailer, so it can't be the player's vehicle. You can check if a vehicle is attached to a trailer with IS_VEHICLE_ATTACHED_TO_TRAILER and you can get the trailer with GET_VEHICLE_TRAILER_VEHICLE. So everything is done based on the vehicle you can drive (or are driving). I have a small TrailerInfo script that I created to detect trailers at one point, which is how I found those natives. The only other two natives related to trailers are to do with ATTACH or DETACH.

If you pass a trailer to this function, it will return the vehicle that is directly in front of it.

private Vehicle GetPullingVehicle(Vehicle trailer)
{
	Vector3 minimumDimensions;
	Vector3 maximumDimensions;

	trailer.Model.GetDimensions(out minimumDimensions, out maximumDimensions);
	Vector3 checkingPosition = trailer.GetOffsetInWorldCoords(new Vector3(0, maximumDimensions.Y, minimumDimensions.Z));
	return World.GetClosestVehicle(checkingPosition, 2f);
}

Raycasting would work fine as well, you'd just have to be careful of anything trying to connect at an angle, which avoided being detected. If you used the raycast with a radius, that would help with that problem.

Share this post


Link to post
Share on other sites
ItsiAdam

If you're using SHV/DN, you could loop over all the vehicles in the world and get their distance too. Probably what that native does.

Share this post


Link to post
Share on other sites
LeeC2202
Posted (edited)
3 hours ago, ItsiAdam said:

If you're using SHV/DN, you could loop over all the vehicles in the world and get their distance too. Probably what that native does.

I think it does... what I posted probably wasn't the best solution in hindsight as distance checking the vehicle collection takes time.

What you could do, is set the checking location to Vector3(0, maximumDimensions.Y + .2f, 0) and then RayCast directly downwards from that point. That should only detect the truck pulling the trailer, no matter which way it is facing... unless you drive a car in front of the trailer of course. :D But that would eliminate the distance checking, which is good as that's probably already been done once anyway, just to collect the trailers.

@OP What I forgot to add in that post I made, is that after you get the vehicle, you have to use the two natives I mentioned on that vehicle, to see if the trailer is actually attached to that particular vehicle. So doing that with the adjusted RayCast would probably get you a guaranteed result.

Edited by LeeC2202
Correction

Share this post


Link to post
Share on other sites
LeeC2202
Posted (edited)

After doing some testing on these methods, there isn't one that really stands out as being more reliable than the other.

Raycasting works as long as the truck is mostly ahead of the trailer, setting two raycasts off at +45 degrees and -45 degrees gives better coverage but a tight turn will render them both null.

Detecting in a radius from a front checking point is okay but again, one the truck turns to the right or left, it goes out of detection range unless you have a wider radius. The problem with a wider radius is that there is a danger of catching other vehicles beside the checking point. Because the Phantom is so long, you need a wide radius to catch it anyway, so it's even worse when it turns to either side.

My idea of RayCasting downwards was actually pretty useless when I thought about it, as there are too many gaps in the chassis of the trucks... plus when you turned extreme left or right, there would be nothing to detect at all. So good in theory, terrible in practice.

The way to make both of these more reliable, would be to rotate the relative checking point based on the rotational velocity of the trailer. So the more it is turning left, the more you rotate the checking point around to the left. But of course, that falls down the minute the truck stops. It's also complicated if the trailer is moving forward but rotating in the reverse direction, as it would do in an almost jack-knife situation.

There's a script file here on my Google Drive that shows both methods, it's a plain .cs file, so you can see exactly what it is doing. It doesn't have the rotating checking point but if you try it with each checking type, you can see how reliable each is. https://drive.google.com/file/d/1q_TkRgp64Orz-EXQbi_oeFZPUCBegjJe/view?usp=sharing

Edit: I probably should have mentioned that this script works with the player's vehicle. So Spawn a trailer, hook up a truck and it will identify the truck based on the trailer's position. As you turn, you will see when it starts and stops detecting the vehicle by the text at the bottom appearing/disappearing.

Edited by LeeC2202
Additional info added

Share this post


Link to post
Share on other sites
LeeC2202

So am I just talking to myself in here now? If this solves the problem then great but at least have the courtesy to acknowledge people who spend their own time trying to find solutions for your problems.

Share this post


Link to post
Share on other sites
NModds
2 hours ago, LeeC2202 said:

So am I just talking to myself in here now? If this solves the problem then great but at least have the courtesy to acknowledge people who spend their own time trying to find solutions for your problems.

Idk why Op doesn't acknowledge your hard work yet, you go all out when helping a person and just to let you know, it's appreciated. It will definitely help future modders looking for a solution.   

Share this post


Link to post
Share on other sites
LeeC2202
1 hour ago, nm710 said:

Idk why Op doesn't acknowledge your hard work yet, you go all out when helping a person and just to let you know, it's appreciated. It will definitely help future modders looking for a solution.   

Well my philosophy is "The more people know, the more people create", so information always has its uses.

But it was a couple of hours of my time, (mainly because I'm a slow coder that writes more bugs than is normal :blush:) so it's nice to know that the time spent had some purpose.

More and more sites seem to have a problem with hit-and-run posters, where they ask a question and are never seen again. It makes you wary of helping anyone though, so the community starts to suffer because of it. IDK, I should just stop worrying about it I guess.

Share this post


Link to post
Share on other sites
NModds
11 hours ago, LeeC2202 said:

Well my philosophy is "The more people know, the more people create", so information always has its uses.

But it was a couple of hours of my time, (mainly because I'm a slow coder that writes more bugs than is normal :blush:) so it's nice to know that the time spent had some purpose.

More and more sites seem to have a problem with hit-and-run posters, where they ask a question and are never seen again. It makes you wary of helping anyone though, so the community starts to suffer because of it. IDK, I should just stop worrying about it I guess.

That's a great philosophy, I wouldn't know anything if people didn't post in the forums. Yeah it does have a negative effect on the community and it's only natural to want at least a thank you or equivalence of a like for the time spent helping them. 

Share this post


Link to post
Share on other sites
LeeC2202

Well, I am going to consider this thread dead, I'm going to unfollow the thread and ignore the user, same with the other thread I answered yesterday.

 

At least that way I won't get tempted to help these people again and hopefully their names will stick in the minds of other people who might fall for their post-and-run tactics.

Share this post


Link to post
Share on other sites

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

  • 1 User Currently Viewing
    0 Members, 0 Anonymous, 1 Guest

×

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.