unity programming 1

13
UNITY PROGRAMMING Petri Lankoski

Upload: petri-lankoski

Post on 27-Nov-2014

1.661 views

Category:

Technology


2 download

DESCRIPTION

Slides from introd

TRANSCRIPT

Page 1: Unity programming 1

UNITY PROGRAMMING

Petri Lankoski

Page 2: Unity programming 1

Contents

Waypoint track Enemy following waypoint track PlayerLogic keeping track of Power and

Health statsDisplaying health & power meter with GUI

Scaling GUI to different screen sizes PowerUp

Page 3: Unity programming 1

Waypointpublic class Waypoint : MonoBehaviour {

public Transform nextTarget;

public string agentTag; // Tag of the things we set the new target

void OnTriggerEnter(Collider other) {

if(other.gameObject.CompareTag(agentTag)) {

SendNextTarget(other.gameObject);

}

}

void OnDrawGizmos() {

Gizmos.DrawIcon (transform.position, "waypoint.psd");

if(nextTarget) {

Gizmos.color = Color.red;

Gizmos.DrawLine(transform.position, nextTarget.position);

}

}

private void SendNextTarget(GameObject obj) {

Enemy script = obj.GetComponent<Enemy>();

script.SetTarget(nextTarget);

}

}

Page 4: Unity programming 1

Waypoint

SetupWaypoint prefab

○ With Waypoint script attached○ With collider in trigger mode (is trigger set)

Gizmos/waypoint.psd○ Size should be 32x32 or 25x25 pixels

Create a track using waypoints TEST

Waypoint.psd(etc, other formats Works)

Page 5: Unity programming 1

Waypoint setup, one track

Page 6: Unity programming 1

Waypoint setup, two tracks

Page 7: Unity programming 1

Enemy Following Waypoints Enemy c# class

Enemy target in public Transform○ So we can set the first target waypoint on the inspector

Implement ○ public void SetTarget(Transform newTarget) { /* Store newTarget to

private variable of Enemy class here*/ } Make enemy move by the track set with Waypoints

○ Use Update() for this○ transform.LookAt()○ transform.Translate(), Time.deltaTime

Create Enemy prefeb Create new tag and set tag to Enemy (or something depending the

value of agentTag) Set the tag to Enemy (or to match the AgentTag variable) to the

waypoints on your track TEST

Page 8: Unity programming 1

Second Enemy

Make an enemy follow different trackCreate new tag and set tag to EnemySet the tag (to the AgentTag variable) to the

waypoints on your track

Page 9: Unity programming 1

Player Object Add FirstPersonController to

scene Standard Assets/Character

Controllers Create PlayerLogic.cs

Add meters for health and power OnGUI():

○ GUI.matrix = GUIGlobals .GetGUIMatrix();

○ Use GUI.DrawTexture() & changing the height or width of the texture

○ Drawing are for DrawTexture: new Rect(x, y, width, height)

power & health as public variable for testing

public class GUIGlobals {

public const float screenWidth = 1280.0f;

public const float screenHeight = 800.0f;

public static Matrix4x4 GetGUIMatrix() {

float xScale=Screen.widt /screenWidth;

float yScale=Screen.height/screenHeight;

return Matrix4x4.TRS(

Vector3.zero,

Quaternion.identity,

new Vector3(xScale, yScale, 1)

);

}

}

Page 10: Unity programming 1

GUI.matrix & GUIGlobals Scales the GUI for

you in any screen sizes

Layout according to values set in GUIGlobals

0,0

0,GUIGlobals.screenHeight

GUIGlobals.screenWidth,GUIGlobals.screenHeight

GUIGlobals.screenWidth,0

new Rect(GUIGlobals.screenWidth-40,GUIGlobals.screenHeight-40,20,20)

Page 11: Unity programming 1

Shortcut to Player, Singletonpublic class GameAgents : MonoBehaviour {

private static GameAgents instance;

private GameObject player;

private PlayerLogic playerLogic;

void Awake() {

instance = this;

player = GameObject.FindWithTag("Player");

playerLogic = player.GetComponent<PlayerLogic>();

}

public static GameObject GetPlayer() { return instance.player;}

public static PlayerLogic GetPlayerLogic() {

return instance.playerLogic; 

}

}

Page 12: Unity programming 1

GameAgents

Add to an empty game object Allows easy access to Player object and

PlayerLogic classes from other classesGameObject pc = GameAgents.GetPlayer();PlayerLogic pl =

GameAgents.GetPlayerLogic();GameAgents.GetPlayerLogic().enabled =

false;GameAgents.GetPlayerLogic().FunctionNa

me(4,6);

Page 13: Unity programming 1

PowerUp Expose health and power on inspector

public int health, power; Collider in trigger mode () Catch collision OnTriggerEnter(Collider other)

Check if player object collided with it○ if(other.gameObject == GameAgents.GetPlayer()) …

This is faster than tag comparison used in Waypoint

Send Health and Power to PlayerLogic script○ GameAgents.GetPlayerLogic().PowerUp(health,pow

er)○ You need to add a function for that to PlayerLogic

public void PowerUp(int powerAdd, int healtAhh) {