Unity3D-Roll-a-ball(1)

学习Unity3D基础。按照教程一步一步实现一个滚球的简单游戏。视频传送门

Setting up the gam

创建地板

GameObject->3D Object->Plane
Rename: Ground
Transform中
Scale:x:2,y:1,z:2
Position: x:0,y:0,z:0

创建player

GameObject->3D Object ->Sphere
Rename:Player
Position: x:0,y:0.5,z:0
scale: x:1,y:1,z:1

创建环境光

GameObject->light->Directional Light

主灯光:
rename: Main Light
Position: 0,0,0
Rotation: 30,60,0
Scale: 1,1,1
Resolution: Very High Resolution(非常高分辨率)

补光灯:
rename: Fill Light
Position: 0,0,0
Rotation: -30,-60,0(反向光源)
Scale: 1,1,1
Color:RGB(35,187,218)(淡蓝色)
Instensity: 0.1(强度柔和)
Shadow Type: No Shadows(关闭光源投影影子)
用于反向的灯光,也使用了不同的颜色,可以看到没有受到主光照射的背面呈现淡蓝色。

GameObject->Create Empty
Rename:Light
将两个光源都拖进去。

Moving the player

点击Player对象,在Inspector中->Add Component->Physics->Rigidbody添加刚体
添加一个player的控制脚本:
Assets->Create->C# script
或者是在Project页签下点create->C# script
名字为:
PlayerController.cs
Edite->Preferences->External Tools->External Script Editor可以修改自己本地的vs之类。
脚本内容为:


    void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical =  Input.GetAxis("Vertical");



        Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

        Rigidbody rigidbody = gameObject.GetComponent<Rigidbody> ();
        rigidbody.AddForce (movement *speed *Time.deltaTime);
    }

当前我这里使用的是Unity3D 5.0.1版本,代码中GetComponent接口有一些改变。
在编写代码的时,我们可以通过:
Help->Scripting Reference
来查看手册。这个函数FixedUpdate函数:
Description
This function is called every fixed framerate frame, if the MonoBehaviour is enabled.
FixedUpdate should be used instead of Update when dealing with Rigidbody. For example when adding a force to a rigidbody, you have to apply the force every fixed frame inside FixedUpdate instead of every frame inside Update.
参考1
参考2
Update函数是受限于fps,如同lol一样,有些机器级速度比较快能到200fps。我们这里是想做Player的控制。也就是说操作是和真实时间相关的,所以就写在FixedUpdate里面在文档中也提到了:比如说,当你为一个刚体施加一个外力的时候,你就需要在FixedUpdate中应用这个外力。

Input.GetAxis
在手册中提到的:
“Horizontal” and “Vertical” are mapped to joystick, A, W, S, D and the arrow keys.

Description

Returns the value of the virtual axis identified by axisName.

The value will be in the range -1…1 for keyboard and joystick input. If the axis is setup to be delta mouse movement, the mouse delta is multiplied by the axis sensitivity and the range is not -1…1. This is frame-rate independent; you do not need to be concerned about varying frame-rates when using this value.

通过这个我们可以获取游戏控制杆,AWSD,方向键输入水平(Horizontal)和垂直(Vertical)方向的值。
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
将水平方向的力写入到x,垂直方向写入z,形成一个向量。
Rigidbody rigidbody = gameObject.GetComponent ();
获得当前脚本绑定的gameObject的刚体实例。
rigidbody.AddForce (movement *speed *Time.deltaTime);
给当前刚体提供一个外力。

最后将之前我们创建的Player对象创建一个Script Component。并且将编写的PlayerController.cs与之关联。就能在游戏中开始通过方向键来控制这个Player对象在Plane上滚动了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值