Create with Code - Unity Learn
主要记录一些在跟做此教程之前不熟悉的内容。
Unit 1 Player Control
完成效果:

prototype1.gif

challenge1.gif
基础操作:
1.按住右键+WASDQE可以在视图中游走
2.Alt+左键旋转视角
3.下图左边六个键分别对应于QWERTY:

image.png
关于LateUpadate:LateUpdate是在所有Update函数调用后被调用。这可用于调整脚本执行顺序。例如:当物体在Update里移动时,跟随物体的相机可以在LateUpdate里实现。
使用Unity自带的Input Manager接收输入:
例如如图水平方向移动,键盘左键为负值,右键为正值。

image.png
程序中接收数值:
horizontalInput = Input.GetAxis("Horizontal");
Unit 2 Basic Gameplay
完成效果:

prototype2.gif

challenge2.gif
为模型添加脚本然后存为prefab,在游戏程序中随机Instantiate。当对象走出游戏范围时Destroy();
间隔固定时间执行某操作:一次性使用InvokeRepeating
InvokeRepeating("methodName", startDelay, timeInterval);
间隔可变时间执行某操作:递归使用Invoke
    void Start()
    {
        Invoke("SpawnRandomBall", startDelay);
    }
    // Spawn random ball at random x position at top of play area
    void SpawnRandomBall ()
    {
        // Generate random ball index and random spawn position
        int index = Random.Range(0, ballPrefabs.Length);
        Vector3 spawnPos = new Vector3(Random.Range(spawnLimitXLeft, spawnLimitXRight), spawnPosY, 0);
        // instantiate ball at random spawn location
        Instantiate(ballPrefabs[index], spawnPos, ballPrefabs[index].transform.rotation);
        Invoke("SpawnRandomBall", Random.Range(3f,5f));
    }
其他方法:使用协程
 void Start()
    {
        StartCoroutine(RandomIntervalSpawner());
    }
    private IEnumerator RandomIntervalSpawner()
    {
        while (true)
        {
            float randomInterwalTime = Random.Range(1f, 3f);
            yield return new WaitForSeconds(randomInterwalTime);
            SpawnRandomBall();
        }
    }
    // Spawn random ball at random x position at top of play area
    void SpawnRandomBall ()
    {
        // Generate random ball index and random spawn position
        Vector3 spawnPos = new Vector3(Random.Range(spawnLimitXLeft, spawnLimitXRight), spawnPosY, 0);
        // instantiate ball at random spawn location
        int randomBallIndex = Random.Range(0, ballPrefabs.Length);
        Instantiate(ballPrefabs[randomBallIndex], spawnPos, ballPrefabs[randomBallIndex].transform.rotation);
    }
用export package的方法对项目进行备份。