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/25 18:53] – [Attaching a C# Script to a GameObject] yannbrasil:courses:blender_unity:unity:4_moving_objects [2026/09/16 21:26] (Version actuelle) – [Exercise] yann
Ligne 80: Ligne 80:
 <code csharp> <code csharp>
 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 94: Ligne 94:
     {     {
         // 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 106: Ligne 107:
             {             {
                 // 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 114: Ligne 115:
     }     }
 } }
- 
 </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 125: Ligne 125:
 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 133: Ligne 134:
 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 |}} 
 + 
 +Hide and display again : we need to hide the children of the paretn GameObkject which have the script : script is disabled when parent GameObject is disabled. 
 + 
 +<code csharp> 
 +//using Unity.App.UI; 
 +using UnityEngine; 
 + 
 +public class earthManager : MonoBehaviour 
 +
 +    // Start is called once before the first execution of Update after the MonoBehaviour is created 
 +    void Start() 
 +    { 
 +         
 +    } 
 + 
 +    // Update is called once per frame 
 +    void Update() 
 +    { 
 +        // if the spacebar is pressed 
 +        //if (Input.GetKeyDown("space")) old input system 
 +        if (UnityEngine.InputSystem.Keyboard.current.spaceKey.wasPressedThisFrame) 
 +        { 
 +            // log a message to the console 
 +            Debug.Log("The spacebar was pressed"); 
 + 
 +            // `this` represents the script instance 
 +            // `this.gameObject` provides access to the script's GameObject 
 +            // `activeSelf` indicates the GameObject's visibility 
 +            // can't deactivate and reactivate the parent directly -> scripts are disabled when object is inactive 
 +            // gets all the direct children from the parent through "Transform" property, because every object has a transform property 
 +            foreach (Transform child in this.transform)  
 +                { 
 +                bool isActive = child.gameObject.activeSelf; 
 +                if (isActive) 
 +                { 
 +                    // if the GameObject is active (visible) 
 +                    Debug.Log("Hiding the 'earth' GameObject"); 
 + 
 +                    // set the GameObject to inactive (hide it) 
 +                    child.gameObject.SetActive(false); 
 +                } 
 +                  
 +                else 
 +                { 
 +                    // if the GameObject is inactive (not visible) 
 +                    Debug.Log("Showing the 'earth' GameObject"); 
 + 
 +                    // set the GameObject to active (show it) 
 +                    child.gameObject.SetActive(true); 
 +                } 
 +                 
 +            } 
 + 
 +             
 +        } 
 +    } 
 +
 + 
 +</code>
  
 ===== The Earth rotates... ===== ===== The Earth rotates... =====
Ligne 144: Ligne 204:
 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 166: Ligne 226:
 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. 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.
Ligne 175: Ligne 235:
 In the editor, you can see in real time that your script is changing the GameObject’s rotation along the Y-axis. 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:unitytransformrotationupdated.png|}}+{{ :brasil:courses:blender_unity:unity:unityearthrotationvalue.png |}}
  
 ===== Conclusion ===== ===== Conclusion =====
Ligne 183: Ligne 243:
 ===== Exercise ===== ===== Exercise =====
  
-  - Add a Milky Way skybox; to do this, use the Asset Store (Milky Way Skybox)+  - 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 rotate on its axis
   - Make the moon orbit the Earth   - Make the moon orbit the Earth
Ligne 191: Ligne 251:
  
 {{ :brasil:courses:blender_unity:unity:earthturning.mp4 |}} {{ :brasil:courses:blender_unity:unity:earthturning.mp4 |}}
 +
 +<code csharp | turnAround.cs>
 +
 +using UnityEngine;
 +
 +public class TurnAround : MonoBehaviour
 +{
 +    // Drag & drop the player in the inspector
 +    // public Transform Target;
 +    public GameObject Target;
 +    public float RotationSpeed = 1;
 +    public float CircleRadius = 1;
 +    public float ElevationOffset = 0;
 +
 +    private Vector3 positionOffset;
 +    private float angle;
 +    //private Transform TargetT;
 +
 +    // Start is called before the first frame update
 +    void Start()
 +    {
 +        //TargetT = Target.transform;
 +    }
 +
 +
 +    void Update()
 +    {
 +        positionOffset.Set(Mathf.Cos(angle) * CircleRadius, ElevationOffset, Mathf.Sin(angle) * CircleRadius);
 +        transform.position = Target.transform.position + positionOffset;
 +        angle += Time.deltaTime * RotationSpeed;
 +    }
 +
 + }
 +</code>
 +
 +<code csharp | lookAt.cs>
 +using UnityEngine;
 +
 +public class LookAt : MonoBehaviour
 +{
 +    public Transform target;
 +    // Start is called before the first frame update
 +    void Start()
 +    {
 +
 +    }
 +
 +    // Update is called once per frame
 +    void Update()
 +    {
 +        // Rotate the camera every frame so it keeps looking at the target
 +        transform.LookAt(target);
 +
 +        // Same as above, but setting the worldUp parameter to Vector3.left in this example turns the camera on its side
 +        //transform.LookAt(target, Vector3.left);
 +    }
 +}
 +</code>
brasil/courses/blender_unity/unity/4_moving_objects.1787676821.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