Learning C# Programming with Unity 3D by Alex Okita

Learning C# Programming with Unity 3D by Alex Okita

Author:Alex Okita [Okita, Alex]
Language: eng
Format: epub
Publisher: CRC Press
Published: 2014-07-31T21:00:00+00:00


public float Speed = 0.1f;

//Update is called once per frame

void Update ()

{

gameObject.transform.position += Movement(Speed);

}

Vector3 Movement(float dist)

{

Vector3 vec = Vector3.zero;

if

(Input.GetKey(KeyCode.A))

{

vec.x

-=

dist;

}

if

(Input.GetKey(KeyCode.D))

{

vec.x

+=

dist;

}

if

(Input.GetKey(KeyCode.W))

{

vec.z

+=

dist;

}

if

(Input.GetKey(KeyCode.S))

306

Learning C# Programming with Unity 3D

{

vec.z

-=

dist;

}

return

vec;

}

}

Therefore, here’s the Player.cs I’m using for this chapter; we’re going to use this to move the little box around in the scene. We’ll pretend that the box is a pretty cool-looking monster hunter armed with a sword, shotgun, or double-barreled shotgun.

We may want to rename the Example.cs to something like MonsterSpawner.cs or

MonsterGenerator.cs, but I’ll leave that up to you. Just remember to rename the class declaration

to match the file name. This will spill out monsters to chase the player around. Then the Monster.cs attached to each of the objects that the MonsterSpawner creates will then seek out the player and chase him around. At least that’s the plan.

NOT E: Prototyping game play using primitive shapes is a regular part of game development. This is somewhat related to what has become to be known as “programmer art.” Changes to code often mean

changes to art. It’s difficult to build a game keeping art and code parallel. Often a simple change in code means days of changes to art. It’s quite often easier to prototype a game making no requests of an artist.

Most of the time, the programmer doesn’t even know himself what to ask of an artist.

6.6.1 Using Enums

The keyword “enum” is short for enumeration. A programmer might say that an enum is a list of named constants. The meaning may seem obtuse. Translated, an enumeration is a list of words that you pick.

For our Monster to have a better set of functions for its behavior, we’re going to create a new enum type called MonsterState.

We’ve already interacted with the PrimitiveType enum.

public enum PrimitiveType

{

Sphere,

Capsule,

Cylinder,

Cube,

Plane

}

We’ve also seen that enums can store many different names. For instance, the InputType enum had

a different enumeration for every key on the keyboard and each input for your mouse or trackpad and controller. Enumerating through a long list of “things” is to help name every possibility with something more useful than a simple numeric index. It’s important to remember that enumerations don’t need to follow any particular pattern. It’s up to you to decide on any organization to keep your enums organized.

In the MoreLogic project, open the Example.cs found in the Assets list in the Project panel.



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.