18.09.2017, 14:03
It's pretty simple actually.
First, you get the camera position and the front vector. The Vector will have a length of 1.0m so you should multiply all 3 components with the maximum distance you want to check (eg 5 meters).
After that you add the vector to the camera position and you'll get the target position.
vx, vy, vz will then hold the target position.
You can now use ColAndreas to obtain the hit position (if any) for the line between the Camera Position and the Target Position:
If ret is 0, nothing was hit. vx, vy, vz will not change and still hold the target position.
If ret is anything else than 0, the ray hit an object or water and vx, vy, vz will now contain the hit position.
Btw if ret is WATER_OBJECT (20000), it means that the ray hit water.
If you use MapAndreas you will have to use a loop to check for the nearest surface height. But I'd really suggest ColAndreas as it can be used literally anywhere, it's really precise and way faster for this specific task.
Make sure to setup ColAndreas first, see the release topic for a good guide.
First, you get the camera position and the front vector. The Vector will have a length of 1.0m so you should multiply all 3 components with the maximum distance you want to check (eg 5 meters).
After that you add the vector to the camera position and you'll get the target position.
Код:
new Float:cx, Float:cy, Float:cz, Float:vx, Float:vy, Float:vz; GetPlayerCameraPos(playerid, cx, cy, cz); GetPlayerCameraFrontVector(playerid, vx, vy, vz); vx = cx + 5.0 * vx; // Vector length will be increased to 5 meters, then added to the camera position vy = cy + 5.0 * vy; vz = cz + 5.0 * vz;
You can now use ColAndreas to obtain the hit position (if any) for the line between the Camera Position and the Target Position:
Код:
new ret = CA_RayCastLine(cx, cy, cz, vx, vy, vz, vx, vy, vz);
If ret is anything else than 0, the ray hit an object or water and vx, vy, vz will now contain the hit position.
Btw if ret is WATER_OBJECT (20000), it means that the ray hit water.
If you use MapAndreas you will have to use a loop to check for the nearest surface height. But I'd really suggest ColAndreas as it can be used literally anywhere, it's really precise and way faster for this specific task.
Make sure to setup ColAndreas first, see the release topic for a good guide.