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:4_moving_objects [2026/08/25 19:28] – [The Earth rotates...] yann | brasil:courses:blender_unity:unity:4_moving_objects [2026/09/17 20:16] (Version actuelle) – [Exercise] yann | ||
|---|---|---|---|
| Ligne 79: | Ligne 79: | ||
| <code csharp> | <code csharp> | ||
| - | using Unity.AppUI.UI; | ||
| using UnityEngine; | using UnityEngine; | ||
| Ligne 136: | Ligne 135: | ||
| {{ : | {{ : | ||
| + | |||
| + | 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(" | ||
| + | if (UnityEngine.InputSystem.Keyboard.current.spaceKey.wasPressedThisFrame) | ||
| + | { | ||
| + | // log a message to the console | ||
| + | Debug.Log(" | ||
| + | |||
| + | // `this` represents the script instance | ||
| + | // `this.gameObject` provides access to the script' | ||
| + | // `activeSelf` indicates the GameObject' | ||
| + | // can't deactivate and reactivate the parent directly -> scripts are disabled when object is inactive | ||
| + | // gets all the direct children from the parent through " | ||
| + | foreach (Transform child in this.transform) | ||
| + | { | ||
| + | bool isActive = child.gameObject.activeSelf; | ||
| + | if (isActive) | ||
| + | { | ||
| + | // if the GameObject is active (visible) | ||
| + | Debug.Log(" | ||
| + | |||
| + | // set the GameObject to inactive (hide it) | ||
| + | child.gameObject.SetActive(false); | ||
| + | } | ||
| + | |||
| + | else | ||
| + | { | ||
| + | // if the GameObject is inactive (not visible) | ||
| + | Debug.Log(" | ||
| + | |||
| + | // set the GameObject to active (show it) | ||
| + | child.gameObject.SetActive(true); | ||
| + | } | ||
| + | | ||
| + | } | ||
| + | |||
| + | | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||
| ===== The Earth rotates... ===== | ===== The Earth rotates... ===== | ||
| Ligne 167: | Ligne 225: | ||
| The script exposes a public variable named “LocalRotationSpeed” that allows you to customize the rotation speed and axis. | The script exposes a public variable named “LocalRotationSpeed” that allows you to customize the rotation speed and axis. | ||
| Set the Y-axis to 50. | Set the Y-axis to 50. | ||
| - | {{ : | + | |
| {{ : | {{ : | ||
| Ligne 177: | 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. | ||
| + | {{ : | ||
| ===== Conclusion ===== | ===== Conclusion ===== | ||
| Ligne 185: | 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) |
| - 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 192: | Ligne 250: | ||
| {{: | {{: | ||
| - | {{ : | + | {{ : |
| + | |||
| + | <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, | ||
| + | transform.position = Target.transform.position + positionOffset; | ||
| + | angle += Time.deltaTime * RotationSpeed; | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | <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 | ||
| + | // | ||
| + | } | ||
| + | } | ||
| + | </ | ||
