Scripting 101
Transform
Avertissement
Writing transform.position = P; is ok. But it is not allowed to set only one parameter: transform.position.x = 4
Coordinates
World
Vector3 P = transform.position; |
Get position |
float x = transform.position.x; |
Get X-coordinate |
transform.position = P; |
Set position |
transform.rotation = Quaternion.Euler(0f, 90f, 0f); |
Rotation relative to the world axis |
Local
Vector3 P = transform.localPosition; |
transform.localrotation = |
Moving
World
transform.Translate(dirVector3) |
Translate by a given vector |
transform.Translate(x, y, z) |
Translate by x, y, z values |
transform.Rotate(rotAngle3); |
Rotate using a Vector3 angle |
transform.Rotate(rx, ry, rz); |
Rotate using individual angles on each axis |
Local
transform.Translate(dirVector3, Space.Self) |
transform.Rotate(rotAngle3, Space.Self); |
Built-In Properties
There are predefined properties and methods that are automatically available in a MonoBehaviour script. Since they are not explicitly defined in the code, this can lead to some confusion.
gameObject |
GameObject |
The current GameObject this script is attached to |
transform |
Transform |
The transform of the current gameObject |
tag |
string |
The tag of the GameObject |
name |
string |
The name of the GameObject |
Instantiate(Class,pos,rot); |
given Class |
Create new gameObject in the scene |
Lifecycle Methods
MonoBehavior functions automatically called by Unity
Start() |
Used to initialize objects, variables, or references before the first frame update. |
Update() |
Every frame, handles non-physics updates: user input, animations and logic |
FixedUpdate() |
At fixed intervals (0.02s), used for physics-related logic and Rigidbody interactions |
void OnTriggerEnter(Collider other) |
Detect an object entering the collider set in trigger mode |
void OnTriggerExit(Collider other) |
|
void OnMouseOver() |
Mouse pointer passes over the object |
void OnMouseDown() |
Mouse click on the object |
Useful helper class
Some utility classes, imported by using UnityEngine, may be handy:
Debug.Log(..) |
Debug |
Display logs, warnings, and errors |
Mathf.Abs(..) |
Mathf |
Mathematical functions: sin, cos, clamp, lerp, etc. |
Random.Range(0, 3) |
Random (int) |
Integer random value among 0, 1, 2 |
Random.Range(0f, 3f) |
Random (float) |
Float random value like 0.4, 2.5… |
Time.time |
Time |
Time since the start of the scene |
Time.deltaTime |
Time passed since the last frame |
|
Input.GetKey(..) |
Input |
Reading keyboard |
Input.mousePosition |
Reading mouse |
|
Input.GetAxis(« Horizontal ») |
Reading joystick |
|
SceneManager.LoadScene(..) |
SceneManagement |
Load or add a scene |
PlayerPrefs.Set/Get |
PlayerPrefs |
Persistent data storage |
Physics.Raycast(ray, ..) |
Physics |
Raycasting technique |
Selection.activeGameObject |
Selection |
current selected gameObject in the scene view |
Hierarchy
GameObject
All parent/child relationship are stored in the transform parameter of a gameobject:
GameObject obj = GameObject.Find(“Name”); |
find an object by name |
GameObject parent = obj.transform.parent.gameObject; |
select its parent gameObject |
foreach (Transform T in obj.transform) { } |
parse its child gameObject |
newobj.transform.SetParent(parentTransform) |
set parent of a new object |
Component
g.GetComponent<Rigidbody>() |
Find a specific component of a GameObject |
Misc
Raycasting
RaycastHit hit; if (Physics.Raycast(position, vecDir, out hit, maxDist)) // throw a ray { GameObject touchedObject = hit.collider.gameObject; Debug.Log("Hit: " + touchedObject.name); }
Add a function in the Editor
using UnityEngine; using UnityEditor; public class NameOfMyClass { [MenuItem("Tools/Generate 32 Materials")] // new entry in the Tools menu public static void Go() {...} }
Coroutine
A coroutine if a function running asynchronously - use only for small process.
public class test : MonoBehaviour { void Start() { StartCoroutine(MyCoroutine()); // Start the coroutine when the scene starts } IEnumerator MyCoroutine() { Debug.Log("Start"); yield return new WaitForSeconds(3f); // Wait for 3 seconds Debug.Log("3 seconds later..."); }
Lerp
Lerp stands for: Linear interpolation - Useful for animation:
// rotate smoothly Quaternion targetRotation = Quaternion.Euler(0f, 180f, 0f); transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.deltaTime);