Spielmechanik-Ingenieur: Space Drifting Controller
Von Wikiprompt, der freien Prompt-Enzyklopädie
Spielmechanik-Ingenieur: Space Drifting Controller Ein Systemprompt zur Generierung von physikbasiertem Game-Mechanik-Code, mit Fokus auf Newton'sche Bewegung und Projektil-Interception mit C#-Skripten.
Prompt-InhaltSpeichern
🌐
public class SpaceDriftController : MonoBehaviour
{
[Header("Ship Physics")]
public float thrustForce = 20f;
public float maxSpeed = 50f;
public float dragCoefficient = 0.1f; // Linear drag for space vacuum approximation
public float mass = 1f;
private Vector3 shipVelocity = Vector3.zero;
void FixedUpdate()
{
// Newton's 2nd law: F = m*a => a = F/m
Vector3 inputForce = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0) * thrustForce;
Vector3 acceleration = inputForce / mass;
// Integrate velocity (semi-implicit Euler for stability)
shipVelocity += acceleration * Time.fixedDeltaTime;
// Apply linear drag (F_drag = -b * v)
shipVelocity *= (1f - dragCoefficient * Time.fixedDeltaTime);
// Clamp to max speed (magnitude preserving direction)
if (shipVelocity.magnitude > maxSpeed)
shipVelocity = shipVelocity.normalized * maxSpeed;
// Update position
transform.position += shipVelocity * Time.fixedDeltaTime;
}
// Expose velocity for weapon system (vector addition)
public Vector3 GetShipVelocity() => shipVelocity;
}
public class ProjectileLauncher : MonoBehaviour
{
[Header("Projectile Settings")]
public GameObject projectilePrefab;
public float projectileSpeed = 100f; // Base muzzle velocity (relative to ship)
public float fireCooldown = 0.1f;
public LayerMask collisionMask;
private float lastFireTime;
private SpaceDriftController shipController;
void Start()
{
shipController = GetComponentInParent<SpaceDriftController>();
}
void Update()
{
if (Input.GetButton("Fire1") && Time.time >= lastFireTime + fireCooldown)
{
FireProjectile();
lastFireTime = Time.time;
}
}
void FireProjectile()
{
// Vector velocity addition: projectile_velocity = ship_velocity + muzzle_velocity
Vector3 shipVel = shipController.GetShipVelocity();
Vector3 muzzleVel = transform.up * projectileSpeed; // Local forward (up in 2D top-down)
Vector3 finalVel = shipVel + muzzleVel;
// Spawn projectile
GameObject proj = Instantiate(projectilePrefab, transform.position, transform.rotation);
Projectile projScript = proj.GetComponent<Projectile>();
projScript.Initialize(finalVel, collisionMask);
}
}
public class Projectile : MonoBehaviour
{
private Vector3 velocity;
private LayerMask collisionMask;
private float lifeTime = 5f;
public void Initialize(Vector3 initialVelocity, LayerMask mask)
{
velocity = initialVelocity;
collisionMask = mask;
Destroy(gameObject, lifeTime);
}
void FixedUpdate()
{
// High-frequency collision polling: use continuous collision detection (CCD) or manual raycast
Vector3 deltaMove = velocity * Time.fixedDeltaTime;
RaycastHit2D hit = Physics2D.Raycast(transform.position, deltaMove.normalized, deltaMove.magnitude, collisionMask);
if (hit.collider != null)
{
// Impact - destroy projectile (or apply damage)
Destroy(gameObject);
return;
}
// Move projectile (no external forces - Newton's 1st law)
transform.position += deltaMove;
}
}
// Mathematical derivation for projectile interception (target prediction)
public static class InterceptionMath
{
public static Vector3 CalculateInterceptPoint(Vector3 shooterPos, Vector3 shooterVel, float projectileSpeed,
Vector3 targetPos, Vector3 targetVel)
{
// Solve for time t where |shooterPos + shooterVel*t + projectileDir*projectileSpeed*t - (targetPos + targetVel*t)| = 0
// Let d = targetPos - shooterPos, v = targetVel - shooterVel
Vector3 d = targetPos - shooterPos;
Vector3 v = targetVel - shooterVel;
// Quadratic: |v|^2 * t^2 + 2*(d·v)*t + (|d|^2 - projectileSpeed^2 * t^2) = 0
// Simplify: (|v|^2 - projectileSpeed^2) * t^2 + 2*(d·v)*t + |d|^2 = 0
float a = Vector3.Dot(v, v) - projectileSpeed * projectileSpeed;
float b = 2f * Vector3.Dot(d, v);
float c = Vector3.Dot(d, d);
float discriminant = b * b - 4f * a * c;
if (discriminant < 0f)
return Vector3.zero; // No solution - target cannot be hit
float sqrtDisc = Mathf.Sqrt(discriminant);
float t1 = (-b + sqrtDisc) / (2f * a);
float t2 = (-b - sqrtDisc) / (2f * a);
// Choose smallest positive time
float t = Mathf.Min(t1, t2);
if (t < 0f)
t = Mathf.Max(t1, t2);
if (t < 0f)
return Vector3.zero; // Only negative times - target moving away
// Intercept point = target position at time t
return targetPos + targetVel * t;
}
}
Melde dich an, um den vollständigen Prompt zu sehen
Weiter mit:
Mit der Anmeldung akzeptierst du unsere Nutzungsbedingungen und Datenschutz
Verwendung
Dieser Prompt ist für die Verwendung mit coding gedacht. Kopiere den Inhalt oben und füge ihn in dein bevorzugtes KI-Tool ein.
Für beste Ergebnisse passe die Platzhalter (eckige Klammern oder Großbuchstaben) an deine Anforderungen an.
Diskussion
0 Kommentare