博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Unity3D-Roll-a-ball(1)
阅读量:2066 次
发布时间:2019-04-29

本文共 2751 字,大约阅读时间需要 9 分钟。

学习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.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.
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上滚动了。

转载地址:http://udfmf.baihongyu.com/

你可能感兴趣的文章
在Java中如何高效判断数组中是否包含某个元素
查看>>
设计模式总结
查看>>
什么时候可以使用Ehcache缓存
查看>>
Java核心知识点-JVM结构和工作方式
查看>>
Java编程中“为了性能”一些尽量做到的地方
查看>>
Java并发编程:线程池的使用
查看>>
redis单机及其集群的搭建
查看>>
Java多线程学习
查看>>
检查Linux服务器性能
查看>>
Java 8新的时间日期库
查看>>
Chrome开发者工具
查看>>
Java工程师成神之路
查看>>
如何在 Linux 上自动设置 JAVA_HOME 环境变量
查看>>
MSSQL复习笔记
查看>>
Spring基础知识汇总
查看>>
Chrome扩展插件
查看>>
log4j.xml 日志文件配置
查看>>
如何删除MySql服务
查看>>
BAT Java和Rti环境变量设置
查看>>
NodeJs npm install 国内镜像
查看>>