(白痴教程+1
Challenge: Master if Statements
KEY:
在update()中,每帧更新一次,也就是说一秒钟更新60词,如果我们想要执行某个动作后输出一个信息,以下面代码为例,会持续输出这条信息
public class GetPoints : MonoBehaviour
{
// variables store points
[SerializeField] private int _points = 0;
// Start is called before the first frame update
void Start()
{}
// Update is called once per frame
void Update()
{
// evreytime we hit the space key
// add 10 points
if(Input.GetKeyDown(KeyCode.Space)){
_points += 10;
// Debug.Log(_points);
}
// if point is greater or euqal to 50 A
// print out "you are awesome!
if(_points >= 50){
Debug.Log("you are awesome!");
}
}
}
这个结果肯定不是我们希望看到的 我们只希望输出一次就够了 这时候我们可以加一个bool变量进行判断 来限制
// check out wheather said message
[SerializeField] private bool _hasSayMessage;
void Update()
{
// evreytime we hit the space key
// add 10 points
if(Input.GetKeyDown(KeyCode.Space)){
_points += 10;
// Debug.Log(_points);
}
// if point is greater or euqal to 50 AND hasn't said the message;
// print out "you are awesome!
if((_points >= 50) && (_hasSayMessage == false)){
Debug.Log("you are awesome!");
_hasSayMessage = true;
}
}
Challenge: Master if Statements part 4
KEY: Change the color of game object (reference link)
这节要求分数达到一定值时改变物体颜色
不难 写个判断就好 重点在于在哪里改变物体颜色
我们知道颜色在Material中 观察一下面板
我们在unity界面更改颜色时在这个地方
所以大概清楚路径在GameObject -> MeshRenderer ->Materials ->Color
因为 MeshRenderer是一个组件 所以我们要用GetComponent<Renderer>()
然后一点一点往下获取
public class ChangeColor : MonoBehaviour
{
/// Problem
// create a progrm that when you hit the space key , you increment a score value
// when score is greater than 50, turn the cube green
// at the start of game, cube is red
///
[SerializeField] private int _score = 0;
private Color _initialColor = Color.green;
private Color _changeColor = Color.red;
// Start is called before the first frame update
void Start()
{
this.GetComponent<Renderer>().material.color = _initialColor;
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.Space)){
_score += 10;
}
if(_score > 50) {
this.GetComponent<Renderer>().material.color = _changeColor;
}
}
}
效果如下(注意看面板的变化):
Speed Program
(和视频里有点不一样但都一个意思 emmmm其实那个warning可以每帧都有 这里就只显示一次
public class SpeedProgram : MonoBehaviour
{
/// Problem
// increment the speed when you hit the S key
// A key decrements the speed
// when the speed is greater than 20 you need to print out "slow down"
// when the spedd is 0 you need to print out "speed up"
// you can't go below 0
///
[SerializeField] private int _speed = 10;
[SerializeField] private int _incre = 5;
[SerializeField] private int _decre = 3;
private bool _warning;
// Start is called before the first frame update
void Start()
{}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.S)){
_speed += _incre;
} else if(Input.GetKeyDown(KeyCode.A) && (_speed >= 0)){
if(_speed >= _decre){
_speed -= _decre;
} else {
_speed = 0;
}
}
if(_speed > 20 && _warning == false){
Debug.Log("Slow Down!");
_warning = true;
} else if(_speed <=20 && _speed > 0){
_warning = false;
} else if(_speed == 0 && _warning == false){
Debug.Log("Speed up!");
_warning = true;
}
}
}
Switch Statement
switch 部分没啥好讲的 主要还是注意 什么时候用if-else 什么时候用switch if-else太多层的话最好转成switch
(有个地方要注意 每个case结束都要加个break
; 一直容易忘
然后有一个challenge是武器切换的 就 嫌他用switch太麻烦 用数组解决(散列表也ok 查找更快(为什么这种我也要记下来 (就当作水博客好了(x
public class WeaponSelect : MonoBehaviour
{
public int weaponID;
// ID 1 = Gun
// ID 2 = Knife
// ID 3 = machineGun
[SerializeField] private int _id=0;
private string[] weaponList = {"Gun", "Knife", "Machine Gun"};
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.RightArrow) && (_id < weaponList.Length-1)){
_id += 1;
}
if(Input.GetKeyDown(KeyCode.LeftArrow) && (_id > 0)){
_id -= 1;
}
Debug.Log(weaponList[_id]);
}
}