tonyism Posted May 31, 2018 Share Posted May 31, 2018 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. Link to comment Share on other sites More sharing options...
NoNameSet Posted May 31, 2018 Share Posted May 31, 2018 couldn't you try raycasting from the trailer and see if the vehicle hit can be attached? Link to comment Share on other sites More sharing options...
Guest Posted May 31, 2018 Share Posted May 31, 2018 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. Link to comment Share on other sites More sharing options...
ItsiAdam Posted May 31, 2018 Share Posted May 31, 2018 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. Link to comment Share on other sites More sharing options...
Guest Posted May 31, 2018 Share Posted May 31, 2018 (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. 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 May 31, 2018 by Guest Correction Link to comment Share on other sites More sharing options...
Guest Posted May 31, 2018 Share Posted May 31, 2018 (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 June 1, 2018 by Guest Additional info added Link to comment Share on other sites More sharing options...
Guest Posted June 2, 2018 Share Posted June 2, 2018 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. Link to comment Share on other sites More sharing options...
NModds Posted June 2, 2018 Share Posted June 2, 2018 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. Link to comment Share on other sites More sharing options...
Guest Posted June 2, 2018 Share Posted June 2, 2018 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 ) 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. Link to comment Share on other sites More sharing options...
NModds Posted June 3, 2018 Share Posted June 3, 2018 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 ) 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. Link to comment Share on other sites More sharing options...
Guest Posted June 3, 2018 Share Posted June 3, 2018 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. Link to comment Share on other sites More sharing options...