Siki_Unity_1-2_Unity5.2入门课程_进入Unity开发的奇幻世界_Roll A Ball

1-2 Unity5.2入门课程 进入Unity开发的奇幻世界

任务1:Roll A Ball项目简介

Unity官网的tutorial入门项目

方向键控制小球在平台上滚动,碰撞方块得分,消掉所有方块后胜利

任务2:初始化游戏环境

1. New Project "Roll A Ball"

2. save Main.unity into "Scenes" folder

3. create a Plane and rename it as "Ground"; and then scale it twice (the default width is 10m)

4. change the color of Ground (material)

a. create a folder called Materials and create a Material called Ground inside the folder

b. change the component Albedo of the Ground material to what u like (here is blue)

c. drag and drop the material to the GameObject(Ground)

5. create a Sphere and change the y position to 0.5 and rename it as Player

任务3:刚体介绍和脚本的创建

1. 刚体的添加

add component Rigidbody to Player

2. 控制小球的移动

a. create a folder called Scripts and create a c# script called Movement.cs inside it

// or add component in Player's inspector->New Scirpt

b. attach Movement.cs to Player

任务4:通过代码控制移动

通过Rigidbody来控制Player的移动

private Rigidbody rd;

// 在Start(){}中
rd = GetComponent<Rigidbody>();

// 在Update(){}中
// 给Player一个外力的作用,力的大小为1,方向为(1, 0, 0)
rd.AddForce(new Vector3(1, 0, 0));

此时,Player会自动向x轴正方向移动

任务5:通过键盘按键控制小球的移动

在Rigidbody的AddForce()中使用参数表示速度方向

// 水平方向控制,返回一个-1~1的浮点数(右方向为正)
float h = Input.GetAxis("Horizontal");
// 给Player一个外力的作用,力的大小为1,方向为(1, 0, 0)
rd.AddForce(new Vector3(h, 0, 0) * force);

float v = Input.GetAxis("Vertical");
rd.AddForce(new Vector3(0, 0, v) * force);
        
// 这里是通过施加外力的方法来控制小球的移动

speed设为5

可简写成

rd.AddForce(new Vector3(h, 0, v) * force);

任务6:控制相机的跟随

1. 将Camera调成俯视状态

2. 将Camera设置成Player的child

但是发现Camera会在小球移动的时候跟随小球旋转

3. 步骤2不合适,故尝试使用代码来控制

在Main Camera中添加脚本FollowTarget.cs

public Transform playerTransform;  // 需要得到Player的位置,故使用Transform

将Player GameObject拖到Main Camera的Inspector中的变量出赋值

计算出想要的位置平移,在Update中实现即可

private Vector3 offset;

// 在Start(){}中
offset = transform.position - playerTransform.position;

// 在Update(){}中
transform.position = playerTransform.position + offset;

任务7:控制小球的移动范围

在Ground的四周添加墙

create a cube called Wall,scale z to 20 and set the position

ctrl + d to copy another three walls, and put them in the other side

将四堵墙设置为Ground的children,做好分类

任务8:创建可收集的食物

1. 创建cube,命名为PickUp,scale为0.5,rotation为(45, 45, 45)

2. 修改cube的颜色,新建一个名为PickUp的Material,将Albedo设置为黄色,并给cube赋值

3. 将PickUp制作成Prefab

创建文件夹Prefabs,将PickUp从Hierarchy拖入Project面板即可

4. 将PickUp复制并放置在Ground上围成一圈

5. 创建一个Empty GameObject,将所有PickUp放入成为children

任务9:控制食物的旋转

创建脚本PickUpRotating

transform.Rotate(new Vector3(0, 1, 0));

// 围绕某个轴进行旋转

任务10:关于Unity手册(API和Manual)

Help->Scripting Reference

任务11:碰撞检测

碰撞检测:刚体Rigidbody检测两个物体是否进行了碰撞

如Player在接触了Wall或是Ground的时候就成为碰撞,能够检测出碰撞到什么物体

OnCollisionEnter() -- when this collider/rigidbody has begun touching another 开始碰撞的时候,只会执行一次

OnCollisionExit() -- when this collider/rigidbody has stopped touching another 结束碰撞的时候,只会执行一次

OnCollisionStay() -- once per frame for every collider/rigidbody that is touching another两个物体碰撞的时候,会一直执行

在Player的Movement.cs中

给PickUp的Prefab加上tag“PickUp”

void OnCollisionEnter(Collision collision) {
    // collision为与该物体碰撞的物体
    // collision.collider为collider组件
    // collision.collider.name为物体名字
    // collision.collider.tag为物体标签
    // collision.collider.gameObject为物体本身
    if (collision.collider.tag == "PickUp") {
        Destroy(collision.collider.gameObject);
    }
}

会发现Player在碰到食物时会停顿一下,食物消失 -- 不大符合要求

(PS另一种为触发检测,见下一个任务)

任务12:触发检测控制主角捡起食物

触发检测:

触发器没有物理的碰撞效果,一个物体可以进入触发器内部,但是可以检测到是否进入了触发区域,比如在主角进入某个区域时有个机关会自动打开,就可以使用触发检测来完成

也是包含OnTriggerEnter(); OnTriggerExit(); OnTriggerStay();

将PickUp的Box Collider中的Is Trigger勾选

// 触发检测
void OnTriggerEnter(Collider collider) {
    // collider表示跟该物体触发的碰撞器
    if (collider.tag == "PickUp") {
        Destroy(collider.gameObject);
    }
}

任务13:显示分数和胜利检测

显示分数

1. private int score = 0;

2. create UI->Text

在Text的Rect Transform中按住alt选择top-left,将Text定位在屏幕左上角

3. Movement.cs

public Text scoreText;
private int score = 0;

// 在触发检测中添加
if (collider.tag == "PickUp") {
    Destroy(collider.gameObject);
    score++;
    scoreText.text = score.ToString();
}

胜利检测

1. create UI->Text

按T切换到UI编辑

按住alt等比例放大拖动并在Inspector里选择左右居中,上下居中

改变颜色

修改内容为You Win!

2. 将该WinText的勾选框uncheck,表示不激活

3. Movement.cs

public GameObject winText;

当想要控制GameObject本身的时候就声明一个GameObject

当想要控制一个物体里面的组件时,只需声明相关组件即可,如上面的Text

gameObject.SetActive(true);

if (collider.tag == "PickUp") {
    score++;
    scoreText.text = score.ToString();
    if(score == 11) {
        winText.SetActive(true);
    }
    Destroy(collider.gameObject);
}

任务14:游戏发布和运行

在File -> Build Settings中,将要发布的scenes拖到Scenes In Build中(有顺序),点击Build/Build And Run即可

若是选择Window平台,生成的会是一个exe文件和一个存放data的文件夹,这两部分缺一不可,而且需要位于同一目录下才能运行。

转载于:https://www.cnblogs.com/FudgeBear/p/8011363.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值