gta5…
- User
gta5 f9キーを押し出したら前後ろ左右にボディーガードが出現してフォーメーションを組んで守ってくれるコード作ってまたプレイヤーが動いたらボディーガードもフォーメーションを維持しながらプレイヤーを囲い込みながら歩いてSHVDN3ベースでお願い visualu sudioでコンパイルするからその時に君がコードを書いてエラーが出たらエラーがなくなるまでコードを書き続けて c#で
Assistant
了解しました!SHVDN3(ScriptHookVDotNet3)を使用して、GTA VでF9キーを押したときにプレイヤーの前後左右にボディーガード(ペド)が出現し、フォーメーションを組んでプレイヤーを守りながらついてくるスクリプトをC#で作成しましょう。
以下が完成したコードです。エラーが出ないように、必要なライブラリの参照も含め、段階的に説明していきます。
まず、環境構築:
- Visual Studio をインストール(持ってない場合)。
- ScriptHookVDotNet3 をダウンロードし、
scriptsフォルダに配置(SHVDN3のGitHubから最新版をDL)。 - Visual Studioで新しいプロジェクトを作成:
Class Library (.NET Framework)を選択。- .NET Frameworkは4.8 を選択(SHVDN3はこれで動く)。
- 必要な参照を追加:
ScriptHookVDotNet3.dll(DLしたSHVDN3の中にある)を参照に追加。- 同様に
ScriptHookVDotNet3.xmlも同じ場所に置く(インテリセンス用)。 System.Windows.Formsも参照に追加(キー入力検知のため)。System.Drawing(オプション、今回は使いませんが念のため)。
では、以下がコードです。順を追って説明します。
using System; using System.Windows.Forms; using GTA; using GTA.Native; namespace BodyGuardFormation { public class BodyGuardScript : Script { // ボディーガードのペドハンドルを保持 private Ped[] bodyGuards = new Ped[4]; // 前後左右の4人 private bool isFormationActive = false; // フォーメーション有効フラグ private Ped player => Game.Player.Character; // プレイヤーペド取得用プロパティ public BodyGuardScript() { // ゲーム内の時間をチェックするTickイベントを有効化 Tick += OnTick; Interval = 100; // 100msごとに処理(軽量化) } // メインループ(100msごとに呼ばれる) private void OnTick(object sender, EventArgs e) { // F9キーでフォーメーションの有効/無効を切り替え if (Game.IsKeyPressed(Keys.F9)) { ToggleBodyGuardFormation(); Wait(200); // 連打防止用ウェイト } // フォーメーションが有効なときだけ処理 if (isFormationActive) { MaintainFormation(); } } // フォーメーションの有効/無効切り替え private void ToggleBodyGuardFormation() { if (!isFormationActive) { CreateBodyGuards(); // ボディーガード生成 isFormationActive = true; UI.Notify("~g~BodyGuard Formation Activated!"); } else { RemoveBodyGuards(); // ボディーガード削除 isFormationActive = false; UI.Notify("~r~BodyGuard Formation Deactivated!"); } } // ボディーガードを生成 private void CreateBodyGuards() { // プレイヤーのモデルによっては関係なく「ig_barry」のモデルを使用(太ったNPC) Model guardModel = new Model(PedHash.Barry); if (guardModel.Request(1000)) // モデル読み込み待ち(1秒) { Vector3 playerPos = player.Position; // 前後左右に配置(距離2.0単位) bodyGuards[0] = World.CreatePed(guardModel, playerPos + player.ForwardVector * 2f); // 前 bodyGuards[1] = World.CreatePed(guardModel, playerPos - player.ForwardVector * 2f); // 後ろ bodyGuards[2] = World.CreatePed(guardModel, playerPos + player.RightVector * 2f); // 右 bodyGuards[3] = World.CreatePed(guardModel, playerPos + player.LeftVector * 2f); // 左 foreach (Ped guard in bodyGuards) { if (guard != null && guard.IsAlive) { // 各ボディーガードの設定 guard.CanRagdoll = false; // ダウンしない guard.BlockPermanentEvents = true; // イベント無視 guard.CanBeDamagedByRelationshipGroup = false; // 同グループの攻撃を受けない guard.RelationshipGroup = RelationshipGroup.Companion; // 味方グループ guard.Task.FightAgainst(player); // 攻撃されたら反撃する(プレイヤーを守る) Function.Call(Hash.SET_PED_COMBAT_ABILITY, guard, 100); // 戦闘スキルMAX Function.Call(Hash.SET_PED_COMBAT_MOVEMENT, guard, 2); // 動き回りながら戦闘 Function.Call(Hash.SET_PED_HEARING_RANGE, guard, 9999f); // 遠くの音も聞こえる Function.Call(Hash.SET_PED_VISUAL_FIELD_HEARING_RANGE, guard, 9999f); // 視野も広く } } } else { UI.Notify("~r~Failed to load BodyGuard model!"); isFormationActive = false; } } // ボディーガードを削除 private void RemoveBodyGuards() { foreach (Ped guard in bodyGuards) { if (guard != null && guard.Exists()) { guard.Delete(); // 消去 } } bodyGuards = new Ped[4]; // 配列リセット } // フォーメーション維持処理(プレイヤーについてくる) private void MaintainFormation() { Vector3 playerPos = player.Position; float playerHeading = player.Heading; // プレイヤーの向き(角度) // 前(プレイヤーの前方2.0単位) if (bodyGuards[0] != null && bodyGuards[0].Exists()) { Vector3 targetPos = playerPos + (player.ForwardVector * 2f); bodyGuards[0].Task.GoTo(targetPos, true, 500); // 0.5秒で到着 bodyGuards[0].Heading = playerHeading; // プレイヤーと同じ向き } // 後ろ(プレイヤーの後方2.0単位) if (bodyGuards[1] != null && bodyGuards[1].Exists()) { Vector3 targetPos = playerPos - (player.ForwardVector * 2f); bodyGuards[1].Task.GoTo(targetPos, true, 500); bodyGuards[1].Heading = playerHeading; } // 右(プレイヤーの右2.0単位) if (bodyGuards[2] != null && bodyGuards[2].Exists()) { Vector3 targetPos = playerPos + (player.RightVector * 2f); bodyGuards[2].Task.GoTo(targetPos, true, 500); bodyGuards[2].Heading = playerHeading; } // 左(プレイヤーの左2.0単位) if (bodyGuards[3] != null && bodyGuards[3].Exists()) { Vector3 targetPos = playerPos - (player.RightVector * 2f); bodyGuards[3].Task.GoTo(targetPos, true, 500); bodyGuards[3].Heading = playerHeading; } // 万が一死んだボディーガードがいたら削除&再生成 for (int i = 0; i < bodyGuards.Length; i++) { if (bodyGuards[i] == null || !bodyGuards[i].IsAlive) { if (bodyGuards[i] != null) bodyGuards[i].Delete(); Model guardModel = new Model(PedHash.Barry); if (guardModel.Request(500)) { switch (i) { case 0: bodyGuards[0] = World.CreatePed(guardModel, playerPos + player.ForwardVector * 2f); break; case 1: bodyGuards[1] = World.CreatePed(guardModel, playerPos - player.ForwardVector * 2f); break; case 2: bodyGuards[2] = World.CreatePed(guardModel, playerPos + player.RightVector * 2f); break; case 3: bodyGuards[3] = World.CreatePed(guardModel, playerPos - player.RightVector * 2f); break; } // 再設定 if (bodyGuards[i] != null) { bodyGuards[i].Task.FightAgainst(player); Function.Call(Hash.SET_PED_COMBAT_ABILITY, bodyGuards[i], 100); } } } } } // ウェイト関数(キー連打対策) private async void Wait(int milliseconds) { await System.Threading.Tasks.Task.Delay(milliseconds); } } }このコードのポイント解説:
Tickイベント で 100msごとに処理 を実行(軽量化のため)。- F9キー を押すと
ToggleBodyGuardFormation()が呼ばれ、ボディーガードの 生成/削除 が行われる。 CreateBodyGuards()で 前後左右 にペド(ig_barryモデル)を生成。MaintainFormation()で プレイヤーの動きに合わせてフォーメーション維持。- 各ボディーガードに
Task.GoTo()で移動指示出し、常にプレイヤーの相対位置に配置。 - 死んだら自動で 再生成 する仕組み付き。
- 各ボディーガードに
- 戦闘能力を MAX に設定 (
SET_PED_COMBAT_ABILITY)。 - プレイヤーを守るよう
FightAgainst()を指示。
ビルド&配置方法:
- Visual Studioで Releaseモード に設定。
BodyGuardScript.csという名前で上記コードを保存。- プロジェクトを ビルド (メニューの
ビルド→ソリューションのビルド)。 - 生成された
BodyGuardFormation.dllを GTA5のscriptsフォルダ に配置。- 例:
C:\Users\ユーザー名\Documents\Rockstar Games\GTA V\scripts\
- 例:
- GTA5を起動 し、ゲーム内で F9キー を押すとボディーガードが出現。
エラーが出る場合のチェックポイント:
ScriptHookVDotNet3.dllが参照に追加されているか。- .NET Framework 4.8 を使っているか。
using GTA;とusing GTA.Native;がちゃんとあるか。System.Windows.Formsの参照追加忘れがないか。- ビルド時に
dllが生成されているか(bin/Releaseフォルダを確認)。
エラー内容があれば貼り付けてください。随時修正します!
これで動けば、F9キーで4人のボディーガードがプレイヤーを囲んでついてくるはずです!
追加メモ:
- ボディーガードのモデルは
PedHash.Barryですが、他のモデルに変更したい場合はPedHashの部分を変えてください(例:PedHash.Alice、PedHash.Security01SMMなど)。 - 距離(2.0fの部分)や戦闘スキルも
SET_PED_COMBAT_ABILITYの値を0~100で調整可能。
では、GTA Vでテストしてみてください!うまくいくはずです!🎉
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.