以下は、GTA5でF9キーを押すとボディーガードが出現し、プレイヤーを囲むようにフォーメーションを維持しながら移動するスクリプトのC#コード例です。このコードはScriptHookV .NETを使用しています。
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using GTA;
using GTA.Math;
using GTA.Native;
public class BodyguardScript : Script
{
private List<Ped> bodyguards = new List<Ped>();
private bool isActive = false;
private Blip[] guardBlips = new Blip[4];
private Vector3[] formationOffsets = new Vector3[4]
{
new Vector3(0f, 2f, 0f), // 前
new Vector3(-2f, 0f, 0f), // 左
new Vector3(0f, -2f, 0f), // 後ろ
new Vector3(2f, 0f, 0f) // 右
};
public BodyguardScript()
{
Tick += OnTick;
KeyDown += OnKeyDown;
Interval = 100;
}
private void OnKeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F9)
{
if (!isActive)
{
SpawnBodyguards();
isActive = true;
UI.Notify("ボディーガードが出現しました");
}
else
{
RemoveBodyguards();
isActive = false;
UI.Notify("ボディーガードを解除しました");
}
}
}
private void SpawnBodyguards()
{
RemoveBodyguards(); // 既存のボディーガードを削除
Ped playerPed = Game.Player.Character;
Vector3 playerPos = playerPed.Position;
float heading = playerPed.Heading;
for (int i = 0; i < 4; i++)
{
// オフセットをプレイヤーの向きに合わせて回転
Vector3 offset = RotateVector(formationOffsets[i], heading);
Vector3 spawnPos = playerPos + offset;
// ボディーガードを生成
Ped bodyguard = World.CreatePed(PedHash.Clay, spawnPos);
if (bodyguard != null && bodyguard.Exists())
{
bodyguard.IsInvincible = true;
bodyguard.CanRagdoll = false;
bodyguard.BlockPermanentEvents = true;
bodyguard.Weapons.Give(WeaponHash.Pistol, 500, true, true);
bodyguard.Task.FightAgainst(playerPed);
// ダイヤモンドのブリップを追加
guardBlips[i] = bodyguard.AddBlip();
guardBlips[i].Sprite = BlipSprite.Standard;
guardBlips[i].Color = BlipColor.Blue;
guardBlips[i].Scale = 0.7f;
guardBlips[i].Name = "ボディーガード";
bodyguards.Add(bodyguard);
}
}
}
private Vector3 RotateVector(Vector3 vector, float angle)
{
float rad = angle * (float)Math.PI / 180f;
float cos = (float)Math.Cos(rad);
float sin = (float)Math.Sin(rad);
return new Vector3(
vector.X * cos - vector.Y * sin,
vector.X * sin + vector.Y * cos,
vector.Z
);
}
private void RemoveBodyguards()
{
foreach (Ped guard in bodyguards)
{
if (guard != null && guard.Exists())
{
guard.Delete();
}
}
for (int i = 0; i < 4; i++)
{
if (guardBlips[i] != null && guardBlips[i].Exists())
{
guardBlips[i].Remove();
}
}
bodyguards.Clear();
}
private void OnTick(object sender, EventArgs e)
{
if (!isActive || bodyguards.Count == 0) return;
Ped playerPed = Game.Player.Character;
Vector3 playerPos = playerPed.Position;
float heading = playerPed.Heading;
for (int i = 0; i < bodyguards.Count; i++)
{
if (i >= 4) break; // 安全策
Ped guard = bodyguards[i];
if (guard == null || !guard.Exists()) continue;
// オフセットをプレイヤーの向きに合わせて回転
Vector3 offset = RotateVector(formationOffsets[i], heading);
Vector3 targetPos = playerPos + offset;
// ボディーガードが死んでいないか確認
if (guard.IsDead)
{
// 新しいボディーガードをスポーン
guard.Delete();
bodyguards[i] = World.CreatePed(PedHash.Clay, targetPos);
if (bodyguards[i] != null)
{
bodyguards[i].IsInvincible = true;
bodyguards[i].CanRagdoll = false;
bodyguards[i].BlockPermanentEvents = true;
bodyguards[i].Weapons.Give(WeaponHash.Pistol, 500, true, true);
}
continue;
}
// ボディーガードを目標位置に移動
if (guard.Position.DistanceTo(targetPos) > 3f)
{
guard.Task.GoTo(targetPos);
}
else
{
guard.Task.StandStill(-1);
guard.Heading = heading;
}
}
}
}
コードの説明:
-
F9キーでボディーガードの出現/解除:
- F9キーを押すと、4人のボディーガードが出現します。
- 再度F9キーを押すとボディーガードが消えます。
-
フォーメーション:
- ボディーガードはプレイヤーの前、左、後ろ、右に配置されます。
- プレイヤーが移動すると、ボディーガードもフォーメーションを維持しながら移動します。
-
ボディーガードの特徴:
- 無敵設定で、ラグドールになりません。
- ピストルを装備しています。
- ダイヤモンド形のブルーのブリップが表示されます。
-
注意点:
- このコードを使用するには、GTA5にScriptHookVとScriptHookV .NETがインストールされている必要があります。
- 必要に応じてボディーガードのモデル(PedHash.Clay)や武器(WeaponHash.Pistol)を変更できます。
このスクリプトを適切な場所に配置し、ゲーム内でF9キーを押すとボディーガードが出現します。