Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 3 Richard Gesick

Similar presentations


Presentation on theme: "Lecture 3 Richard Gesick"— Presentation transcript:

1 Lecture 3 Richard Gesick
GUI Ch 2 Lecture 3 Richard Gesick

2 objectives Both 2D and 3D GUI elements How to create GUI buttons
How to create a 2D health bar Tracking the player's level by using a GUI label Using two GUI boxes to make an experience bar Creating a 3D health bar Showing 3D damage reports Showing enemy name tags

3 2D List<Rect> SkillButtons = new List<Rect>();
List<Rect> ItemButtons = new List<Rect>(); SkillButtons.Add(new Rect(Screen.width/2 + 50, Screen.height/ , 55, 55)); SkillButtons.Add(new Rect(Screen.width/ , Screen.height/ , 55, 55)); ItemButtons.Add(new Rect(Screen.width/ , Screen.height/ , 55, 55)); Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

4 OnGUI() method GUI.Button(SkillButtons[0], "Skill A");
GUI.Button(SkillButtons[1], "Skill B"); GUI.Button(ItemButtons[0], "Item A"); Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

5 A health bar with a GUI box
public float currentHP = 100; public float maxHP = 100; public float currentBarLength; public float maxBarLength = 100; . . . currentBarLength = currentHP * maxBarLength / maxHP; GUI.Box(new Rect(Screen.width/2 - 20, Screen.height/ , currentBarLength, 25f), ""); Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

6 3D txtColor = DamageReport.color; txtColor.a = 0; // 0 transparent, 1 fully opague HealthBar.transform.LookAt(Camera.main.transform); DamageReport.text = Damage.ToString(); txtColor.a = 1; HealthBar.transform.localScale = Vector3.Lerp(OrigScale, new Vector3(currentBarLength, OrigScale.y,OrigScale.z), Time.fixedTime); Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

7 Assignments Rather than assign the 3D gui to the object, assign to a global object, i.e. the camera. To maintain visibility, in update use: HealthBar.transform.LookAt(Camera.main.transform); NameTag.transform.LookAt (new Vector3(-Camera.main.transform.position.x, -Camera.main.transform.position.y, -Camera.main.transform.position.z)); Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

8 bullets Prefab bullet GameObject instantiatedProjectile= Instantiate(bullet, new Vector3(transform.position.x, transform.position.y + 1, transform.position.z + 1), transform.rotation) as GameObject; //Or make a shot spawn position Destroy(instantiatedProjectile, 2f); Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

9 collisions OnTriggerEnter(Collider col)
OnCollisionEnter(Collision col) Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley


Download ppt "Lecture 3 Richard Gesick"

Similar presentations


Ads by Google