湖北公司网站建设多少钱,区块链 做网站,汝阳网站开发,求购信息平台游戏效果
游戏中#xff1a; 游戏中止#xff1a; 一、制作参考
如何制作游戏#xff1f;【15分钟】教会你制作Unity小恐龙游戏#xff01;新手15分钟内马上学会#xff01;_ unity教学 _ 制作游戏 _ 游戏开发_哔哩哔哩_bilibili
二、图片资源
https://download.csdn.…游戏效果
游戏中 游戏中止 一、制作参考
如何制作游戏【15分钟】教会你制作Unity小恐龙游戏新手15分钟内马上学会_ unity教学 _ 制作游戏 _ 游戏开发_哔哩哔哩_bilibili
二、图片资源
https://download.csdn.net/download/benben044/89522911?spm1001.2014.3001.5501 三、创建场景
1、将资源都拖到Assets目录下 2、调整编辑布局
将Scene、Hierarchy、Game这3个模块分开方便可以同时观察到三个模块的信息。
同时将Main Camera的color设置为白色方便看到ground的颜色。 3、放dinosaur到scene
将assets中的run-2放到Scene中就可以在Game中看到dinosaur的图案同时将恐龙rename为Dinosaur。 4、添加cloud到scene
同时给cloud进行重命名。 5、给dinosaur和ground添加组件
1给dinosaur添加Rigidbody 2d组件使其具有物理属性设置Gravity Scale为2重力大一点下降速度会更快一点。
2再给dinosaur添加Box Collider 2d组件使其具有碰撞属性
3接着给ground添加Box Collider 2d组件使其具有碰撞属性
通过以上操作dinosaur因为有物理属性所以会自然掉落但是因为dinosaur和ground都有碰撞属性所以dinosaur掉到地面后就停止掉落了。
在测试中如果发现Dinosaur掉到地面后脚没有落地可以编辑Box Collider 2D中的Edit Collider调整碰撞的范围。 四、编辑C#脚本
1、创建恐龙脚本
public class Dinosaur : MonoBehaviour
{Rigidbody2D rb;bool isJumping;public float jump; // 当为public时即可在Inspector面板看到属性信息// Start is called before the first frame updatevoid Start(){rb GetComponentRigidbody2D();isJumping false;}// Update is called once per framevoid Update(){if (Input.GetKeyDown(KeyCode.Space) isJumping false) { rb.velocity new Vector2(0, jump); isJumping true;}}private void OnCollisionEnter2D(Collision2D collision){// 只有碰到底才能跳否则在天空中就飞起来了isJumping false; }
}
在Dinosaur的Dinosaur组件中设置jump为8。 2、创建恐龙动画
首先在Assets下创建Animations的目录。
在Window-Animation下创建动画组件然后点击Scene下的Dinosaur最后点击Create在Assets下的Animations下创建DinosaurAnimation.anim文件如下图所示 然后将dinosaur_assets下的run-1和run-2拖入animation编辑器第1个点是run-1第2个点是run-2第3个点是run-1循环的动作。 3、创建Movement脚本
在Assets下创建Movement的脚本该脚本的功能是当角色在run时天上的云、地面都在向左移动。即角色不动参照物动从而造成角色向右移动的假象。
public class Movement : MonoBehaviour
{public float movementSpeed;public float startPosition;public float endPosition;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){transform.position new Vector2(transform.position.x - movementSpeed * Time.deltaTime, transform.position.y);// 到dinosaur快要越界掉下去时从开始处重新开始runif (transform.position.x endPosition){transform.position new Vector2(startPosition, transform.position.y);}}
}
给ground所有的cloud都加载该脚本并且设置3个变量分别为55-15。
4、加入仙人掌
将仙人掌放到Scene中并且添加Box Collider 2D的脚本这样恐龙就不能穿透它了。同时加入Movement的脚本参数设置也是55-15。
5、创建GameManager
在Scene中Create Empty名为GameManager。
然后在Assets下创建GameManager的脚本并把该脚本赋予GameManager的物体上。
脚本内容如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class GameManager : MonoBehaviour
{public GameObject cactus; // 仙人掌public GameObject cactusSpawnPostition; //仙人掌出现的位置public float spawnTime; // 仙人掌出现的时间float timer; // 计时器// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){timer Time.deltaTime;if (timer spawnTime){Instantiate(cactus, cactusSpawnPostition.transform);timer 0;}}
}意思是当超过spawnTime时就让cactus在cactusSpawnPosition出现。 6、创建cactusSpawnPosition
这个就是上个脚本中仙人掌每次自动生成的位置。
修改该对象的tag为红色菱形这样在scene中更容易被辨认出来。
然后在scene中确定位置如下图所示 接着拖动hierachy中的cactus到Assets成为prefab预制体修改器transform中的的Position全是0。
然后设置GameManager的参数如下 7、给仙人掌添加tag
点击Asset中的cactus_single然后在其Tag旁边的Untagged点击一下选择Add Tag如下图 添加Cacuts的tag如下 在Tag栏位选择刚刚创建的Cactus如下 8、删除不用的仙人掌
如果仙人掌已经在movement中endPosition的左边就可以destory它。
所以如果越界后是仙人掌就destroy它否则类似ground、cloud就循环从头开始。
所以优化Movement脚本如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Movement : MonoBehaviour
{public float movementSpeed;public float startPosition;public float endPosition;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){transform.position new Vector2(transform.position.x - movementSpeed * Time.deltaTime, transform.position.y);// 到dinosaur快要越界掉下去时从开始处重新开始runif (transform.position.x endPosition){if(gameObject.tag Cactus){Destroy(gameObject);}else{transform.position new Vector2(startPosition, transform.position.y);}}}
}9、创建GameOver和Restart
1GameOver
在Hierarchy中选择UI - Legacy - Text如下 编写文字GAME OVER
并且选择好FFT格式的字体。
2Restart
在Cavas下再创建Button位于UI - Legacy - Button重命名为RestartButton。
然后将Button的图标替换为已有的素材在调整下size。 3创建Panel
将上面创建的2个UI组件都装到该Panel下同时调整Panel-Color-A值为0。 这样同时控制Panel的Active状态就可以控制Panel的出现和消失。
只有当触发GameOver时该Panel才会出现否则该Panel的Active为false。
10、 保存场景
在File-Save As中在Assets-Scenes下保存场景为lv1level1的意思。 11、在GameManager中加入GameOver和Restart
创建GameOverScene变量对应GameOver的PanelStart()中加入控制速度的Time.scaleTime1在多个函数中设置GameOver的Panel的Active为false在GameOver()中设置Time.scaleTime0同时出现GameOver的Panel在Restart()中重新加载lv1的scene同时GameOver的Panel的Active为false
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;public class GameManager : MonoBehaviour
{public GameObject cactus; // 仙人掌public GameObject cactusSpawnPostition; //仙人掌出现的位置public float spawnTime; // 仙人掌出现的时间float timer; // 计时器public GameObject GameOverScene;// Start is called before the first frame updatevoid Start(){Time.timeScale 1.0f;GameOverScene.SetActive(false);}// Update is called once per framevoid Update(){timer Time.deltaTime;if (timer spawnTime){Instantiate(cactus, cactusSpawnPostition.transform);timer 0;}}public void GameOver(){Time.timeScale 0; // 将游戏暂停GameOverScene.SetActive(true);}public void Restart(){SceneManager.LoadScene(lv1);GameOverScene.SetActive(false);}
}编写完代码后在Inspector中配置GameOverScene的值。 12、在Dinosaur脚本中加入游戏暂停的触发条件
主要是在OnCollisionEnter2D中加入触发条件。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Dinosaur : MonoBehaviour
{Rigidbody2D rb;bool isJumping;public float jump; // 当为public时即可在Inspector面板看到属性信息public GameManager gm;// Start is called before the first frame updatevoid Start(){rb GetComponentRigidbody2D();isJumping false;}// Update is called once per framevoid Update(){if (Input.GetKeyDown(KeyCode.Space) isJumping false) { rb.velocity new Vector2(0, jump); isJumping true;}}private void OnCollisionEnter2D(Collision2D collision){// 只有碰到底才能跳否则在天空中就飞起来了isJumping false; if(collision.gameObject.tag Cactus){// 撞到仙人掌游戏暂停gm.GameOver();}}
}编写完代码后配置GameManager的变量。 至此该游戏制作完成
五、总结
1、检测碰撞OnCollisionEnter2D这个是和Update平级的函数
2、物体移动时transform.position new Vector2(x, y)其中x是transform.position.x - Time.deltaTime * speed.
3、时间是float类型的数据
4、实例化预设体用Instantiate函数参数1是实例化的对象参数2是transform信息可以用某个物体的transform信息作为参数2
5、检测碰撞的物体时可以给对方物体一个tag然后通过collision.gameobject.tag tag判断和该物体是否相撞
6、加载场景使用SceneManager.LoadScene(场景名称)
7、如果有多个UI需要一起控制则统一放到Panel中然后控制该Panel即可
8、控制暂停、重启可使用时间变量Time.timeScale
9、脚本被挂载的当前物体为transform.gameObject