Unity dùng C# script gắn lên GameObject.
Các method như Start, Update, FixedUpdate được Unity gọi theo vòng đời game.
public class Player : MonoBehaviour
[SerializeField] private float speed = 5f;
float x = Input.GetAxis("Horizontal");
transform.Translate(Vector3.right * x * speed * Time.deltaTime);
| API | Ghi nhớ |
|---|
MonoBehaviour | Base class cho Unity script |
Start | Chạy một lần khi object bắt đầu |
Update | Chạy mỗi frame |
FixedUpdate | Chạy theo physics tick |
[SerializeField] | Hiện private field trong Inspector |
GameObject | Object trong scene |
Transform | Vị trí, xoay, scale |
Rigidbody | Physics body |
Collider | Vùng va chạm |
Instantiate | Tạo object |
Destroy | Xóa object |
| Coroutine | Chạy logic theo thời gian |
| Method | Khi Unity gọi | Dùng để |
|---|
Awake | Khi component được load | Cache reference, init sớm |
OnEnable | Khi component active/enabled | Subscribe event |
Start | Trước frame update đầu tiên | Init cần object khác đã Awake |
Update | Mỗi frame | Input, logic theo frame |
FixedUpdate | Theo physics tick | Rigidbody/physics |
LateUpdate | Sau Update | Camera follow |
OnDisable | Khi component disabled | Unsubscribe event |
OnDestroy | Trước khi object bị destroy | Cleanup |
private Rigidbody rb = null!;
rb = GetComponent<Rigidbody>();
rb.MovePosition(rb.position + Vector3.forward * Time.fixedDeltaTime);
float horizontal = Input.GetAxisRaw("Horizontal");
Vector3 move = new(horizontal, 0f, 0f);
transform.Translate(move * speed * Time.deltaTime);
| Giá trị | Dùng khi |
|---|
Time.deltaTime | Di chuyển theo frame trong Update |
Time.fixedDeltaTime | Physics trong FixedUpdate |
void OnTriggerEnter(Collider other)
if (other.CompareTag("Coin"))
Destroy(other.gameObject);
void OnCollisionEnter(Collision collision)
Debug.Log(collision.gameObject.name);
public class PlayerMove2D : MonoBehaviour
[SerializeField] private float speed = 5f;
Vector2 input = new(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
transform.Translate(input.normalized * speed * Time.deltaTime);
public class PlayerMove3D : MonoBehaviour
[SerializeField] private float speed = 5f;
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
Vector3 move = new(h, 0f, v).normalized;
transform.Translate(move * speed * Time.deltaTime);
public class Jump : MonoBehaviour
[SerializeField] private float force = 6f;
private Rigidbody rb = null!;
void Awake() => rb = GetComponent<Rigidbody>();
if (Input.GetKeyDown(KeyCode.Space))
rb.AddForce(Vector3.up * force, ForceMode.Impulse);
public class Health : MonoBehaviour
[SerializeField] private int maxHealth = 100;
private int currentHealth;
void Awake() => currentHealth = maxHealth;
public void TakeDamage(int damage)
if (currentHealth <= 0) Destroy(gameObject);
IEnumerator SpawnAfterDelay(GameObject prefab)
yield return new WaitForSeconds(2f);
Instantiate(prefab, transform.position, Quaternion.identity);
public class Spawner : MonoBehaviour
[SerializeField] private GameObject prefab = null!;
Instantiate(prefab, transform.position, Quaternion.identity);
public class CameraFollow : MonoBehaviour
[SerializeField] private Transform target = null!;
[SerializeField] private Vector3 offset = new(0f, 5f, -8f);
transform.position = target.position + offset;
- Di chuyển physics trong
Update thay vì FixedUpdate.
- Quên nhân
Time.deltaTime khiến tốc độ phụ thuộc FPS.
- Gọi
GetComponent liên tục trong Update.
- Dùng
DestroyImmediate khi đang Play Mode; thường dùng Destroy.
- So sánh tag bằng
other.tag == "Player"; nên dùng CompareTag.
- Subscribe event trong
OnEnable nhưng quên unsubscribe trong OnDisable.
- Quên kéo prefab/reference vào Inspector cho field
[SerializeField].
Unity C#, MonoBehaviour, Awake, Start, Update, FixedUpdate, LateUpdate, OnEnable, OnDisable, SerializeField, GameObject, Transform, Rigidbody, Collider, OnTriggerEnter, OnCollisionEnter, Instantiate, Destroy, Coroutine, Camera follow, unity movement, di chuyển nhân vật