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:4_moving_objects [2026/08/20 15:22] – [Creating Your First C# Script] yannbrasil:courses:blender_unity:unity:4_moving_objects [2026/08/25 19:29] (Version actuelle) – [Exercise] yann
Ligne 1: Ligne 1:
 +<note tip>
 +**Get an offline copy of this page by clicking the libreoffice writer icon** ~~ODT~~
 +</note>
 +
 +------
 +
 ====== Moving objects : Earth is turning ====== ====== Moving objects : Earth is turning ======
  
Ligne 6: Ligne 12:
 The GameObject will then not be displayed on the screen. The GameObject will then not be displayed on the screen.
  
-{{:brasil:courses:blender_unity:unity:unityshowhidegameobject.png|}}+{{ :brasil:courses:blender_unity:unity:unityearthhide.png |}}
  
 You can also do this using code. You can also do this using code.
Ligne 16: Ligne 22:
 In the root of the Assets folder, create a new subfolder and name it “Scripts.” In the root of the Assets folder, create a new subfolder and name it “Scripts.”
  
-{{:brasil:courses:blender_unity:unity:unitycreatefolderscripts-1024x576.jpg?1024|}}+{{ :brasil:courses:blender_unity:unity:unitycreatescriptfolder.png |}}
  
 Double-click the Scripts folder to open it, and create a new C# script that you'll name “EarthManager.cs”. Double-click the Scripts folder to open it, and create a new C# script that you'll name “EarthManager.cs”.
  
-{{:brasil:courses:blender_unity:unity:unitycreatescript-1024x576.jpg?1024|}}+{{ :brasil:courses:blender_unity:unity:unitycreateearthmanager.png |}}
  
 Unity does not have a built-in code editor, so you are free to choose your preferred editor (Visual Studio, Visual Studio Code, Notepad++, etc.). Unity does not have a built-in code editor, so you are free to choose your preferred editor (Visual Studio, Visual Studio Code, Notepad++, etc.).
Ligne 31: Ligne 37:
 You can change the default code editor by going to the Edit / Preferences / External Tools menu. You can change the default code editor by going to the Edit / Preferences / External Tools menu.
  
-{{:brasil:courses:blender_unity:unity:unityeditpreferencesexternaltools.jpg|}}+{{ :brasil:courses:blender_unity:unity:unityselecteditor.png |}}
  
 ===== Structure of a C# script ===== ===== Structure of a C# script =====
Ligne 37: Ligne 43:
 Double-click the script, and it will open in the default editor set in Unity (Visual Studio Code in my case). Double-click the script, and it will open in the default editor set in Unity (Visual Studio Code in my case).
  
-{{:brasil:courses:blender_unity:unity:unityeditpreferencesexternaltools.jpg|}}+{{ :brasil:courses:blender_unity:unity:unityvisualstudio.png?1024 |}}
  
 By default, all Unity C# scripts inherit from the MonoBehavior class. By default, all Unity C# scripts inherit from the MonoBehavior class.
Ligne 43: Ligne 49:
  
 <code csharp> <code csharp>
-using UnityEngine; +public class earthManager : MonoBehaviour
-public class EarthManager : MonoBehaviour+
 { {
-    // Start is called before the first frame update +    // Start is called once before the first execution of Update after the MonoBehaviour is created 
-    private void Start()+    void Start()
     {     {
-         +        
     }     }
-     // Update is called once per frame + 
-    private void Update()+    // Update is called once per frame 
 +    void Update()
     {     {
-         +        
     }     }
 } }
Ligne 68: Ligne 74:
 Scripts can be attached to a GameObject. This allows you to add behaviors to a GameObject. To add the first behavior to our Earth, we’ll drag and drop our “EarthManager” script onto the “Earth” GameObject. Scripts can be attached to a GameObject. This allows you to add behaviors to a GameObject. To add the first behavior to our Earth, we’ll drag and drop our “EarthManager” script onto the “Earth” GameObject.
  
-{{:brasil:courses:blender_unity:unity:unitydraganddropscript-1024x576.jpg?1024|}}+{{ :brasil:courses:blender_unity:unity:unityaddscriptannoted.png?1024 |}}
  
 Now that the script is attached to the GameObject, we're going to code the desired behavior. Now that the script is attached to the GameObject, we're going to code the desired behavior.
  
 <code csharp> <code csharp>
 +using Unity.AppUI.UI;
 using UnityEngine; using UnityEngine;
 + 
 public class EarthManager : MonoBehaviour public class EarthManager : MonoBehaviour
 { {
-    // Start is called only once+    // Start is called once before the first execution of Update after the MonoBehaviour is created
     private void Start()     private void Start()
     {     {
Ligne 88: Ligne 95:
     {     {
         // if the spacebar is pressed         // if the spacebar is pressed
-        if (Input.GetKeyDown(space))+        //if (Input.GetKeyDown("space")) old input system 
 +        if(UnityEngine.InputSystem.Keyboard.current.spaceKey.wasPressedThisFrame)
         {         {
             // log a message to the console             // log a message to the console
-            Debug.Log(The spacebar was pressed);+            Debug.Log("The spacebar was pressed");
  
             // `this` represents the script instance             // `this` represents the script instance
Ligne 100: Ligne 108:
             {             {
                 // if the GameObject is active (visible)                 // if the GameObject is active (visible)
-                Debug.Log(Hiding the earth’ GameObject);+                Debug.Log("Hiding the 'earthGameObject");
  
                 // set the GameObject to inactive (hide it)                 // set the GameObject to inactive (hide it)
Ligne 108: Ligne 116:
     }     }
 } }
- 
 </code> </code>
  
 To test our code, we're going to run our game in the editor: To test our code, we're going to run our game in the editor:
  
-{{:brasil:courses:blender_unity:unity:unityplayeditor-1024x163.png?1024|}}+{{ :brasil:courses:blender_unity:unity:unitygamemode.png |}}
  
 If you press the spacebar, you'll notice that the Earth disappears (it is no longer displayed by the render engine because its GameObject's visibility is turned off). If you press the spacebar, you'll notice that the Earth disappears (it is no longer displayed by the render engine because its GameObject's visibility is turned off).
Ligne 119: Ligne 126:
 Stay in Play mode (with the Game tab active), select the Earth GameObject in the hierarchy on the left if you haven’t already, and check the status of the GameObject’s visibility checkbox in the Inspector on the right. Stay in Play mode (with the Game tab active), select the Earth GameObject in the hierarchy on the left if you haven’t already, and check the status of the GameObject’s visibility checkbox in the Inspector on the right.
  
-{{:brasil:courses:blender_unity:unity:unityscriptshowhidecheckbox.png|}}+{{ :brasil:courses:blender_unity:unity:unityhideaction.png |}} 
 +{{ :brasil:courses:blender_unity:unity:unityhideaction2.png |}}
  
 The interface reflects the changes made by the running code when you are in Play mode. This provides a basic level of visual debugging within the editor. The interface reflects the changes made by the running code when you are in Play mode. This provides a basic level of visual debugging within the editor.
Ligne 127: Ligne 135:
 Debug.Log statements write a log to the editor console. These logs are a starting point for debugging; they let you know which lines of code have been executed and even display the values of variables. Debug.Log statements write a log to the editor console. These logs are a starting point for debugging; they let you know which lines of code have been executed and even display the values of variables.
  
-{{:brasil:courses:blender_unity:unity:unityconsole.png|}}+{{ :brasil:courses:blender_unity:unity:unityconsoledebug.png |}}
  
 ===== The Earth rotates... ===== ===== The Earth rotates... =====
Ligne 138: Ligne 146:
 using UnityEngine; using UnityEngine;
  
-public class EarthRotation : MonoBehaviour+public class earthRotate : MonoBehaviour
 { {
     public Vector3 localRotationSpeed;     public Vector3 localRotationSpeed;
  
-    // Start is called before the first frame update +    // Start is called once before the first execution of Update after the MonoBehaviour is created 
-    private void Start()+    void Start()
     {     {
 +        
     }     }
  
     // Update is called once per frame     // Update is called once per frame
-    private void Update()+    void Update()
     {     {
         this.transform.Rotate(localRotationSpeed * Time.deltaTime, Space.Self);         this.transform.Rotate(localRotationSpeed * Time.deltaTime, Space.Self);
Ligne 160: Ligne 168:
 Set the Y-axis to 50. Set the Y-axis to 50.
  
-{{:brasil:courses:blender_unity:unity:unityearthrotationscript.png|}}+{{ :brasil:courses:blender_unity:unity:unityearthrotatescriptannoted.png?1024 |}} 
 + 
 +This variable is of type Vector3, meaning it’s a 3-dimensional vector that can store a value for the X axis, a value for the Y axis, and a value for the Z axis. 
 +Vectors are very useful; we’ll come back to them later. 
 + 
 +Run your game (using the Play button); the Earth rotates on its axis at a speed of 50 along the Y-axis. If you press the spacebar, it continues to disappear. 
 + 
 +In the editor, you can see in real time that your script is changing the GameObject’s rotation along the Y-axis. 
 + 
 +{{ :brasil:courses:blender_unity:unity:unityearthrotationvalue.png |}} 
 + 
 +===== Conclusion ===== 
 + 
 +Using two small C# scripts, we were able to add two different behaviors to our “Earth” GameObject. These scripts can be reused indefinitely; you can apply them to other GameObjects (this will create multiple instances of the same script). 
 + 
 +===== Exercise ===== 
 + 
 +  - Add a Milky Way skybox; to do this, use the Asset Store (Milky Way Skybox) or the file {{ :brasil:courses:blender_unity:unity:milkywayskybox.unitypackage |}} 
 +  - Make the moon rotate on its axis 
 +  - Make the moon orbit the Earth 
 +  - Make the camera rotate around the Earth in the opposite direction; the camera must always be oriented toward the center of the Earth
  
 +{{:brasil:courses:blender_unity:unity:earthandmoon.png?1024|}}
  
 +{{ :brasil:courses:blender_unity:unity:earthturning.mp4 |}}
brasil/courses/blender_unity/unity/4_moving_objects.1787232178.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