Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
brasil:courses:blender_unity:unity:5_the_solar_system [2026/08/20 16:08] yannbrasil:courses:blender_unity:unity:5_the_solar_system [2026/08/27 15:16] (Version actuelle) – [Ship Controller] yann
Ligne 1: Ligne 1:
 +<note tip>
 +**Get an offline copy of this page by clicking the libreoffice writer icon** ~~ODT~~
 +</note>
 +
 +------
 +
 ====== Mini Project : navigating in the solar system ====== ====== Mini Project : navigating in the solar system ======
  
Ligne 7: Ligne 13:
   - On the status bar, select the Add (+) button.   - On the status bar, select the Add (+) button.
   - From the Add menu, select Add + package by name. The Name and Version fields appear.   - From the Add menu, select Add + package by name. The Name and Version fields appear.
-  - In the Name field, enter com.unity.cloud.gltfast.+  - In the Name field, enter **com.unity.cloud.gltfast**.
   - Select Add.   - Select Add.
   - The Editor installs the latest available version of the package and any dependent packages.   - The Editor installs the latest available version of the package and any dependent packages.
Ligne 15: Ligne 21:
 Useful files for the project : {{ :brasil:courses:blender_unity:unity:mini-project-files.zip |}} Useful files for the project : {{ :brasil:courses:blender_unity:unity:mini-project-files.zip |}}
 </note> </note>
 +
 +<note bloquante>
 +**Project backup: highlighted directories only**
 +
 +{{ :brasil:courses:blender_unity:unity:projectbackup.png |}}
 +
 +</note>
 +
 +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.
 +
 +{{ :brasil:courses:blender_unity:unity:solarsystem.jpg?1024 |}}
 +
 +Here are some guidelines:
 +
 +{{ :brasil:courses:blender_unity:unity:fidentite.png |}}
 +
 +The following diagram shows the actual relative sizes of the planets:
 +
 +{{:brasil:courses:blender_unity:unity:planetratio.jpg?1024|}}
 +
 +You will adjust the size and distance ratios relative to the Sun to make your application easy and enjoyable to use.
 +
 +{{:brasil:courses:blender_unity:unity:ship1.png|}}
 +
 +{{:brasil:courses:blender_unity:unity:ship2.png|}}
 +
 +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”.
 +
 +{{:brasil:courses:blender_unity:unity:saturnsrings.png|}}
 +
 +{{:brasil:courses:blender_unity:unity:saturnsrings2.png?1024|}}
 +
 +{{:brasil:courses:blender_unity:unity:saturnsrings3.png|}}
 +
 +====== 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).
 +<note tip>
 +**This code has been generated with Claude AI.** 
 +
 +//**Prompt :**// You are a Unity 3D developer. Write a script that allows a **spaceship to be controlled using the keyboard and mouse**, using the **New Input System package**. The various flight parameters/constants, such as **speed, acceleration, etc.**, must be adjustable through the **Unity Inspector** (i.e., using public variables or `[SerializeField]` fields in the script).
 +
 +</note>
 +<code csharp>
 +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);
 +    }
 +}
 +</code>
 +
 +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:solarsystemingame.png |}}
 +
 +{{ :brasil:courses:blender_unity:unity:solarsystemingame2.png |}}
 +
 +{{ :brasil:courses:blender_unity:unity:solarsystemingame.mp4 |}}
brasil/courses/blender_unity/unity/5_the_solar_system.1787234907.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