unity遊戲程式設計(15) 實作space shooter遊戲

Download Unity遊戲程式設計(15) 實作Space shooter遊戲

Post on 22-Jan-2018

820 views

Category:

Software

0 download

Embed Size (px)

TRANSCRIPT

  1. 1. Unity(15) Space shooter June 12, 2017
  2. 2. shapethefuture Space Shooter tutorial 2 Wu, ShyiShiou Dept. of E.E., NKUT
  3. 3. shapethefuture https://unity3d.com/learn/tutorials/projects/space-shooter-tutorial 3 Wu, ShyiShiou Dept. of E.E., NKUT
  4. 4. shapethefuture 3D File> New Project Space shooter Main.unity Space Shooter tutorial.unitypackage Assets> Import Package> Custom Package 1/2 4 Wu, ShyiShiou Dept. of E.E., NKUT
  5. 5. shapethefuture File> Build Settings Player Settings Default Is Full Screen* Default Screen Width: 600 Default Screen Height: 900 2/2 5 Wu, ShyiShiou Dept. of E.E., NKUT
  6. 6. shapethefuture Models/vehicle_playerShipHiearchy Player Transform Player Component> Physics> Rigidbody Use Gravity 1/2 6 Wu, ShyiShiou Dept. of E.E., NKUT
  7. 7. shapethefuture Player Component> Physics> Mesh Collider Convex Is Trigger Models/player_ship_colliderMesh Player PrefabsVFXEnginesengines_playerPlayer 2/2 7 Wu, ShyiShiou Dept. of E.E., NKUT
  8. 8. shapethefuture Main CameraTransform Main Camera Rotation (X, Y, Z) = (90, 0, 0) Main CameraPlayer Position (X, Y, Z) = (0, 10, 0) Main Camera Projection = Orthographic Size = 10 Player Main Camera Position (X, Y, Z) = (0, 10, 5) 1/2 8 Wu, ShyiShiou Dept. of E.E., NKUT
  9. 9. shapethefuture Main Camera Clear Flags = Solid Color Background = black 2/2 9 Wu, ShyiShiou Dept. of E.E., NKUT
  10. 10. shapethefuture Directional Light Window> Lighting> SettingsLighting Skybox Material = none Ambient Color = black 1/3 10 Wu, ShyiShiou Dept. of E.E., NKUT
  11. 11. shapethefuture GameObject> Light> Directional LightMain Light Position Rotation (X, Y, Z) = (20, -115, 0) Edit> Duplicate (Ctrl-D)Main LightFill Light Rotation Intensity = 0.5 Rotation (X, Y, Z) = (5, 125, 0) Color (R, G, B) = (128, 192, 192) 2/3 11 Wu, ShyiShiou Dept. of E.E., NKUT
  12. 12. shapethefuture Fill LightRim Light Transform Color = white Rotation (X, Y, Z) = (-15, 65, 0) Intensity = 0.25 GameObject> Create EmptyLighting Transform Position (X, Y, Z) = (0, 100, 0) Main LightFill LightRim LightLighting 3/3 12 Wu, ShyiShiou Dept. of E.E., NKUT
  13. 13. shapethefuture GameObject> 3D Object> QuadQuad Background Transform BackgroundMain Camera Rotation (X, Y, Z) = (90, 0, 0) Background Scale (X, Y, Z) = (15, 30, 0) Materialstile_nebula_green_diffBackground 1/2 13 Wu, ShyiShiou Dept. of E.E., NKUT
  14. 14. shapethefuture Mesh Collider ShaderUnlit/Texture BackgroundPlayer Position (X, Y, Z) = (0, -10, 0) 2/2 14 Wu, ShyiShiou Dept. of E.E., NKUT
  15. 15. shapethefuture PlayerPlayerController using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour { void FixedUpdate () { Rigidbody rigidBody = GetComponent (); float moveHorizontal = Input.GetAxis ("Horizontal"); float moveVertical = Input.GetAxis ("Vertical"); Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical); rigidBody.velocity = movement; } } 1/5 15 Wu, ShyiShiou Dept. of E.E., NKUT
  16. 16. shapethefuture Player PlayerController public class PlayerController : MonoBehaviour { public float speed; void FixedUpdate () { Rigidbody rigidBody = GetComponent (); float moveHorizontal = Input.GetAxis ("Horizontal"); float moveVertical = Input.GetAxis ("Vertical"); Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical); rigidBody.velocity = movement * speed; } } 2/5 16 Wu, ShyiShiou Dept. of E.E., NKUT
  17. 17. shapethefuture PlayerControllerSpeed10 Player Player PlayerControllerPlayer using System.Collections; using System.Collections.Generic; using UnityEngine; [System.Serializable] public class Player_Boundary { public float xMin, xMax, zMin, zMax; } 3/5 17 Wu, ShyiShiou Dept. of E.E., NKUT
  18. 18. shapethefuture public class PlayerController : MonoBehaviour { public float speed; public float tilt; public Player_Boundary boundary; void FixedUpdate () { Rigidbody rigidBody = GetComponent (); float moveHorizontal = Input.GetAxis ("Horizontal"); float moveVertical = Input.GetAxis ("Vertical"); Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical); rigidBody.velocity = movement * speed; rigidBody.position = new Vector3 ( Mathf.Clamp (rigidBody.position.x, boundary.xMin, boundary.xMax), 0.0f, Mathf.Clamp (rigidBody.position.z, boundary.zMin, boundary.zMax)); rigidBody.rotation = Quaternion.Euler ( 0.0f, 0.0f, rigidBody.velocity.x * -tilt); } } 4/5 18 Wu, ShyiShiou Dept. of E.E., NKUT
  19. 19. shapethefuture PlayerBoundry PlayerPositionX PlayerPositionZ Player 5/5 19 Wu, ShyiShiou Dept. of E.E., NKUT
  20. 20. shapethefuture GameObject> Create EmptyBolt Transform GameObject> 3D Object> QuadVFX Transform VFXMain Camera Rotation (X, Y, Z) = (90, 0, 0) Mesh Collider VFXBolt 1/4 20 Wu, ShyiShiou Dept. of E.E., NKUT
  21. 21. shapethefuture Assets> Create>Material fx_bolt_orange Shader = Particles/Additive Particle Texture = fx_lazer_orange_dff fx_bolt_orangeVFX BoltRigidbody Use Gravity BoltCapsule Collider Direction = Z-Axis Radius = 0.03Height = 0.5 Is Trigger 2/4 21 Wu, ShyiShiou Dept. of E.E., NKUT
  22. 22. shapethefuture BoltMover using System.Collections; using System.Collections.Generic; using UnityEngine; public class Mover : MonoBehaviour { public float speed; // Use this for initialization void Start () { GetComponent().velocity = transform.forward * speed; } } 3/4 22 Wu, ShyiShiou Dept. of E.E., NKUT
  23. 23. shapethefuture BoltPrefabs Speed = 20 Bolt BoltHierarchyGame 4/4 23 Wu, ShyiShiou Dept. of E.E., NKUT
  24. 24. shapethefuture GameObject> Create EmptyShot Spawn Transform Shot SpawnPlayer Shot SpawnPosition (X, Y, Z) = (0, 0, 1) 1/3 24 Wu, ShyiShiou Dept. of E.E., NKUT
  25. 25. shapethefuture PlayerPlayerController public class PlayerController : MonoBehaviour { public float speed; public float tilt; public Player_Boundary boundary; public GameObject shot; public Transform shotSpawn; public float fireRate; private float nextFire; void Update () { if (Input.GetButton("Fire1") && Time.time > nextFire) { nextFire = Time.time + fireRate; Instantiate(shot, shotSpawn.position, shotSpawn.rotation); } } } 2/3 25 Wu, ShyiShiou Dept. of E.E., NKUT
  26. 26. shapethefuture BoltPlayer ControllerShot Shot SpawnPlayer ControllerShot Spawn Player ControllerFire Rate0.25 Hierarchy 3/3 26 Wu, ShyiShiou Dept. of E.E., NKUT
  27. 27. shapethefuture GameObject> 3D Object> CubeBoundary Transform Box ColliderIs Trigger Boundary Position (X, Y, Z) = (0, 0, 5) Main CameraZ Boundary Scale (X, Y, Z) = (15, 1, 20) Mesh Renderer Mesh Filter 1/2 27 Wu, ShyiShiou Dept. of E.E., NKUT
  28. 28. shapethefuture BoundaryDestroyByBoundry using System.Collections; using System.Collections.Generic; using UnityEngine; public class DestroyByBoundary : MonoBehaviour { void OnTriggerExit(Collider other) { Destroy(other.gameObject); } } HirarchyBundary 2/2 28 Wu, ShyiShiou Dept. of E.E., NKUT
  29. 29. shapethefuture GameObject> Create EmptyAsteroid Position (X, Y, Z) = (0, 0, 8) Models/prop_asteroid_01Asteroid prop_asteroid_01Transform AsteroidRigidbody Use Gravity AsteroidCapsule Collider RadiusHeight Direction = Z-Axis Radius = 0.486 Height = 1.524 1/4 29 Wu, ShyiShiou Dept. of E.E., NKUT
  30. 30. shapethefuture AsteroidRandomRotator using System.Collections; using System.Collections.Generic; using UnityEngine; public class RandomRotator : MonoBehaviour { public float tumble; void Start () { GetComponent().angularVelocity = Random.insideUnitSphere*tumble; } } AsteroidTumble5 ... 2/4 30 Wu, ShyiShiou Dept. of E.E., NKUT
  31. 31. shapethefuture AsteroidDestroyByContact using System.Collections; using System.Collections.Generic; using UnityEngine; public class DestroyByContact : MonoBehaviour { void OnTriggerEnter(Collider other) { if (other.tag == "Boundary") { return; } Destroy(other.gameObject); Destroy(gameObject); } } 3/4 31 Wu, ShyiShiou Dept. of E.E., NKUT
  32. 32. shapethefuture BoundaryTagBoundary 4/4 32 Wu, ShyiShiou Dept. of E.E., NKUT
  33. 33. shapethefuture DestroyByContact public class DestroyByContact : MonoBehaviour { public GameObject explosion; void OnTriggerEnter(Collider other) { if (other.tag == "Boundary") { return; } Instantiate(explosion, transform.position, transform.rotation); Destroy(other.gameObject); Destroy(gameObject); } } PrefabsVFXExplosionsexplosion_asteroid explosion 1/4 33 Wu, ShyiShiou Dept. of E.E., NKUT
  34. 34. shapethefuture ... PlayerTagPlayer DestroyByContact public class DestroyByContact : MonoBehaviour { public GameObject explosion; public GameObject playerExplosion; 2/4 34 Wu, ShyiShiou Dept. of E.E., NKUT
  35. 35. shapethefuture void OnTriggerEnter(Collider other) { ... Instantiate(explosion, transform.position, transform.rotation); if (other.tag == "Player") { Instantiate(playerExplosion, other.transform.position, other.transform.rotation); } Destroy(other.gameObject); Destroy(gameObject); } } PrefabsVFXExplosionsexplosion_player playerExplosion 3/4 35 Wu, ShyiShiou Dept. of E.E., NKUT
  36. 36. shapethefuture AsteroidMover Speed-5 AsteroidPrefabs Asteroid 4/4 36 Wu, ShyiShiou Dept. of E.E., NKUT
  37. 37. shapethefuture GameObject> Create EmptyGame Controller Transform TagGameController 1/3 37 Wu, ShyiShiou Dept. of E.E., NKUT
  38. 38. shapethefuture Game ControllerGameController using System.Collections; using System.Collections.Generic; using UnityEngine; public class GameController : MonoBehaviour { public GameObject hazard