UFCFS D Technologies for the Web Unity 3D: Review of Topics and Related Concepts
UFCFS D Technologies for the Web Review Topics Scripting Physics and Collision Detection Prefabs Spawning Objects Camera Overlays Heads-Up Displays Publishing
UFCFS D Technologies for the Web Scripting
UFCFS D Technologies for the Web Changing an Object’s Properties via Scripts Cube Object // fragment void Update () { if(Input.GetKeyDown(KeyCode.R)){ gameObject.renderer.material.color = Color.red; } ColourSwitcher.cs
UFCFS D Technologies for the Web Inter-Script Communication TransmitReceive Transmit.cs Code to send message to Receive.cs attached to cube object Receive.cs Code to handle message sent by Transmit.cs. attached to sphere object Sphere Cube
UFCFS D Technologies for the Web Debug Utility Function Very useful debug and environment events utility function Debug.Log (“Message you to want to send to the Console Window”); Debug.Log("I have received " + numberOfMessages + ” messages from the transmit script");
UFCFS D Technologies for the Web Physics and Collision Detection
UFCFS D Technologies for the Web Example Colliders: Box Cube Box Collider
UFCFS D Technologies for the Web Example Colliders: Models Box on MeshCapsule on MeshMesh on Mesh
UFCFS D Technologies for the Web Using Colliders as ‘Colliders’ The default setting for any collider attached to an object is to restrict the object being passed through be other world objects. The collision event must be handled by a script attached to one or both of the objects involved in the collision ObjectCollisons.cs Code to handle collisions
UFCFS D Technologies for the Web Handling Events Initiated by Collisions This script is attached to the First Person Controller (Capsule Collider) void OnControllerColliderHit(hit:ControllerColliderHit){ if (hit.collider == GameObject.Find("RedSphere").collider){ Debug.Log("I've hit the Red Sphere"); }; if (hit.collider == GameObject.Find("BlueSphere").collider){ Debug.Log("I've hit the Blue Sphere"); }; Unity has a built in method for detecting collisions:
UFCFS D Technologies for the Web Using Colliders as ‘Triggers’ Object collisions may be used to generate events ‘triggers’ which can be used to update logic in the World, generate actions, instantiate (create) new objects or remove unwanted objects from the world. Using triggers is one way for the player to collect items that update values via attached scripts. ObjectTriggers.cs Code to handle collisions as event triggers. Objects defined as triggers can be ‘passed through’ by other objects
UFCFS D Technologies for the Web Tag Names and the Tag Manager To reference objects in the collision function each object must have a name (Tag) defined via the Tag Manager The tag name used in any script reference must exactly match the tag name defined in the Tag Manager So RedSphere is not the same as redSphere
UFCFS D Technologies for the Web Handling Events Initiated by Triggers This script is attached to the First Person Controller (Capsule Collider) void OnTriggerEnter(collisionInfo:Collider){ if(collisionInfo.gameObject.tag == "RedSphere"){ Debug.Log("I've gone through the Red Sphere!"); } if(collisionInfo.gameObject.tag == "BlueSphere"){ Debug.Log("I've deleted the Blue Sphere!"); Destroy(collisionInfo.gameObject); } Unity has a built in method for detecting collisions as triggers:
UFCFS D Technologies for the Web Prefabs
UFCFS D Technologies for the Web Camera Mesh Audio Listener The First Person Controller is a prefabricated object FPS.js Code to handle FPS Walker etc. Mesh collider Scripts
UFCFS D Technologies for the Web Creating Prefabs In order to create a Prefab, simply drag a object that you've created in the scene into the Project View. The object's name will turn blue to show that it is a Prefab, then rename your new Prefab The object and all its ‘children’ have been copied into the Prefab data The Prefab can now be re-used in multiple instances The original object in the Hierarchy has now become an ‘instance’ of the new Prefab.
UFCFS D Technologies for the Web Creating a Simple Prefab (Receive) Receive Receive.cs Code to handle message sent by Transmit.cs attached to sphere object Prefab created from the Receive cube by attaching a script.
UFCFS D Technologies for the Web Spawning Objects
UFCFS D Technologies for the Web Instantiate Command The Instantiate command has three parameters (arguments) Object to create, position to create it, rotation to give it: Instantiate(object,object’s position,object’s rotation);
UFCFS D Technologies for the Web Instantiate Command Position and Rotation of objects to be instantiated must be specified as Vector3 values (X,Y,Z) used as follows: GameObject aPrefab; // variable of type GameObject Instantiate(aPrefab, Vector3(0,12,30), Vector3(0,0,90));
UFCFS D Technologies for the Web Dummy Object Positioned in Scene at (0, 2, 5)
UFCFS D Technologies for the Web Dummy Object Mesh Render Switched Off
UFCFS D Technologies for the Web Camera Overlays
UFCFS D Technologies for the Web Split - Viewport Camera Layouts
UFCFS D Technologies for the Web Vertical Split-Screen x 0, y 0 Height (0.5) Width (1) Viewport
UFCFS D Technologies for the Web Creating Camera Overlays
UFCFS D Technologies for the Web Camera Depth Order cameraOne depth order = 0 cameraTwo depth order = 1 result
UFCFS D Technologies for the Web Adjust Depth - in the Inspector Window
UFCFS D Technologies for the Web Overlay and Scaling Scaled camera window overlay on main camera could be used as a follow overhead camera to aid character navigation in the world
UFCFS D Technologies for the Web Heads-Up Displays (HUDS)
UFCFS D Technologies for the Web Steps Required to Establish a HUD Acquire or create art work in an appropriate format Import the HUD artwork via Asset- Import New Asset Create a GUI Texture Game Object and associate the artwork with the texture object Position the HUD components in the interface Create scripts which initialize and update the HUD based on world states and events Attach scripts to objects that change world state and trigger events which update values in the appropriate HUD script
UFCFS D Technologies for the Web HUD Declaration and Initialization public variables associated with textures
UFCFS D Technologies for the Web Updating the HUD via Scripts (BatteryCollect.js) // static variable is accessible by other scripts i.e. its // variable scope is global public int charge; Texture2D charge1tex; Texture2D charge2tex; Texture2D charge3tex; Texture2D charge4tex; Texture2D charge0tex; // initialise GUI texture to false (don't display it) void Start(){ guiTexture.enabled = false; charge = 0; } This script is attached to the GUI Texture GameObject
UFCFS D Technologies for the Web /* the Update method checks status of charge variable which is increased via an external script each time the player collides (collects) with a battery */ void Update () { /* first battery collected assign the first texture image to guiTexture and enable (display) texture */ if(charge == 1){ guiTexture.texture = charge1tex; guiTexture.enabled = true; } // display subsequent textures to indicate power collected else if(charge == 2){ guiTexture.texture = charge2tex; } // etc. Updating the HUD via Scripts
UFCFS D Technologies for the Web Updating the HUD via Scripts (PlayerCollisions.js) void Start () { } void Update () { } public void OnTriggerEnter(Collider collisionInfo){ if(collisionInfo.gameObject.tag == "battery"){ BatteryCollect.charge++; Destroy(collisionInfo.gameObject); } This script is attached to the First Person Controller
UFCFS D Technologies for the Web Publishing for the Web
UFCFS D Technologies for the Web Choose File – Build Settings… 1. Choose Web Player 2. Add Current Scene 3. Choose Build
UFCFS D Technologies for the Web Check Colour and Layout of HTML Page Unity will create a default html page for your web player and save them both to a common folder with the name you chose after selecting build. You will need to edit the file to remove unwanted text, provide an appropriate html page title, adjust layout, and change page background colour.
UFCFS D Technologies for the Web Publish to your UWE Student Module Homepage and make sure to test everything is working. Ask a fellow student to access your page from their account to ensure file permissions are correctly set. Publish and Test!