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
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.
Deep Learning with Python by François Chollet(12644)
Hello! Python by Anthony Briggs(9947)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9822)
The Mikado Method by Ola Ellnestam Daniel Brolund(9813)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(9688)
Dependency Injection in .NET by Mark Seemann(9368)
Hit Refresh by Satya Nadella(8854)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8333)
The Kubernetes Operator Framework Book by Michael Dame(7925)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7810)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7788)
Grails in Action by Glen Smith Peter Ledbrook(7719)
Exploring Deepfakes by Bryan Lyon and Matt Tora(7712)
Practical Computer Architecture with Python and ARM by Alan Clements(7658)
Implementing Enterprise Observability for Success by Manisha Agrawal and Karun Krishnannair(7621)
Robo-Advisor with Python by Aki Ranin(7612)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7590)
Building Low Latency Applications with C++ by Sourav Ghosh(7494)
Svelte with Test-Driven Development by Daniel Irvine(7479)
