To make the chicken jump at the player when the distance is less than 300, you need to implement a method to initiate the jump and modify the existing code inside the timer callback. Here's how you can achieve it:
- Implement a method
JumpAtPlayer that makes the chicken jump towards the nearest player.
- Modify the condition inside the timer callback to trigger the jump when the distance is less than 300.
Here's how the code could look:
private void CreateChicken()
{
var chicken = Utilities.CreateEntityByName<CChicken>("chicken");
if (chicken is null) return;
var raw = _targetEntity?.EntityHandle.Raw;
if (raw is null) return;
chicken.Leader.Raw = raw.Value;
chicken.DispatchSpawn();
chicken.Leader.Raw = raw.Value;
var position = _positions[Random.Shared.Next(_positions.Length)];
chicken.Teleport(position);
var timer = _plugin.AddTimer(0.5f, () =>
{
if (!chicken.IsValid || _targetEntity?.IsValid == false) return;
var players = Utilities.GetPlayers().Where(u => u is { PawnIsAlive: true, IsValid: true });
var nearestPlayer = players
.Select(u => new { Player = u, Distance = Vector3.Distance(chicken.AbsOrigin!.ToVector3(), u.PlayerPawn.Value.AbsOrigin!.ToVector3()) })
.OrderBy(u => u.Distance)
.FirstOrDefault();
if (nearestPlayer != null)
{
if (nearestPlayer.Distance < 100)
{
Explode(chicken);
}
else if (nearestPlayer.Distance < 300)
{
JumpAtPlayer(chicken, nearestPlayer.Player);
}
}
}, TimerFlags.REPEAT | TimerFlags.STOP_ON_MAPCHANGE);
_gayTimer.Add(timer);
_currentChicken++;
_chickens.Add(chicken);
}
private void JumpAtPlayer(CChicken chicken, Player player)
{
// Logic to make the chicken jump towards the player
// This can involve modifying the chicken's velocity so that it moves towards the player's position in a jump arc
var direction = (player.PlayerPawn.Value.AbsOrigin!.ToVector3() - chicken.AbsOrigin!.ToVector3()).Normalized();
var jumpVelocity = new Vector3(direction.x, direction.y, 5.0f); // Adjust the z-component for jump height
chicken.SetVelocity(jumpVelocity);
}
In this revised code:
nearestPlayer identifies the closest player within the specified range.
- The chicken will explode if the nearest player is within 100 units.
- If the nearest player is between 100 and 300 units away, the
JumpAtPlayer method is called to make the chicken jump towards the player.
- The
JumpAtPlayer method calculates the direction vector towards the player and applies a velocity to the chicken that includes an upward component to simulate a jump. Adjust the z-component for the desired jump height.