menu_bookDocumentation
s&box Vector3.WithAimCone: Direction spread
Vector3.WithAimCone Extension Method
The WithAimCone extension method adds random spread to a direction vector, useful for bullet spread, projectile dispersion, and other aim-based randomization.
Extension Methods
CSHARP
public static class Extensions
{
public static Vector3 WithAimCone(this Vector3 direction, float degrees)
{
var angle = Rotation.LookAt(direction);
angle *= new Angles(Game.Random.Float(-degrees / 2.0f, degrees / 2.0f), Game.Random.Float(-degrees / 2.0f, degrees / 2.0f), 0);
return angle.Forward;
}
public static Vector3 WithAimCone(this Vector3 direction, float horizontalDegrees, float verticalDegrees)
{
var angle = Rotation.LookAt(direction);
angle *= new Angles(Game.Random.Float(-verticalDegrees / 2.0f, verticalDegrees / 2.0f), Game.Random.Float(-horizontalDegrees / 2.0f, horizontalDegrees / 2.0f), 0);
return angle.Forward;
}
}Parameters
WithAimCone(Vector3 direction, float degrees)
- Vector3 direction - The base direction to add spread to
- float degrees - The total cone angle (spread is applied to both horizontal and vertical axes)
WithAimCone(Vector3 direction, float horizontalDegrees, float verticalDegrees)
- Vector3 direction - The base direction to add spread to
- float horizontalDegrees - The horizontal cone angle (yaw spread)
- float verticalDegrees - The vertical cone angle (pitch spread)
Usage
CSHARP
// Add 5 degrees of spread in all directions
var spreadDirection = aimDirection.WithAimCone(5.0f);
// Add different horizontal and vertical spread
var spreadDirection = aimDirection.WithAimCone(horizontalDegrees: 10.0f, verticalDegrees: 2.0f);
// Use for bullet spread
var bulletDirection = player.EyeRotation.Forward.WithAimCone(weapon.Spread);Notes
- Uses Game.Random.Float for random spread generation
- Spread is symmetric around the base direction
- Returns a normalized direction vector
- Commonly used in weapon systems for bullet spread
- The two-parameter version allows asymmetric spread (wider horizontal than vertical, or vice versa)
Was this helpful?