Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Procedural Content Generation for Unity Game Development

You're reading from   Procedural Content Generation for Unity Game Development Harness the power of procedural content generation to design unique games with Unity

Arrow left icon
Product type Paperback
Published in Jan 2016
Publisher
ISBN-13 9781785287473
Length 260 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Ryan Watkins Ryan Watkins
Author Profile Icon Ryan Watkins
Ryan Watkins
Arrow right icon
View More author details
Toc

Adding a spawn point


Now that we have our Weapon prefab put together, we are going to need a way to get it to the player. We've already answered this problem, with the chest, in the previous chapter. We will just reuse the Chest object to also spawn weapons as well.

Open the Chest.cs script that's to be edited. We will want to add a variable to store a reference to our generated weapon. Add the public Weapon weapon; variable right under our randomItem variable. Then, we need to make some adjustments to our Open() function, which can be seen in Code Snip 6.3:

1 public void Open () {
2  spriteRenderer.sprite = openSprite;
3
4  GameObject toInstantiate;
5
6  if (Random.Range (0, 2) == 1) {
7    randomItem.RandomItemInit ();
8    toInstantiate = randomItem.gameObject;
9  } else {
10    toInstantiate = weapon.gameObject;
11  }
12  GameObject instance = Instantiate (toInstantiate, new Vector3 (transform.position.x, transform.position.y, 0f), Quaternion.identity) as GameObject;
13  instance.transform...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime