Untitled
unknown
plain_text
a year ago
1.7 kB
2
Indexable
using UnityEngine; public class Spaceship0 : MonoBehaviour { public float thrustForce = 10f; // Force applied for forward/backward movement public float rotationSpeed = 100f; // Speed of rotation public float maxSpeed = 20f; // Maximum allowed speed public float rotationalDamping = 0.1f; // Damping factor for rotation public float dragWhenIdle = 5f; // Drag applied when not pressing W private Rigidbody rb; void Start() { // Get the Rigidbody component attached to the spaceship rb = GetComponent<Rigidbody>(); } void Update() { // Get input for forward/backward movement float moveDirection = Input.GetAxis("Vertical"); // W = 1, S = -1 float rotationDirection = Input.GetAxis("Horizontal"); // A = -1, D = 1 // Calculate forward and backward thrust Vector3 thrust = transform.forward * moveDirection * thrustForce; // Apply thrust to the spaceship rb.AddForce(thrust); // Clamp the spaceship's velocity to the maximum speed rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxSpeed); // Calculate rotation around the Y-axis float rotation = rotationDirection * rotationSpeed * Time.deltaTime; // Apply rotation with reduced inertia rb.angularVelocity = Vector3.Lerp(rb.angularVelocity, Vector3.up * rotation, rotationalDamping); // Apply drag when not pressing W if (moveDirection <= 0) { rb.drag = dragWhenIdle; } else { rb.drag = 0; // Reset drag to 0 for normal thrust } } }
Editor is loading...
Leave a Comment