[U3D Learning Note] Unity C# Survival Guide (4)(5) -- if Statement & switch Statement

(白痴教程+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]);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值