Log in to reply
 

.net Teleport to random location code not working



  • I'm trying to teleport my entity (me, the player) at a random location. X and Y are straightforward enough but Z is tripping me really hard. I can't figure out where the ground is.

    I downloaded the PlayerTpController.cpp from chaos mod as I know there's a random teleport there and for the most part I reversed engineered the function in C# apart form the line that figures out where Z should be.

    It just can't figure out where Z is in the world ...

     public static unsafe void TpToRandom()
        {
            bool isonGround=false;
            Ped playerPed = Game.Player.Character;
            Vector3 playerPos = Function.Call<Vector3>(Hash.GET_ENTITY_COORDS, playerPed, false);
            Random rnd = new Random();
            float x, y, z= playerPos.Z, _;
            float groundZ = 800;
    
            do
            {
                x = rnd.Next(-3747, 4500);
                y = rnd.Next(-4400, 8022);
            }
            while (Function.Call<bool>(Hash.TEST_VERTICAL_PROBE_AGAINST_ALL_WATER, x, y, z, 0, &_));
    
            for (int i = 0; i < 200; i++)
            {
                float testZ = (float)((i * 5) - 100);
    
                TeleportPlayer(x, y, testZ);
                if(i%5==0)
                {
                    Task.Delay(0);
                }
    
                isonGround = Function.Call<bool>(Hash.GET_GROUND_Z_FOR_3D_COORD, x, y, testZ, &groundZ, false,false);
    
                if (isonGround)
                {                   
                    break;
                }
            }
            if (!isonGround)
            {
                TeleportPlayer(x, y, isonGround ? groundZ : z);
            }
        }
    

    It usually always goes through the full loop and TPs me high up, SOMETIMES though it TPs me bellow the map ...





  • @JohnFromGWN
    Which bit ? I don't see a line that does anything to do with Z at the new random location that's on ground ?

    pos = Game.Player.Character.Position.Around(1500.0F)
    //Get the current player position
    pos.Z = Game.Player.Character.Position.Z + 5F ' five meters above player
    //Get the plyaer Z position and add 5 to it, so pos.Z is now above the player

    //The rest creates the titan and gives player a weapon or sets him as the driver
    v = World.CreateVehicle("Titan", pos)
    checkdist()
    If isclose = False Then
    pedselect()
    s1 = v.CreatePedOnSeat(VehicleSeat.Driver, "s_m_y_Marine_03")
    weapselect()
    s1.Weapons.Give(weaphash, 999, True, True)
    s1.MarkAsNoLongerNeeded()



  • Seems my problem is I can't get the game to return a proper Z

                OutputArgument outputArgument = new OutputArgument();
                isonGround = Function.Call<bool>((Hash.GET_GROUND_Z_FOR_3D_COORD), new InputArgument[5]
            {
                (x),
                (y),
                (testZ),
                outputArgument,
                (false)
            });
            groundZ = outputArgument.GetResult<float>();
    

    groundZ is always 0



  • If the position is too far away from the player you won't be able to get the ground height, which is why you see Chaos Mod teleport the player before it knows the Z coordinate.



  • @Jitnaught Chaos goes through a loop teleporting the player many times and that's what I tried replicating but the GET_GROUND_Z_FOR_3D_COORD from my last comment never returns true and the outputArgument that it's supposed to return is also always 0, even though it teleports me 200 times, 5 units higher than the previous one every time.


Log in to reply
 

Looks like your connection to GTA5-Mods.com Forums was lost, please wait while we try to reconnect.