找回密码
 立即注册
查看: 318|回复: 0

Unity官方教程-Create with Code学习笔记

[复制链接]
发表于 2022-3-16 21:18 | 显示全部楼层 |阅读模式
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);    }

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-9-22 16:52 , Processed in 0.090362 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表