以下は、GTA VのScript Hook V .NET (SHVDN3) を使用して、F9キーを押したときにプレイヤーの周囲にボディーガードが出現し、フォーメーションを組んでプレイヤーを守りながら移動するC#スクリプトの例です。このコードはVisual Studioでコンパイルできるように設計されています。
必要な準備
- Script Hook V .NET (SHVDN3) をインストール済みであること。
- Visual Studioで新しいプロジェクトを作成し、
System.Drawing と GTA.Native を参照に追加すること。
- プロジェクトの出力ファイル(.dll)を
scripts フォルダに配置すること。
コード
using System;
using System.Collections.Generic;
using System.Linq;
using GTA;
using GTA.Math;
using GTA.Native;
namespace BodyguardFormation
{
public class BodyguardScript : Script
{
private List<Ped> bodyguards = new List<Ped>();
private bool isBodyguardsActive = false;
public BodyguardScript()
{
Tick += OnTick;
KeyDown += OnKeyDown;
Interval = 0; // Tickイベントの間隔を最短に設定
}
private void OnTick(object sender, EventArgs e)
{
if (!isBodyguardsActive || bodyguards.Count == 0) return;
// プレイヤーの現在位置を取得
Ped player = Game.Player.Character;
Vector3 playerPosition = player.Position;
// ボディーガードのフォーメーションを更新
UpdateBodyguardFormation(playerPosition);
// ボディーガードが死亡した場合、リストから削除
bodyguards.RemoveAll(b => !b.Exists() || b.IsDead);
}
private void OnKeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == System.Windows.Forms.Keys.F9)
{
if (!isBodyguardsActive)
{
SpawnBodyguards();
isBodyguardsActive = true;
}
else
{
DismissBodyguards();
isBodyguardsActive = false;
}
}
}
private void SpawnBodyguards()
{
Ped player = Game.Player.Character;
Vector3 playerPosition = player.Position;
// ボディーガードを生成
for (int i = 0; i < 4; i++)
{
Vector3 spawnPosition = GetFormationPosition(playerPosition, i);
Ped bodyguard = World.CreatePed(PedHash.Armoured, spawnPosition);
if (bodyguard != null)
{
bodyguard.Task.FightAgainstHatedTargets(50f); // 敵に対して攻撃させる
bodyguard.RelationshipGroup = "PLAYER"; // プレイヤーと同じグループに設定
Function.Call(Hash.SET_PED_AS_GROUP_MEMBER, bodyguard.Handle, Game.Player.Group.Handle);
bodyguards.Add(bodyguard);
}
}
}
private void DismissBodyguards()
{
foreach (var bodyguard in bodyguards)
{
if (bodyguard.Exists())
{
bodyguard.Delete();
}
}
bodyguards.Clear();
}
private void UpdateBodyguardFormation(Vector3 playerPosition)
{
float formationRadius = 2.0f; // プレイヤーからの距離
float angleStep = 360.0f / bodyguards.Count;
for (int i = 0; i < bodyguards.Count; i++)
{
if (bodyguards[i].Exists())
{
float angle = angleStep * i;
Vector3 targetPosition = GetFormationPosition(playerPosition, angle);
// ボディーガードをターゲット位置に移動させる
bodyguards[i].Task.GoTo(targetPosition, false, 1.0f);
}
}
}
private Vector3 GetFormationPosition(Vector3 center, float angle)
{
float radians = angle * (float)Math.PI / 180.0f;
float x = center.X + 2.0f * (float)Math.Cos(radians);
float y = center.Y + 2.0f * (float)Math.Sin(radians);
return new Vector3(x, y, center.Z);
}
private Vector3 GetFormationPosition(Vector3 center, int index)
{
float angle = 90.0f * index; // 4人の場合、90度ずつ配置
return GetFormationPosition(center, angle);
}
}
}
説明
-
F9キーでボディーガードを召喚/解散:
- F9キーを押すと、プレイヤーの周囲に4人のボディーガードが出現します。
- 再びF9キーを押すと、ボディーガードが解散します。
-
フォーメーションの維持:
- プレイヤーが移動すると、ボディーガードは円形のフォーメーションを維持しながらプレイヤーを囲むように移動します。
-
敵に対する攻撃:
- ボディーガードは敵に対して自動的に攻撃を行います。
-
死亡時の処理:
- ボディーガードが死亡した場合、リストから削除されます。
エラー対策
-
必要なアセンブリの参照:
System.Drawing と GTA.Native をプロジェクトに追加してください。
-
SHVDN3のバージョン確認:
- 使用しているScript Hook V .NETのバージョンが最新であることを確認してください。
-
エラーが出た場合:
- エラーメッセージを共有いただければ、修正を続けます。
実行方法
- 上記のコードをVisual Studioでプロジェクトに追加し、コンパイルします。
- 出力された
.dllファイルをGTA Vのscriptsフォルダに配置します。
- ゲームを起動し、F9キーを押して動作を確認します。
質問や問題があれば、お知らせください!