Looking for help on my C# code for spawning a bodyguard

Hello!

Basically I want to use the snowball and that when the snowball hits something it spawns a bodyguard there.

Now I have all the code for that done, I just don’t know how to spawn the bodyguard at that position, I tried raycasting but it just spawns it at where I’m aiming not where the snowball hit.

Any help would be much appreciated.

Showing what you have so far would help.

Here is the code so far

using System;
using System.Windows.Forms;
using GTA;
using GTA.Math;
using GTA.Native;
using System.Collections.Generic;

namespace YoshiEgg
{
    public class YoshiEggHandler : Script
    {
        private Ped yoshi;
        private Prop eggProp;
        private bool isThrowingEgg;

        public YoshiEggHandler()
        {
            this.Tick += OnTick;
        }

        private void OnTick(object sender, EventArgs e)
        {
            if (Game.IsKeyPressed(Keys.Y))
            {
                SpawnEgg();
                isThrowingEgg = true;
            }

            if (isThrowingEgg && eggProp != null && eggProp.Exists() && eggProp.IsInAir)
            {
                SphereRaycastResult sphereRaycastResult = RaycastSphere(eggProp.Position, 0.25f);
                if (sphereRaycastResult.DidHitAnything)
                {
                    SpawnYoshi(sphereRaycastResult.WorldCoords);
                    isThrowingEgg = false;
                    eggProp.Delete();
                }
            }
        }

        private void SpawnEgg()
        {
            int weaponHashInt = Function.Call<int>(Hash.GET_HASH_KEY, "WEAPON_SNOWBALL");
            Function.Call(Hash.GIVE_WEAPON_TO_PED, Game.Player.Character.Handle, weaponHashInt, 1, false, true);
            eggProp = World.CreateProp("w_ex_snowball", Game.Player.Character.Position, false, false);
            eggProp.AttachTo(Game.Player.Character.Weapons.CurrentWeaponObject, 0);
        }

        private void SpawnYoshi(Vector3 position)
        {
            yoshi = World.CreatePed(new Model("Yoshi"), position);
            yoshi.Armor = 100;

            PedGroup playerGroup = Game.Player.Character.CurrentPedGroup;
            Function.Call(Hash.SET_PED_AS_GROUP_MEMBER, yoshi, playerGroup);
            Function.Call(Hash.SET_PED_COMBAT_ABILITY, yoshi, 100);
        }

        public SphereRaycastResult RaycastSphere(Vector3 position, float radius)
        {
            RaycastResult raycastResult = World.RaycastCapsule(position - new Vector3(0f, 0f, radius), position + new Vector3(0f, 0f, radius), radius, IntersectOptions.Everything, Game.Player.Character);
            return new SphereRaycastResult
            {
                DidHitAnything = raycastResult.DitHitAnything,
                DidHitEntity = raycastResult.DitHitEntity,
                WorldCoords = raycastResult.HitCoords,
                EntityResult = raycastResult.HitEntity
            };
        }
    }
}

Any help is extremely appreciated!

EDIT:

The code is updated, but now the egg spawns within the player and doesn’t do anything.
This is quite the headache for such a simple thing lol

You need to use the snowball entity position, not the player position, you shouldn’t even need a raycast or anything. I’m into Lua so I can write it in Lua but you’ll have to port it to C#.

local throwingEgg = false
local currentEggs, currentSpawns = {}, {}

SpawnModelAtPosition = function(model, pos)
  if not IsModelInCdimage(model) then print('Model not found in client cd image, returning'); return; end
  if not IsModelValid(model) then print('Model not valid for client, returning'); return; end
  while not HasModelLoaded(model) do RequestModel(model); Citizen.Wait(10); end
  table.insert(currentSpawns, CreatePed(2, model, pos, GetEntityHeading(PlayerPedId()), true, false))
end
  
Citizen.CreateThread(function()
  AddTextEntry('eggthing', 'Press ~INPUT_MP_TEXT_CHAT_TEAM~ to throw egg')
  while true do
    Citizen.Wait(0)
    local wep = GetSelectedPedWeapon(PlayerPedId())
    if wep and wep == `WEAPON_SNOWBALL` then
      DisplayHelpTextThisFrame('eggthing', false)
      if IsControlJustReleased(0, 246) or IsDisabledControlJustReleased(0, 246) then
        throwingEgg = not throwingEgg
        AddTextEntry('eggthing', 'Press ~INPUT_MP_TEXT_CHAT_TEAM~ to throw '..((throwingEgg and 'snowball') or 'egg'))
      end
      if throwingEgg then
        if IsPedShooting(PlayerPedId()) then
          currentEggs[GetClosestObjectOfType(GetEntityCoords(PlayerPedId()), 1.0, `w_ex_snowball`, false, false, false)] = GetEntityCoords(PlayerPedId())
        end
      end
      for k,v in pairs(currentEggs) do
        if DoesEntityExist(k) then
          currentEggs[k] = GetEntityCoords(k)
        else
          SpawnModelAtPosition(`a_c_chimp`, v+vector3(0.0, 0.0, 1.0))
          currentEggs[k] = nil
        end
      end
    end
  end
end)

Much appreciated! Turns out that’s what it was and it was a GTA 5 Native I didn’t know existed lol