Unity programming 1
-
Upload
petri-lankoski -
Category
Technology
-
view
1.659 -
download
2
description
Transcript of Unity programming 1
UNITY PROGRAMMING
Petri Lankoski
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
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);
}
}
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)
Waypoint setup, one track
Waypoint setup, two tracks
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
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
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)
);
}
}
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)
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;
}
}
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);
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) {