Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| brasil:courses:blender_unity:unity:5_the_solar_system [2026/08/20 16:16] – yann | brasil: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~~ | ||
| + | </ | ||
| + | |||
| + | ------ | ||
| + | |||
| ====== 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 19: | Ligne 25: | ||
| **Project backup: highlighted directories only** | **Project backup: highlighted directories only** | ||
| - | {{: | + | {{ : |
| </ | </ | ||
| Ligne 29: | Ligne 35: | ||
| The planets’ orbits can be shown or hidden, and the planets will move at speeds proportional to their respective orbital periods. | 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: | Here are some guidelines: | ||
| + | |||
| + | {{ : | ||
| The following diagram shows the actual relative sizes of the planets: | 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. | 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! 🚀 | 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 " | ||
| + | <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/ | ||
| + | |||
| + | </ | ||
| + | <code csharp> | ||
| + | using UnityEngine; | ||
| + | using UnityEngine.InputSystem; | ||
| + | |||
| + | [RequireComponent(typeof(Rigidbody))] | ||
| + | public class SpaceshipController : MonoBehaviour | ||
| + | { | ||
| + | [Header(" | ||
| + | [Tooltip(" | ||
| + | public float maxTranslationSpeed = 50f; | ||
| + | [Tooltip(" | ||
| + | public float translationAcceleration = 25f; | ||
| + | [Tooltip(" | ||
| + | public float translationBraking = 15f; | ||
| + | |||
| + | [Header(" | ||
| + | [Tooltip(" | ||
| + | public float mouseSensitivity = 0.2f; | ||
| + | [Tooltip(" | ||
| + | public bool invertYAxis = false; | ||
| + | |||
| + | [Header(" | ||
| + | [Tooltip(" | ||
| + | public float maxRollSpeed = 90f; | ||
| + | [Tooltip(" | ||
| + | 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; | ||
| + | |||
| + | private float currentRollSpeed; | ||
| + | |||
| + | void Awake() | ||
| + | { | ||
| + | rb = GetComponent< | ||
| + | rb.useGravity = false; | ||
| + | rb.linearDamping = 0f; // braking is handled manually | ||
| + | rb.angularDamping = 0f; | ||
| + | |||
| + | // Forward/ | ||
| + | moveAction = new InputAction(" | ||
| + | moveAction.AddCompositeBinding(" | ||
| + | .With(" | ||
| + | .With(" | ||
| + | .With(" | ||
| + | .With(" | ||
| + | |||
| + | // Up/down movement (Space / Left Ctrl) | ||
| + | verticalAction = new InputAction(" | ||
| + | verticalAction.AddCompositeBinding(" | ||
| + | .With(" | ||
| + | .With(" | ||
| + | |||
| + | // Roll (Q/E) | ||
| + | rollAction = new InputAction(" | ||
| + | rollAction.AddCompositeBinding(" | ||
| + | .With(" | ||
| + | .With(" | ||
| + | |||
| + | // Mouse (delta) | ||
| + | lookAction = new InputAction(" | ||
| + | } | ||
| + | |||
| + | 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< | ||
| + | verticalInput = verticalAction.ReadValue< | ||
| + | rollInput = rollAction.ReadValue< | ||
| + | |||
| + | // Accumulate the mouse delta here and consume it in FixedUpdate, | ||
| + | // so no movement is lost if Update runs faster than FixedUpdate. | ||
| + | lookAccumulator += lookAction.ReadValue< | ||
| + | } | ||
| + | |||
| + | void FixedUpdate() | ||
| + | { | ||
| + | Vector2 lookDelta = lookAccumulator; | ||
| + | lookAccumulator = Vector2.zero; | ||
| + | |||
| + | HandleTranslation(); | ||
| + | HandleRotation(lookDelta); | ||
| + | } | ||
| + | |||
| + | private void HandleTranslation() | ||
| + | { | ||
| + | Vector3 direction = new Vector3(moveInput.x, | ||
| + | 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, | ||
| + | } | ||
| + | |||
| + | 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, | ||
| + | float roll = currentRollSpeed * Time.fixedDeltaTime; | ||
| + | |||
| + | Quaternion deltaRotation = Quaternion.Euler(pitch, | ||
| + | rb.MoveRotation(rb.rotation * deltaRotation); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | What it does | ||
| + | * **W/S**: move forward/ | ||
| + | * **A/D**: strafe left/right. | ||
| + | * **Space / Left Ctrl**: move up/down. | ||
| + | * **Mouse**: pitch and yaw. | ||
| + | * **Q/E**: roll, with its own acceleration. | ||
| + | * **All speed/ | ||
| + | |||
| + | |||
| + | Things worth double-checking | ||
| + | * I'm not fully certain which exact Input System package version is in your project — the composite names (" | ||
| + | * 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 ====== | ||
| + | |||
| + | {{ : | ||
| + | |||
| + | {{ : | ||
| + | |||
| + | {{ : | ||
