Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
unity_game_dev [2020/04/12 00:16]
paul [Unity Helpers]
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's view to be aligned with your scene view, select the camera and in the GameObject menu select "Align with Scene View".
 +
 +===== 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.
- +</code> 
-==== Convertions ====+=== Conversions ===
 <code c#> <code c#>
 // Convert from Quaternion to Euler: // Convert from Quaternion to Euler:
Line 126: Line 137:
 The ''InvokeRepeating()'' method is one them. It lets you run a script on a timer at watever rate you like. [[https://docs.unity3d.com/ScriptReference/MonoBehaviour.InvokeRepeating.html|docs]]. The ''InvokeRepeating()'' method is one them. It lets you run a script on a timer at watever rate you like. [[https://docs.unity3d.com/ScriptReference/MonoBehaviour.InvokeRepeating.html|docs]].
  
 +Here is an example from [[https://www.youtube.com/watch?v=bDEUy2xHeuU|this tutorial video]] on how to use it to handle it automatic weapon firing.
 +
 +<code c#>
 +if (Input.GetButtonDown("Fire1"))
 +{
 +    InvokeRepeating("Shoot", 0f, 1f / currentWeapon.fireRate);
 +} else if (Input.GetButtonDown("Fire1"))
 +    {
 +        CancelInvoke("Shoot");
 +    }
 +}
 +
 +    private void Shoot()
 +    {
 +        RaycastHit _hit;
 +        if(Physics.Raycast(cam.transform.position, cam.transform.forward, out _hit, currentWeapon.range, mask ))
 +        {
 +            // We hit something
 +            Debug.Log("We hit " + _hit.collider.name);
 +            if(_hit.collider.tag == PLAYER_TAG)
 +            {
 +                CmdPlayerShot(_hit.collider.name, currentWeapon.damage);
 +            }
 +        }
 +    }
 +</code>
 +
 +===== 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;    // an array of sounds that will be selected randomly
 +
 +        // Update is called once per frame
 +        void Update()
 +        {
 +            if(Input.GetButtonDown("Fire1"))
 +            {
 +                // pick & play a random footstep sound from the array,
 +                // excluding sound at index 0
 +                int n = Random.Range(1, m_Sounds.Length);
 +                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;
 +            }
 +        }
 +    }
 +</code>
 +
 +===== 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<Text>().text;
 +        textDisplay.GetComponent<Text>().text = "Welcome " + theName + " to the game!";
 +    }
 +
 +}
 +</code>
 +
 +==== Rect Transform ====
 +
 +These friggen things. If you press shift and then alt, something happens. Will need to dive into this at one point.
 +
 +{{::toppanelanchorspreset.gif?400|}}
 +
 +===== 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("Unity::Component")]
 +    [NativeHeader("Runtime/Export/Component.bindings.h")]
 +    [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("Property rigidbody has been deprecated. Use GetComponent<Rigidbody>() instead. (UnityUpgradable)", true)]
 +        public Component rigidbody { get; }
 +        //
 +        // Summary:
 +        //     The Rigidbody2D that is attached to the Component's GameObject.
 +        [EditorBrowsable(EditorBrowsableState.Never)]
 +        [Obsolete("Property rigidbody2D has been deprecated. Use GetComponent<Rigidbody2D>() instead. (UnityUpgradable)", true)]
 +        public Component rigidbody2D { get; }
 +        //
 +        // Summary:
 +        //     The Camera attached to this GameObject. (Null if there is none attached).
 +        [EditorBrowsable(EditorBrowsableState.Never)]
 +        [Obsolete("Property camera has been deprecated. Use GetComponent<Camera>() instead. (UnityUpgradable)", true)]
 +        public Component camera { get; }
 +        //
 +        // Summary:
 +        //     The Light attached to this GameObject. (Null if there is none attached).
 +        [EditorBrowsable(EditorBrowsableState.Never)]
 +        [Obsolete("Property light has been deprecated. Use GetComponent<Light>() instead. (UnityUpgradable)", true)]
 +        public Component light { get; }
 +        //
 +        // Summary:
 +        //     The Animation attached to this GameObject. (Null if there is none attached).
 +        [EditorBrowsable(EditorBrowsableState.Never)]
 +        [Obsolete("Property animation has been deprecated. Use GetComponent<Animation>() instead. (UnityUpgradable)", true)]
 +        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("Property constantForce has been deprecated. Use GetComponent<ConstantForce>() instead. (UnityUpgradable)", true)]
 +        public Component constantForce { get; }
 +        //
 +        // Summary:
 +        //     The AudioSource attached to this GameObject. (Null if there is none attached).
 +        [EditorBrowsable(EditorBrowsableState.Never)]
 +        [Obsolete("Property audio has been deprecated. Use GetComponent<AudioSource>() instead. (UnityUpgradable)", true)]
 +        public Component audio { get; }
 +        //
 +        // Summary:
 +        //     The GUIText attached to this GameObject. (Null if there is none attached).
 +        [EditorBrowsable(EditorBrowsableState.Never)]
 +        [Obsolete("Property guiText has been deprecated. Use GetComponent<GUIText>() instead. (UnityUpgradable)", true)]
 +        public Component guiText { get; }
 +        //
 +        // Summary:
 +        //     The NetworkView attached to this GameObject (Read Only). (null if there is none
 +        //     attached).
 +        [EditorBrowsable(EditorBrowsableState.Never)]
 +        [Obsolete("Property networkView has been deprecated. Use GetComponent<NetworkView>() instead. (UnityUpgradable)", true)]
 +        public Component networkView { get; }
 +        [EditorBrowsable(EditorBrowsableState.Never)]
 +        [Obsolete("Property guiElement has been deprecated. Use GetComponent<GUIElement>() instead. (UnityUpgradable)", true)]
 +        public Component guiElement { get; }
 +        //
 +        // Summary:
 +        //     The GUITexture attached to this GameObject (Read Only). (null if there is none
 +        //     attached).
 +        [EditorBrowsable(EditorBrowsableState.Never)]
 +        [Obsolete("Property guiTexture has been deprecated. Use GetComponent<GUITexture>() instead. (UnityUpgradable)", true)]
 +        public Component guiTexture { get; }
 +        //
 +        // Summary:
 +        //     The Collider attached to this GameObject. (Null if there is none attached).
 +        [EditorBrowsable(EditorBrowsableState.Never)]
 +        [Obsolete("Property collider has been deprecated. Use GetComponent<Collider>() instead. (UnityUpgradable)", true)]
 +        public Component collider { get; }
 +        //
 +        // Summary:
 +        //     The Collider2D component attached to the object.
 +        [EditorBrowsable(EditorBrowsableState.Never)]
 +        [Obsolete("Property collider2D has been deprecated. Use GetComponent<Collider2D>() instead. (UnityUpgradable)", true)]
 +        public Component collider2D { get; }
 +        //
 +        // Summary:
 +        //     The Renderer attached to this GameObject. (Null if there is none attached).
 +        [EditorBrowsable(EditorBrowsableState.Never)]
 +        [Obsolete("Property renderer has been deprecated. Use GetComponent<Renderer>() instead. (UnityUpgradable)", true)]
 +        public Component renderer { get; }
 +        //
 +        // Summary:
 +        //     The HingeJoint attached to this GameObject. (Null if there is none attached).
 +        [EditorBrowsable(EditorBrowsableState.Never)]
 +        [Obsolete("Property hingeJoint has been deprecated. Use GetComponent<HingeJoint>() instead. (UnityUpgradable)", true)]
 +        public Component hingeJoint { get; }
 +        //
 +        // Summary:
 +        //     The ParticleSystem attached to this GameObject. (Null if there is none attached).
 +        [EditorBrowsable(EditorBrowsableState.Never)]
 +        [Obsolete("Property particleSystem has been deprecated. Use GetComponent<ParticleSystem>() instead. (UnityUpgradable)", true)]
 +        public Component particleSystem { get; }
 +</code>
 +
 +===== Debug =====
 +
 +You can format your debug colors!
 +
 +<code c#>
 +Debug.LogError("<Color=Red><a>Missing</a></Color> Beams Reference");
 +</code>
 +
 +
 +===== 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://support.unity3d.com/hc/en-us/articles/360001252086-UNet-Deprecation-FAQ|here]].
 +
 +Another option is to use Photon's PUN service. It is free for up to 20 concurrent users and handles matchmaking. There's an official [[https://doc.photonengine.com/en-us/pun/v2/demos-and-tutorials/pun-basics-tutorial/intro|written tutorial here]].
 +
 +==== Photon PUN ====
 +
 +=== PhotonView ===
 +A [[https://doc-api.photonengine.com/en/pun/v2/class_photon_1_1_pun_1_1_photon_view.html#details|PhotonView]] identifies an object across the network (viewID) and configures how the controlling client updates remote instances.
  
  • unity_game_dev.1586650579.txt.gz
  • Last modified: 2020/04/12 00:16
  • by paul