Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
unity_game_dev [2020/04/12 00:15] paul |
unity_game_dev [2020/04/18 21:02] (current) paul [Unity Engine] |
||
---|---|---|---|
Line 6: | Line 6: | ||
Unity comes with a bunch of tools that you have to install from the package manager. This is confusing because they have both an asset store and a package manager. You can install Probuilder, which lets you design, prototype and play-test levels rapidly in the Unity Editor. | Unity comes with a bunch of tools that you have to install from the package manager. This is confusing because they have both an asset store and a package manager. You can install Probuilder, which lets you design, prototype and play-test levels rapidly in the Unity Editor. | ||
+ | |||
+ | ==== Cameras ==== | ||
+ | |||
+ | To set a camera' | ||
+ | |||
+ | ===== Reference Frames ===== | ||
+ | |||
+ | Unity uses left handed chirality. | ||
+ | |||
+ | Forward in Unity is the +z axis. | ||
+ | Right is +x, up is +y. | ||
===== Prefabs ===== | ===== Prefabs ===== | ||
Line 57: | Line 68: | ||
// You usually create a rotation in euler and then convert to quaternion. | // You usually create a rotation in euler and then convert to quaternion. | ||
- | + | </ | |
- | ==== Convertions ==== | + | === Conversions |
<code c#> | <code c#> | ||
// Convert from Quaternion to Euler: | // Convert from Quaternion to Euler: | ||
Line 120: | Line 131: | ||
[[https:// | [[https:// | ||
- | ==== Unity Helpers ==== | + | ===== Unity Helpers |
Unity is filled with awesome functions that handle complex timing for you. | Unity is filled with awesome functions that handle complex timing for you. | ||
- | InvokeRepeating is one them. It lets you run a script on a timer at watever rate you like. [[https:// | + | The '' |
+ | |||
+ | Here is an example from [[https:// | ||
+ | |||
+ | <code c#> | ||
+ | if (Input.GetButtonDown(" | ||
+ | { | ||
+ | InvokeRepeating(" | ||
+ | } else if (Input.GetButtonDown(" | ||
+ | { | ||
+ | CancelInvoke(" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | private void Shoot() | ||
+ | { | ||
+ | RaycastHit _hit; | ||
+ | if(Physics.Raycast(cam.transform.position, | ||
+ | { | ||
+ | // We hit something | ||
+ | Debug.Log(" | ||
+ | if(_hit.collider.tag == PLAYER_TAG) | ||
+ | { | ||
+ | CmdPlayerShot(_hit.collider.name, | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Audio ===== | ||
+ | |||
+ | Here's a method that will play random audio noises from a list. | ||
+ | |||
+ | <code c#> | ||
+ | public class Shoot : MonoBehaviour | ||
+ | { | ||
+ | [SerializeField] private AudioSource sound; | ||
+ | [SerializeField] private AudioClip[] m_Sounds; | ||
+ | |||
+ | // Update is called once per frame | ||
+ | void Update() | ||
+ | { | ||
+ | if(Input.GetButtonDown(" | ||
+ | { | ||
+ | // pick & play a random footstep sound from the array, | ||
+ | // excluding sound at index 0 | ||
+ | int n = Random.Range(1, | ||
+ | sound.clip = m_Sounds[n]; | ||
+ | sound.PlayOneShot(sound.clip); | ||
+ | // move picked sound to index 0 so it's not picked next time | ||
+ | m_Sounds[n] = m_Sounds[0]; | ||
+ | m_Sounds[0] = sound.clip; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== UI ===== | ||
+ | |||
+ | ==== InputField ==== | ||
+ | |||
+ | Here's an input field for text set by a button. The script is attached to an empty game object in the scene. | ||
+ | <code c#> | ||
+ | using System.Collections; | ||
+ | using System.Collections.Generic; | ||
+ | using UnityEngine; | ||
+ | using UnityEngine.UI; | ||
+ | |||
+ | public class NameTransfer : MonoBehaviour | ||
+ | { | ||
+ | public string theName; | ||
+ | public GameObject inputField; | ||
+ | public GameObject textDisplay; | ||
+ | |||
+ | |||
+ | public void StoreName() | ||
+ | { | ||
+ | theName = inputField.GetComponent< | ||
+ | textDisplay.GetComponent< | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Rect Transform ==== | ||
+ | |||
+ | These friggen things. If you press shift and then alt, something happens. Will need to dive into this at one point. | ||
+ | |||
+ | {{:: | ||
+ | |||
+ | ===== Unity Engine ===== | ||
+ | |||
+ | Objects of classes always have an object of that type attached to them. | ||
+ | |||
+ | <code c#> | ||
+ | namespace UnityEngine | ||
+ | { | ||
+ | // | ||
+ | // Summary: | ||
+ | // Base class for everything attached to GameObjects. | ||
+ | [NativeClass(" | ||
+ | [NativeHeader(" | ||
+ | [RequiredByNativeCode] | ||
+ | public class Component : Object | ||
+ | { | ||
+ | public Component(); | ||
+ | |||
+ | // | ||
+ | // Summary: | ||
+ | // The game object this component is attached to. A component is always attached | ||
+ | // to a game object. | ||
+ | public GameObject gameObject { get; } | ||
+ | // | ||
+ | // Summary: | ||
+ | // The tag of this game object. | ||
+ | public string tag { get; set; } | ||
+ | // | ||
+ | // Summary: | ||
+ | // The Rigidbody attached to this GameObject. (Null if there is none attached). | ||
+ | [EditorBrowsable(EditorBrowsableState.Never)] | ||
+ | [Obsolete(" | ||
+ | public Component rigidbody { get; } | ||
+ | // | ||
+ | // Summary: | ||
+ | // The Rigidbody2D that is attached to the Component' | ||
+ | [EditorBrowsable(EditorBrowsableState.Never)] | ||
+ | [Obsolete(" | ||
+ | public Component rigidbody2D { get; } | ||
+ | // | ||
+ | // Summary: | ||
+ | // The Camera attached to this GameObject. (Null if there is none attached). | ||
+ | [EditorBrowsable(EditorBrowsableState.Never)] | ||
+ | [Obsolete(" | ||
+ | public Component camera { get; } | ||
+ | // | ||
+ | // Summary: | ||
+ | // The Light attached to this GameObject. (Null if there is none attached). | ||
+ | [EditorBrowsable(EditorBrowsableState.Never)] | ||
+ | [Obsolete(" | ||
+ | public Component light { get; } | ||
+ | // | ||
+ | // Summary: | ||
+ | // The Animation attached to this GameObject. (Null if there is none attached). | ||
+ | [EditorBrowsable(EditorBrowsableState.Never)] | ||
+ | [Obsolete(" | ||
+ | public Component animation { get; } | ||
+ | // | ||
+ | // Summary: | ||
+ | // The Transform attached to this GameObject. | ||
+ | public Transform transform { get; } | ||
+ | // | ||
+ | // Summary: | ||
+ | // The ConstantForce attached to this GameObject. (Null if there is none attached). | ||
+ | [EditorBrowsable(EditorBrowsableState.Never)] | ||
+ | [Obsolete(" | ||
+ | public Component constantForce { get; } | ||
+ | // | ||
+ | // Summary: | ||
+ | // The AudioSource attached to this GameObject. (Null if there is none attached). | ||
+ | [EditorBrowsable(EditorBrowsableState.Never)] | ||
+ | [Obsolete(" | ||
+ | public Component audio { get; } | ||
+ | // | ||
+ | // Summary: | ||
+ | // The GUIText attached to this GameObject. (Null if there is none attached). | ||
+ | [EditorBrowsable(EditorBrowsableState.Never)] | ||
+ | [Obsolete(" | ||
+ | public Component guiText { get; } | ||
+ | // | ||
+ | // Summary: | ||
+ | // The NetworkView attached to this GameObject (Read Only). (null if there is none | ||
+ | // | ||
+ | [EditorBrowsable(EditorBrowsableState.Never)] | ||
+ | [Obsolete(" | ||
+ | public Component networkView { get; } | ||
+ | [EditorBrowsable(EditorBrowsableState.Never)] | ||
+ | [Obsolete(" | ||
+ | public Component guiElement { get; } | ||
+ | // | ||
+ | // Summary: | ||
+ | // The GUITexture attached to this GameObject (Read Only). (null if there is none | ||
+ | // | ||
+ | [EditorBrowsable(EditorBrowsableState.Never)] | ||
+ | [Obsolete(" | ||
+ | public Component guiTexture { get; } | ||
+ | // | ||
+ | // Summary: | ||
+ | // The Collider attached to this GameObject. (Null if there is none attached). | ||
+ | [EditorBrowsable(EditorBrowsableState.Never)] | ||
+ | [Obsolete(" | ||
+ | public Component collider { get; } | ||
+ | // | ||
+ | // Summary: | ||
+ | // The Collider2D component attached to the object. | ||
+ | [EditorBrowsable(EditorBrowsableState.Never)] | ||
+ | [Obsolete(" | ||
+ | public Component collider2D { get; } | ||
+ | // | ||
+ | // Summary: | ||
+ | // The Renderer attached to this GameObject. (Null if there is none attached). | ||
+ | [EditorBrowsable(EditorBrowsableState.Never)] | ||
+ | [Obsolete(" | ||
+ | public Component renderer { get; } | ||
+ | // | ||
+ | // Summary: | ||
+ | // The HingeJoint attached to this GameObject. (Null if there is none attached). | ||
+ | [EditorBrowsable(EditorBrowsableState.Never)] | ||
+ | [Obsolete(" | ||
+ | public Component hingeJoint { get; } | ||
+ | // | ||
+ | // Summary: | ||
+ | // The ParticleSystem attached to this GameObject. (Null if there is none attached). | ||
+ | [EditorBrowsable(EditorBrowsableState.Never)] | ||
+ | [Obsolete(" | ||
+ | public Component particleSystem { get; } | ||
+ | </ | ||
+ | |||
+ | ===== Debug ===== | ||
+ | |||
+ | You can format your debug colors! | ||
+ | |||
+ | <code c#> | ||
+ | Debug.LogError("< | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Multiplayer ===== | ||
+ | |||
+ | There multiple ways of doing multiplayer in Unity. Unity used to support a multiplayer system called UNet but it was deprecated in 2019 and removed from the codebase. They have yet to fully roll out their new version. Read about it [[https:// | ||
+ | |||
+ | Another option is to use Photon' | ||
+ | |||
+ | ==== Photon PUN ==== | ||
+ | === PhotonView === | ||
+ | A [[https:// | ||