| Q | Pan tool |
| W | Move tool |
| E | Rotate tool |
| R | Scale tool |
| T | Rect tool |
| Ctrl+P | Play/Stop |
| Ctrl+Shift+P | Pause |
| F | Focus on selection |
| Ctrl+D | Duplicate |
using UnityEngine;
public class MyScript : MonoBehaviour
{
// Called once when script is loaded
void Awake() { }
// Called once when object is enabled
void Start() { }
// Called every frame
void Update() { }
// Called at fixed intervals (physics)
void FixedUpdate() { }
// Called after all Update functions
void LateUpdate() { }
// Called when object is destroyed
void OnDestroy() { }
} // Position
transform.position = new Vector3(0, 1, 0);
transform.localPosition = Vector3.zero;
// Rotation
transform.rotation = Quaternion.identity;
transform.eulerAngles = new Vector3(0, 90, 0);
transform.Rotate(Vector3.up, 45f);
transform.LookAt(target);
// Scale
transform.localScale = Vector3.one;
// Movement
transform.Translate(Vector3.forward * speed * Time.deltaTime);
transform.position = Vector3.Lerp(start, end, t);
transform.position = Vector3.MoveTowards(current, target, step); // Parent/child
transform.SetParent(parentTransform);
transform.parent = null; // Unparent
// Find children
Transform child = transform.Find("ChildName");
Transform child = transform.GetChild(0);
// Iterate children
foreach (Transform child in transform)
{
Debug.Log(child.name);
}
// World vs local
Vector3 worldPos = transform.TransformPoint(localPos);
Vector3 localPos = transform.InverseTransformPoint(worldPos); Rigidbody rb = GetComponent<Rigidbody>();
// Force
rb.AddForce(Vector3.up * 10f);
rb.AddForce(Vector3.up * 10f, ForceMode.Impulse);
// Velocity
rb.velocity = new Vector3(0, 5, 0);
rb.angularVelocity = Vector3.zero;
// Properties
rb.mass = 1f;
rb.drag = 0.5f;
rb.useGravity = true;
rb.isKinematic = false; // Collision (physics)
void OnCollisionEnter(Collision collision)
{
Debug.Log("Hit: " + collision.gameObject.name);
ContactPoint contact = collision.contacts[0];
}
void OnCollisionStay(Collision collision) { }
void OnCollisionExit(Collision collision) { }
// Trigger (no physics response)
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
// Do something
}
}
void OnTriggerStay(Collider other) { }
void OnTriggerExit(Collider other) { } RaycastHit hit;
if (Physics.Raycast(origin, direction, out hit, maxDistance))
{
Debug.Log("Hit: " + hit.collider.name);
Debug.Log("Point: " + hit.point);
Debug.Log("Normal: " + hit.normal);
}
// From camera
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
// Hit something
}
// Multiple hits
RaycastHit[] hits = Physics.RaycastAll(origin, direction); // Keyboard
if (Input.GetKeyDown(KeyCode.Space)) { } // Frame pressed
if (Input.GetKey(KeyCode.W)) { } // Held down
if (Input.GetKeyUp(KeyCode.Escape)) { } // Frame released
// Mouse
if (Input.GetMouseButtonDown(0)) { } // Left click
Vector3 mousePos = Input.mousePosition;
float scroll = Input.mouseScrollDelta.y;
// Axis
float h = Input.GetAxis("Horizontal"); // -1 to 1
float v = Input.GetAxis("Vertical");
float rawH = Input.GetAxisRaw("Horizontal"); // -1, 0, or 1 using UnityEngine.InputSystem;
public class PlayerInput : MonoBehaviour
{
PlayerControls controls;
void Awake()
{
controls = new PlayerControls();
}
void OnEnable()
{
controls.Enable();
controls.Player.Jump.performed += OnJump;
}
void OnDisable()
{
controls.Player.Jump.performed -= OnJump;
controls.Disable();
}
void OnJump(InputAction.CallbackContext context)
{
// Jump logic
}
void Update()
{
Vector2 move = controls.Player.Move.ReadValue<Vector2>();
}
} // Get component on same object
Renderer renderer = GetComponent<Renderer>();
// Get component in children
Collider col = GetComponentInChildren<Collider>();
Collider[] cols = GetComponentsInChildren<Collider>();
// Get component in parent
Canvas canvas = GetComponentInParent<Canvas>();
// Find objects
GameObject obj = GameObject.Find("Name");
GameObject obj = GameObject.FindWithTag("Player");
Enemy[] enemies = FindObjectsOfType<Enemy>();
// Add/remove components
Rigidbody rb = gameObject.AddComponent<Rigidbody>();
Destroy(GetComponent<Collider>()); // Instantiate prefab
public GameObject prefab;
GameObject obj = Instantiate(prefab);
GameObject obj = Instantiate(prefab, position, rotation);
GameObject obj = Instantiate(prefab, parent);
// As specific type
Enemy enemy = Instantiate(enemyPrefab);
// Destroy
Destroy(gameObject); // Destroy this object
Destroy(gameObject, 2f); // After 2 seconds
Destroy(GetComponent<Rigidbody>()); // Component
// Don't destroy on load
DontDestroyOnLoad(gameObject); IEnumerator MyCoroutine()
{
Debug.Log("Start");
yield return null; // Wait one frame
yield return new WaitForSeconds(2f);
yield return new WaitForSecondsRealtime(2f); // Unaffected by timeScale
yield return new WaitUntil(() => condition);
yield return new WaitWhile(() => condition);
yield return new WaitForEndOfFrame();
yield return new WaitForFixedUpdate();
Debug.Log("End");
}
// Start/stop
Coroutine c = StartCoroutine(MyCoroutine());
StopCoroutine(c);
StopAllCoroutines();