codeAPI Reference
s&box Projectile: Collision-based projectile
Projectile Component API Reference
Projectile is a Component that implements ICollisionListener for collision-based projectiles. It destroys itself on collision and can notify the instigator of hits.
Type Signature
CSHARP
public class Projectile : Component, Component.ICollisionListener
{
[RequireComponent] public Rigidbody Rigidbody { get; set; }
[Sync(SyncFlags.FromHost)] public Player Instigator { get; set; }
protected TimeSince TimeSinceCreated;
protected virtual void OnHit(Collision collision = default);
protected void NotifyHit(IDamageable target, Vector3 point);
public void UpdateDirection(Vector3 direction, float speed);
}Properties
- Rigidbody Rigidbody { get; set; } - Required rigidbody component
- Player Instigator { get; set; } - The player who fired this projectile (synced from host)
- TimeSince TimeSinceCreated - Time since the projectile was created
Methods
OnHit(Collision collision)
Called when the projectile hits something. Default implementation destroys the GameObject. Override for custom hit behavior.NotifyHit(IDamageable target, Vector3 point)
Notifies the instigator that their projectile hit a target. Currently a TODO for hitmarker implementation.UpdateDirection(Vector3 direction, float speed)
Updates the projectile's direction and velocity. Sets rotation to look at direction and sets rigidbody velocity.ICollisionListener Implementation
- OnCollisionStart(Collision collision) - Called on collision. Ignores collision with the instigator. Calls OnHit.
Usage
CSHARP
public class MyProjectile : Projectile
{
protected override void OnHit(Collision collision)
{
// Custom hit logic
var explosion = GameObject.Clone(explosionPrefab, WorldTransform);
explosion.NetworkSpawn();
base.OnHit(collision);
}
}Notes
- Tags itself with "projectile" on start
- Ignores collisions with the instigator player
- IsProxy check ensures only host handles collisions
- TimeSinceCreated reset on enable
- Useful for bullets, rockets, grenades, etc.
Was this helpful?