110 likes | 208 Views
Learning Unity. A Simple Game. Just by adding in a few platforms, we can make a game. Try to get from the left platform to the right one. Prefabs. A Prefab is a tool for combining objects together. First create an object Created a sphere with a sphere above and below (all attached together)
E N D
A Simple Game • Just by adding in a few platforms, we can make a game. • Try to get from the left platform to the right one.
Prefabs • A Prefab is a tool for combining objects together. • First create an object • Created a sphere with a sphere above and below (all attached together) • Create Prefab • Drag into Prefab
Create the Prefab • First delete the original instance • Create new script • Instantiate to create object. Creates on top of script object • Add script to actor • Link object to prefab
Prefabs Extended • Prefabs also carry any scripts that were attached to them • You can modify Prefabs by first instantiating them • Drag to the main screen • Then editing them • Then dragging back to the prefab • Add a RididBody • Modify code • Instantiate(object, transform.position + Vector3(0,0,2), transform.rotation) ; • Now object appears in front of you.
Responding to Mouse • Mouse Click • Type 1: Input.GetMouseButtonDown(button) • Test is button has been pressed • Type 2: function OnMouseDown() {} • Called when this object is clicked on • Now create instance when you click
Responding to Key • Input.GetKeyDown(key) • Returns true if key was just pressed • If – else if • Only test the “else if” condition if the “if” condition fails • Comments • Human only • Not read by computer
Forces • To apply a force to an object • First, extract the Rigidbody component • Second, set the velocity on that component • Optional: AddForce – allows you to apply a constant force on the object • Also, instantiation modification • Position at transform.rotation * Vector3(0,0,2) • This will place the object in front of where you are currently looking
Removing an Object • Create new Script (Name: DestroyOnClick) • Attach to the prefab • Call the Destroy method • Can be used on the • gameObject • Removes the whole thing • Transform • ERROR: do not do • this • Destroy the script itself • Any other script
Global variables • Static keyword • Create variable that anyone can access • Not specific to an instance • Tracks how many objects have been clicked on
Showing score • Create new GUIText • GameObject -> Create Other -> GUI Text • Place so you can see in world • Will stay static in position with reference to you • Attach a script to it (name: UpdateScore) • Every tick, update score to reflect number clicked