Ceci est une ancienne révision du document !


Get an offline copy of this page by clicking the libreoffice writer icon Exporter la page au format Open Document

Mini Project : navigating in the solar system

Install the Unity glTFast package using the Unity Package Manager

To install the Unity glTFast package, follow these steps:
  1. In your Unity project, go to Windows > Package Manager.
  2. On the status bar, select the Add (+) button.
  3. From the Add menu, select Add + package by name. The Name and Version fields appear.
  4. In the Name field, enter com.unity.cloud.gltfast.
  5. Select Add.
  6. The Editor installs the latest available version of the package and any dependent packages.
Useful files for the project : mini-project-files.zip
Project backup: highlighted directories only

Using Blender and Unity 3D, and building on the previous exercises, you will create a small solar system exploration application.

The Sun will be considered stationary, and the planets’ orbits will be assumed to be circular for simplification.

The planets’ orbits can be shown or hidden, and the planets will move at speeds proportional to their respective orbital periods.

Here are some guidelines:

The following diagram shows the actual relative sizes of the planets:

You will adjust the size and distance ratios relative to the Sun to make your application easy and enjoyable to use.

Students who finish early can create a journey around the planets using a spacecraft of their own design… or not! 🚀

Saturn’s Rings

Start with a circle and extrude it inward or outward. Then unwrap the mesh using the “Follow Active Quads” option. The whole process is summarized in the following screenshot:

Don’t forget to select a face before using “Follow Active Face”.

Ship Controller

Here's the spaceship control script in C#, self-contained (it creates its own InputActions in code, so no separate .inputactions asset is needed — but the Input System package still needs to be installed, and Active Input Handling must be set to “Input System Package (New)” or “Both” in Project Settings > Player).

This code has been generated with Claude AI.
using UnityEngine;
using UnityEngine.InputSystem;
 
[RequireComponent(typeof(Rigidbody))]
public class SpaceshipController : MonoBehaviour
{
    [Header("Translation (forward / strafe / up-down)")]
    [Tooltip("Maximum movement speed (units/second)")]
    public float maxTranslationSpeed = 50f;
    [Tooltip("Acceleration toward target speed (units/second²)")]
    public float translationAcceleration = 25f;
    [Tooltip("Deceleration when no movement key is pressed")]
    public float translationBraking = 15f;
 
    [Header("Rotation (pitch / yaw via mouse)")]
    [Tooltip("Mouse sensitivity for pitch/yaw")]
    public float mouseSensitivity = 0.2f;
    [Tooltip("Invert the mouse Y axis")]
    public bool invertYAxis = false;
 
    [Header("Roll (Q/E keys)")]
    [Tooltip("Maximum roll angular speed (degrees/second)")]
    public float maxRollSpeed = 90f;
    [Tooltip("Roll angular acceleration (degrees/second²)")]
    public float rollAcceleration = 180f;
 
    private Rigidbody rb;
 
    // New Input System actions, created directly in code
    private InputAction moveAction;
    private InputAction verticalAction;
    private InputAction rollAction;
    private InputAction lookAction;
 
    private Vector2 moveInput;
    private float verticalInput;
    private float rollInput;
    private Vector2 lookAccumulator; // accumulator so mouse deltas aren't lost between FixedUpdate calls
 
    private float currentRollSpeed;
 
    void Awake()
    {
        rb = GetComponent<Rigidbody>();
        rb.useGravity = false;
        rb.linearDamping = 0f;      // braking is handled manually
        rb.angularDamping = 0f;
 
        // Forward/backward + left/right movement (WASD)
        moveAction = new InputAction("Move", InputActionType.Value, expectedControlType: "Vector2");
        moveAction.AddCompositeBinding("2DVector")
            .With("Up", "<Keyboard>/w")
            .With("Down", "<Keyboard>/s")
            .With("Left", "<Keyboard>/a")
            .With("Right", "<Keyboard>/d");
 
        // Up/down movement (Space / Left Ctrl)
        verticalAction = new InputAction("Vertical", InputActionType.Value, expectedControlType: "Axis");
        verticalAction.AddCompositeBinding("1DAxis")
            .With("Positive", "<Keyboard>/space")
            .With("Negative", "<Keyboard>/leftCtrl");
 
        // Roll (Q/E)
        rollAction = new InputAction("Roll", InputActionType.Value, expectedControlType: "Axis");
        rollAction.AddCompositeBinding("1DAxis")
            .With("Positive", "<Keyboard>/e")
            .With("Negative", "<Keyboard>/q");
 
        // Mouse (delta)
        lookAction = new InputAction("Look", InputActionType.Value, binding: "<Mouse>/delta");
    }
 
    void OnEnable()
    {
        moveAction.Enable();
        verticalAction.Enable();
        rollAction.Enable();
        lookAction.Enable();
    }
 
    void OnDisable()
    {
        moveAction.Disable();
        verticalAction.Disable();
        rollAction.Disable();
        lookAction.Disable();
    }
 
    void Update()
    {
        moveInput = moveAction.ReadValue<Vector2>();
        verticalInput = verticalAction.ReadValue<float>();
        rollInput = rollAction.ReadValue<float>();
 
        // Accumulate the mouse delta here and consume it in FixedUpdate,
        // so no movement is lost if Update runs faster than FixedUpdate.
        lookAccumulator += lookAction.ReadValue<Vector2>();
    }
 
    void FixedUpdate()
    {
        Vector2 lookDelta = lookAccumulator;
        lookAccumulator = Vector2.zero;
 
        HandleTranslation();
        HandleRotation(lookDelta);
    }
 
    private void HandleTranslation()
    {
        Vector3 direction = new Vector3(moveInput.x, verticalInput, moveInput.y);
        if (direction.sqrMagnitude > 1f)
            direction.Normalize();
 
        Vector3 targetWorldVelocity = transform.TransformDirection(direction) * maxTranslationSpeed;
 
        float rateOfChange = direction.sqrMagnitude > 0.0001f ? translationAcceleration : translationBraking;
        rb.linearVelocity = Vector3.MoveTowards(rb.linearVelocity, targetWorldVelocity, rateOfChange * Time.fixedDeltaTime);
    }
 
    private void HandleRotation(Vector2 lookDelta)
    {
        // Pitch/yaw: direct rotation proportional to mouse movement
        float yaw = lookDelta.x * mouseSensitivity;
        float pitch = -lookDelta.y * mouseSensitivity * (invertYAxis ? -1f : 1f);
 
        // Roll: accelerated model, similar to translation
        float targetRollSpeed = -rollInput * maxRollSpeed;
        currentRollSpeed = Mathf.MoveTowards(currentRollSpeed, targetRollSpeed, rollAcceleration * Time.fixedDeltaTime);
        float roll = currentRollSpeed * Time.fixedDeltaTime;
 
        Quaternion deltaRotation = Quaternion.Euler(pitch, yaw, roll);
        rb.MoveRotation(rb.rotation * deltaRotation);
    }
}

What it does

  • W/S: move forward/backward along the ship's axis.
  • A/D: strafe left/right.
  • Space / Left Ctrl: move up/down.
  • Mouse: pitch and yaw.
  • Q/E: roll, with its own acceleration.
  • All speed/acceleration settings are public fields visible in the Inspector.

Things worth double-checking

  • I'm not fully certain which exact Input System package version is in your project — the composite names (“2DVector”, “1DAxis”) and binding paths (<Keyboard>/space, <Mouse>/delta) are the ones documented in current stable versions, but if you're on a much older or very recent version, check the official Unity documentation to confirm these paths and composite names haven't changed.
  • The script assumes rb.linearVelocity / rb.linearDamping (names used since Unity 6) exist in your Unity version. If you're on a pre-Unity-6 version, replace them with rb.velocity and rb.drag / rb.angularDrag.
  • Gravity is already disabled in the script (rb.useGravity = false), but check that no other component re-enables it.

Results

brasil/courses/blender_unity/unity/5_the_solar_system.1787835883.txt.gz · Dernière modification : de yann
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0