找回密码
 立即注册
查看: 3996|回复: 62

[脚本] 让GameObject保持预订速率向前移动的脚本

[复制链接]
发表于 2012-12-14 23:55 | 显示全部楼层 |阅读模式
还是一篇41post的翻译,对于老鸟们可能就没用了,这个脚本太简单了,但是小小鸟们可以看看,或许有点用能,希望大家喜欢!!!
    下面正文:
这个Unity编程教程将展示如何创建一个让GameObject保持预订速率向前移动的脚本。这个油门速率控制器可以应用在像船,飞机,火车上。照例,Unity工程文件会在文章的结尾提供下载。
本教程的灵感来自于半条命,有一辆玩家可以驾驶的列车,你们可能已经不怎么记得了,来看看这张截图:



下面这段代码也可以做到如此。来看看代码:
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class ThrottleController : MonoBehaviour  
  5. {  
  6. //The game object's Transform  
  7. private Transform goTransform;  
  8. //the throttle increment to the current velocity  
  9. private float increment=0.0f;  
  10. //this variable stores the vertical axis values  
  11. private float vertAxis=0.0f;  
  12. //the throttle  
  13. private float throttle =0.0f;  
  14.   
  15. void Awake ()  
  16. {  
  17. //get this game object's Transform  
  18. goTransform = this.GetComponent<Transform>();  
  19. }  

  20. void Update ()  
  21. {  
  22. //Get the vertical input value and store it at the vertAxis variable  
  23. vertAxis = Input.GetAxis("Vertical");  
  24.   
  25. //change the 'increment' value based on the vertical input  
  26. if(vertAxis>0)  
  27. {  
  28. increment = 0.05f;  
  29. }  
  30. else if(vertAxis<0)  
  31. {  
  32. increment = -0.05f;  
  33. }  
  34.   
  35. //after releasing the vertical axis, add the increment the throttle  
  36. if(Input.GetButtonUp("Vertical"))  
  37. {  
  38. throttle = throttle+increment;  
  39. }  
  40.   
  41. //set the throttle limit between -0.05f (reverse) and 0.25f (max speed)  
  42. throttle=Mathf.Clamp(throttle, -0.05f,0.25f);  
  43.   
  44. //translates the game object based on the throttle  
  45. goTransform.Translate(throttle * Vector3.forward);  
  46.   
  47. //rotates the game object, based on horizontal input  
  48. goTransform.Rotate(Vector3.up * Input.GetAxis("Horizontal"));  
  49. }  
  50. }  
复制代码
实质上,这个脚本是检查垂直轴的按钮是否被按下和释放。然后,将增量加上垂直输入的值加上添加的油门变量设置为使GameObject移动的速率。这是核心内容。
首先要声明goTransform为Transform变量(例如第7行),之后在代码中存储GameObject的Transform将被用到。下面,我们有3个浮点数被声明并初始化:
     increment ,改变油门的值。
     vertAxis,存储键盘输入结果,在1到-1之间
     throttle,GameObject的移动速率(例如第10,12,14行)
然后,在Awake()方法中有一行初始化goTransform的代码(例如19行)。剩下的都在Update()方法中运行。
verAxis变量就是垂直轴输入的值(例如25行)。使用if-else语句来判断verAxis是正还是负,如果为正,increment就被设置为0.05f,如果为负,increment就等于-0.05f(例如第28到35行)。
最后一个if语句在垂直输入按钮被释放时,把increment值加到throttle变量。(例如第38到41行)。使用第44行代码,玩家可以更好的控制油门,不会直接到最大值或最小值。因为throttle是以0.05为基数改变的,在-0.05到0.25f的范围内都是可以的,并且有5个不同速度可以控制GameObject向前移动。
就是这些了,别忘了评论啊!!!

Unity工程下载:
http://www.41post.com/uploads/files/throttlecontroller.zip
英文源地址 :
http://www.41post.com/4073/programming/unity-throttle-controller

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
发表于 2012-12-15 20:43 | 显示全部楼层
嗯 不错哦 么么哒
发表于 2013-1-31 09:48 | 显示全部楼层
老鸟路过 。。。。。
发表于 2017-2-9 10:31 | 显示全部楼层
真心顶
发表于 2017-2-9 10:45 | 显示全部楼层
难得一见的好帖
发表于 2017-2-9 11:19 | 显示全部楼层
说的非常好
发表于 2017-2-9 10:30 | 显示全部楼层
很好哦
发表于 2017-2-9 10:27 | 显示全部楼层
LZ真是人才
发表于 2017-3-20 15:58 | 显示全部楼层
很不错
发表于 2017-3-20 15:24 | 显示全部楼层
好帖就是要顶
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-10 05:08 , Processed in 0.133173 second(s), 27 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表