标题1:前言
主要是由于即将大学毕业,但是感觉什么都没学,又苦于找工作,所以想要尝试一下用之前学的u3D知识,编写一些简单的一个小恐龙游戏。很久之前根据书籍《游戏设计,原型与开发 基于untiy与C#从构思到实现》----jeremy Gibson Bond 实现过一些简单的实例,例如接苹果,弹弓(类似愤怒的小鸟),太空射击以及地牢游戏等。但是由于大学有时候比较忙,感觉有点生疏之类的,所以趁着暑假,写一些东西练习一下。
首先是谷歌小恐龙这个游戏,是谷歌浏览器在用户断网的情况下,自带的一款小游戏,基本内容就是一直恐龙在地面上跳动,然后躲避迎面而来的仙人掌和翼龙。游戏比较简单易懂,用来练习耶很不错。
但是直接实现这个游戏也有点过于简单,因此想要在这个游戏的基础上增加一些自己的内容,丰富一下,顺便也强化一下代码设计能力。
谷歌小恐龙游戏:谷歌浏览器内输入chrome://dino
基础设计
地板设计:
首先是设计地面移动的效果,一般来说这样的游戏基本是使用相对运动的模式,因此只需要地面向前移动就可以看起来像是小恐龙在前面跑动一样。为了连接的连贯性准备两个地面对象,利用卷轴进行交替移动。
上面这个是地面的对象实体
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class groundMove : MonoBehaviour
{
[Header("Set in Inspector")]
public float speed = 5f;
[Header("Set Dynamically")]
private Rigidbody rid;
// Start is called before the first frame update
void Start()
{
rid = GetComponent<Rigidbody>();//初始化刚体
}// Update is called once per frame
void Update()
{
Vector3 vel=Vector3.zero;//普通移动
vel = Vector3.left;
rid.velocity = vel*speed;
Vector3 pos=gameObject.transform.position;
if (pos.x <= 10.8f)
{
pos.x = 39.2f;
gameObject.transform.position = pos;
}
}
}
以上是地面移动代码,根据摄像机的坐标以及大小进行交替移动 ,一共两个地面对象。
仙人掌设计:
仙人掌的设计也是一样,因为恐龙并不是真正的移动,因此仙人掌的逻辑跟地面差不多,可以直接给他赋予一个向左的速度,超出屏幕就消失,然后再拿多几个仙人掌的对象



以上为使用的三个个仙人掌图像
这三个实体随机出现,仙人掌的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class CactusMove : MonoBehaviour
{
[Header("Set inInspector")]
public float speed = 5f;
public Vector3 summonPos;//表示生成坐标
[Header("Set Dynamically")]
private Rigidbody rig;
// Start is called before the first frame update
void Start()
{
rig = GetComponent<Rigidbody>();//初始化刚体
summonPos = new Vector3(39, 3.8f, 0);//生成坐标初始化
gameObject.transform.position = summonPos;
}// Update is called once per frame
void Update()
{
Vector3 pos =gameObject.transform.position;
Vector3 vel = Vector3.left;
rig.velocity = vel * speed;
if (pos.x <= 18)


5722

被折叠的 条评论
为什么被折叠?



