Introduction to Game Design, Prototyping, and Development: From Concept to Playable Game with Unity and C# by Jeremy Gibson

Introduction to Game Design, Prototyping, and Development: From Concept to Playable Game with Unity and C# by Jeremy Gibson

Author:Jeremy Gibson [Gibson, Jeremy]
Language: eng
Format: azw3
Publisher: Pearson Education
Published: 2014-07-04T04:00:00+00:00


* * *

1 using UnityEngine;

2 using System.Collections;

3 using System.Collections.Generic; // Necessary to use generic Lists

4

5 public class Boid : MonoBehaviour {

6 // This static List holds all Boid instances & is shared amongst them

7 static public List<Boid> boids;

8

9 // Note:This code does NOT use a Rigidbody. It handles velocity directly

10 public Vector3 velocity; // The current velocity

11 public Vector3 newVelocity; // The velocity for next frame

12 public Vector3 newPosition; // The position for next frame

13

14 public List<Boid> neighbors; // All nearby Boids

15 public List<Boid> collisionRisks; // All Boids that are too close

16 public Boid closest; // The single closest Boid

17

18 // Initialize this Boid on Awake()

19 void Awake () { // 1

20 // Define the boids List if it is still null

21 if (boids == null) { // 2

22 boids = new List<Boid>();

23 }

24 // Add this Boid to boids

25 boids.Add(this);

26

27 // Give this Boid instance a random position and velocity

28 Vector3 randPos = Random.insideUnitSphere * BoidSpawner.S.spawnRadius;

29 randPos.y = 0; // Flatten the Boid to only move in the XZ plane

30 this.transform.position = randPos;

31 velocity = Random.onUnitSphere;

32 velocity *= BoidSpawner.S.spawnVelocity;

33

34 // Initialize the two Lists

35 neighbors = new List<Boid>(); // 3

36 collisionRisks = new List<Boid>();

37

38 // Make this.transform a child of the Boids GameObject

39 this.transform.parent = GameObject.Find("Boids").transform; // 4

40

41 // Give the Boid a random color, but make sure it's not too dark

42 Color randColor = Color.black;

43 while ( randColor.r + randColor.g + randColor.b < 1.0f ) {

44 randColor = new Color(Random.value, Random.value, Random.value);

45 }

46 Renderer[] rends = gameObject.GetComponentsInChildren<Renderer>();

47 foreach ( Renderer r in rends ) {

48 r.material.color = randColor;

49 }

50

51 }

52

53 // Update is called once per frame

54 void Update () { // 5

55

56 // Get the list of nearby Boids (this Boid's neighbors)

57 List<Boid> neighbors = GetNeighbors(this); // 6

58

59 // Initialize newVelocity and newPosition to the current values

60 newVelocity = velocity;

61 newPosition = this.transform.position;

62

63 // Velocity Matching: This sets the velocity of the boid to be

64 // similar to that of its neighbors

65 Vector3 neighborVel = GetAverageVelocity( neighbors );

66 // Utilizes the fields set on the BoidSpawner.S Singleton

67 newVelocity += neighborVel * BoidSpawner.S.velocityMatchingAmt;

68

69 // Flock Centering: Move toward middle of neighbors

70 Vector3 neighborCenterOffset = GetAveragePosition( neighbors ) - this.transform.position;

71 newVelocity += neighborCenterOffset*BoidSpawner.S.flockCenteringAmt;

72

73 // Collision Avoidance: Avoid running into Boids that are too close

74 Vector3 dist;

75 if (collisionRisks.Count > 0) {

76 Vector3 collisionAveragePos=GetAveragePosition(collisionRisks);

77 dist = collisionAveragePos - this.transform.position;

78 newVelocity += dist * BoidSpawner.S.collisionAvoidanceAmt;

79 }

80

81 // Mouse Attraction - Move toward the mouse no matter how far away

82 dist = BoidSpawner.S.mousePos - this.transform.position;

83 if (dist.magnitude > BoidSpawner.S.mouseAvoidanceDist) {

84 newVelocity += dist * BoidSpawner.S.mouseAttractionAmt;

85 } else {

86 // If the mouse is too close, move away quickly!

87 newVelocity -= dist.normalized*BoidSpawner.S.mouseAvoidanceDist*BoidSpawner.S.mouseAvoidanceAmt;

88 }

89

90 // newVelocity & newPosition are ready, but wait until LateUpdate()

91 // to set them so that this Boid doesn't move before others have

92 // had a chance to calculate their new values.

93 }

94

95 // By allowing all Boids to Update() themselves before any Boids

96 // move, we avoid race conditions that could be caused by some Boids

97 // moving before others have decided where to go.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.