This is for the Udemy Unity course. We do using as an import. For example, using UnityEngine. Update is called once per frame, and start is called before the first frame update, void Start() and void Update().
Let's create 4 statements with Debug.Log. Let's first make a game called Number Wizard Game Flow.
'. We think of a number between minimum and maximum and it takes the minimum and adds the maximum and divides by 2 to give a "guess". Then we ask the player whether higher or lower, and guess and repeat until we get the correct numbers. It's.a very simple little binary search mechanism. We're going to welcome the player, pick a number, and figure out the highest and lowest numbers that we can choose.
print("hi") and Debug.("hi") both do the same thing. We also need to attach our script to the game object, and in this instance, this will be the main camera.
We then click on "play" and select the corresponding console. Variables are like boxes. Each variable has a name and each variable contains Data. Initializing an integer is very similar to doing so in java:
int hitPoints = 20;
Float is the following:
float speed = 3.8f;
We can also create a boolean variable:
bool isAlive = true;
Finally, we can define type string:
string name = "Rick";
Writing in a string is very similar to writing the string concatenation in java:
Debug.Log("The Highest number you can pick is " + max); This joins the string with the variable.
Now, let's learn input functions in C#.
We want to do Up: "you pushed up" and down arrow "You pushed down" and enter "Correct".
We can look at the unity documentation as in this link:
https://docs.unity3d.com/ScriptReference/Input.html
We can subsequently look for syntax.
if (Input.GetKeyDown(KeyCode.UpArrow)) {
Debug.Log("Up Arrow Key was Pressed");
}
Start() runs at the initialization, update runs asynchronously.
Using if and else if is pretty much the same as Java as well.
if(expression) {
do something
} else if (expression) {
do something
} else {
do something
}
The challenge is to allow only one key element at a time.
C# is organized into methods, classes, and states. Update() and Start() are in different scopes in regards to C#.
How C# are organized and we can look at code and have statements with if and debug statements.
C# is denoted in both methods and functions. If it is displayed in the class it can then be used in all of the methods.
Here's the number wizard code so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NumberWizard : MonoBehaviour
{
// Start is called before the first frame update
int max = 1000;
int min = 1;
int guess = 500;
void Start()
{
Debug.Log("Welcome to the Number Wizard");
Debug.Log("Pick a number, don't tell me what it is: ");
Debug.Log("The Highest number you can pick is " + max + ".");
Debug.Log("The Lowest number you can pick is " + min + ".");
Debug.Log("Push Up = higher, Push Down = lower, Push Enter = Correct");
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
Debug.Log("Up Arrow Key was Pressed.");
min = guess;
Debug.Log(guess);
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
Debug.Log("Down Arrow Key was pressed.");
max = guess;
Debug.Log(guess);
}
else if (Input.GetKeyDown(KeyCode.Return)) {
Debug.Log("You hit enter");
}
}
}
Let's go to the next one. We go to the update method. Now we update the guess() method every time after updating the min and max methods. We can also assign a variable as itself + 1.
We can think of a function as a contract. Naming is important for functions because it's doing a thing, telling use what to do and the brackets down the end is where we put parameters. Void means that there's no return value, similar to Java. In Unity, the function Start() is always called a the beginning.
The general process is called refactoring. The reason to refactor is to make the code readable, since easy-to-read code is huge in programming. Classes can be defined as this:
public class NumberWizard: MonoBehaviour {}
Here's the final code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NumberWizard : MonoBehaviour
{
// Start is called before the first frame update
int max = 1000;
int min = 1;
int guess = 500;
void Start()
{
StartGame();
}
void StartGame() {
max = 1000;
min = 1;
guess = 500;
Debug.Log("你好");
Debug.Log("Welcome to the Number Wizard");
Debug.Log("Pick a number, don't tell me what it is: ");
Debug.Log("The Highest number you can pick is " + max + ".");
Debug.Log("The Lowest number you can pick is " + min + ".");
Debug.Log("Push Up = higher, Push Down = lower, Push Enter = Correct");
max = max + 1;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
Debug.Log("Up Arrow Key was Pressed.");
min = guess;
NextGuess();
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
Debug.Log("Down Arrow Key was pressed.");
max = guess;
NextGuess();
}
else if (Input.GetKeyDown(KeyCode.Return)) {
Debug.Log("You hit enter");
StartGame();
}
}
void NextGuess() {
guess = (min + max) / 2;
Debug.Log(guess);
}
}
Comments
Post a Comment