Unity 油门控制器.docxVIP

  • 9
  • 0
  • 约2.41千字
  • 约 4页
  • 2017-12-14 发布于河南
  • 举报
Unity 油门控制器

还是一篇41post的翻译,对于老鸟们可能就没用了,这个脚本太简单了,但是小小鸟们可以看看,或许有点用能,希望大家喜欢!!!????下面正文:????这个Unity编程教程将展示如何创建一个让GameObject保持预订速率向前移动的脚本。这个油门速率控制器可以应用在像船,飞机,火车上。照例,Unity工程文件会在文章的结尾提供下载。????本教程的灵感来自于半条命,有一辆玩家可以驾驶的列车,你们可能已经不怎么记得了,来看看这张截图:下面这段代码也可以做到如此。来看看代码:usingUnityEngine;??usingSystem.Collections;??publicclassThrottleController : MonoBehaviour??{?????? //The game objects Transform????privateTransform goTransform;??????//the throttle increment to the current velocity????privatefloatincrement=0.0f;??????//this variable stores the vertical axis values????privatefloatvertAxis=0.0f;??????//the throttle????privatefloatthrottle =0.0f;??????voidAwake ()??????{??????????//get this game objects Transform????????goTransform =?this.GetComponentTransform();??????}??????voidUpdate ()??????{??????????//Get the vertical input value and store it at the vertAxis variable????????vertAxis = Input.GetAxis(Vertical);??????????//change the increment value based on the vertical input????????if(vertAxis0)??????????{??????????????increment = 0.05f;??????????}??????????else?if(vertAxis0)??????????{??????????????increment = -0.05f;??????????}??????????//after releasing the vertical axis, add the increment the throttle????????if(Input.GetButtonUp(Vertical))??????????{??????????????throttle = throttle+increment;??????????}??????????//set the throttle limit between -0.05f (reverse) and 0.25f (max speed)????????throttle=Mathf.Clamp(throttle, -0.05f,0.25f);??????????//translates the game object based on the throttle????????goTransform.Translate(throttle * Vector3.forward);??????????//rotates the game object, based on horizontal input????????goTransform.Rotate(Vector3.up * Input.GetAxis(Horizontal));??????}??}??????实质上,这个脚本是检查垂直轴的按钮是否被按下和释放。然后,将增量加上垂直输入的值加上添加的油门变量设置为使GameObject移动的速率。这是核心内容。????首先要声明goTransform为Transform变量(例如第7行),之后在代码中存储GameObject的Transform将被用到。下面,我们有3个浮点数被声明并初始化:???? increment?,改变油门的值。???? vertAxis,存储键盘输入结果,在1到-1之间???? throttle,GameObject的移动速率(例如第10,12,14行)????然后,在Awake()方法中有一行初始化goTransform的代码(例如

文档评论(0)

1亿VIP精品文档

相关文档