Unity版本:Unity 2018.2.14f1
原视频链接:https://unity3d.com/cn/learn/tutorials/s/space-shooter-tutorial

教程目录:
Unity游戏开发官方入门教程:飞机大战(一)——创建新项目、导入资源、设置场景
Unity游戏开发官方入门教程:飞机大战(二)——创建飞船对象
Unity游戏开发官方入门教程:飞机大战(三)——设置相机和光照
Unity游戏开发官方入门教程:飞机大战(四)——使用Quad加入背景
Unity游戏开发官方入门教程:飞机大战(五)——实现飞船控制脚本
Unity游戏开发官方入门教程:飞机大战(六)——创建子弹
Unity游戏开发官方入门教程:飞机大战(七)——发射子弹
Unity游戏开发官方入门教程:飞机大战(八)——创建销毁边界
Unity游戏开发官方入门教程:飞机大战(九)——创建和销毁敌人
Unity游戏开发官方入门教程:飞机大战(十)——敌人的爆炸和移动
Unity游戏开发官方入门教程:飞机大战(十一)——游戏控制

新建脚本控制飞船移动

  1. 在Assets中新建文件夹Script,用于存放脚本文件
  2. Player->Add component->New Script->命名为PlayerController
  3. 将PlayerController拖拽进Script
  4. 点击"Open"编辑该脚本
  5. 脚本内容如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class PlayerController : MonoBehaviour
{private Rigidbody rb;void Start(){rb = GetComponent<Rigidbody>();}void FixedUpdate(){float moveHorizontal = Input.GetAxis("Horizontal");float moveVertical = Input.GetAxis("Vertical");Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);rb.velocity = movement;}
}
  1. 保存脚本,运行游戏,现在可以用方向键或ASWD键控制飞船移动了。

加入速度控制

飞船的速度变量尚未设置,因此移动速度较为缓慢,现加入速度控制。

  1. 在脚本中增加一个public变量speed,作为velocity的系数,修改后的脚本如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class PlayerController : MonoBehaviour
{public float speed;private Rigidbody rb;void Start(){rb = GetComponent<Rigidbody>();}void FixedUpdate(){float moveHorizontal = Input.GetAxis("Horizontal");float moveVertical = Input.GetAxis("Vertical");Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);rb.velocity = movement * speed;}
}
  1. 在Inspector中可见public变量speed,将speed修改为10
  2. 保存并运行,可见速度变为原来的10倍:

加入边界控制

为了防止飞船飞出游戏边界,现加入边界控制。

  1. 增加一个边界类Boundary
  2. 在边界类中增加4个public变量xMin, xMax, zMin, zMax,用于控制边界的4个顶点
  3. 在开头加上[System.Serializable],使得新增的Boundary类能够显示在Inspector中。
    修改后的脚本如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;[System.Serializable]
public class Boundary
{public float xMin, xMax, zMin, zMax;
}public class PlayerController : MonoBehaviour
{public float speed;public Boundary boundary;private Rigidbody rb;void Start(){rb = GetComponent<Rigidbody>();}void FixedUpdate(){float moveHorizontal = Input.GetAxis("Horizontal");float moveVertical = Input.GetAxis("Vertical");Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);rb.velocity = movement * speed;rb.position = new Vector3(Mathf.Clamp(rb.position.x, boundary.xMin, boundary.xMax),0.0f,Mathf.Clamp(rb.position.z, boundary.zMin, boundary.zMax));}
}
  1. 拖拽飞船的position的x,可知x的范围大概是(-6,6)
  2. 同理可知,z的范围大概是(-4,8)
  3. 在Inspector中修改xMin, xMax, zMin, zMax:
  4. 运行游戏,成功实现边界控制:

加入飞船倾斜效果

左右移动飞船的效果太生硬,现加入倾斜效果。

  1. 新增public变量tilt,用于设置旋转幅度
  2. 使用Unity中的四元数类的欧拉角函数Quaternion.Euler(),让飞船绕着z轴旋转一定角度。
    修改后的脚本如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;[System.Serializable]
public class Boundary
{public float xMin, xMax, zMin, zMax;
}public class PlayerController : MonoBehaviour
{public float speed;public float tilt;public Boundary boundary;private Rigidbody rb;void Start(){rb = GetComponent<Rigidbody>();}void FixedUpdate(){float moveHorizontal = Input.GetAxis("Horizontal");float moveVertical = Input.GetAxis("Vertical");Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);rb.velocity = movement * speed;rb.position = new Vector3(Mathf.Clamp(rb.position.x, boundary.xMin, boundary.xMax),0.0f,Mathf.Clamp(rb.position.z, boundary.zMin, boundary.zMax));rb.rotation = Quaternion.Euler(0.0f, 0.0f, rb.velocity.x * -tilt);}
}
  1. 在Inspector中将tilt的值修改为4:
  2. 飞船倾斜效果如下:

参考资料:https://unity3d.com/cn/learn/tutorials/s/space-shooter-tutorial

Unity游戏开发官方入门教程:飞机大战(五)——实现飞船控制脚本相关推荐

  1. Unity游戏开发官方入门教程:飞机大战(六)——创建子弹

    Unity版本:Unity 2018.2.14f1 原视频链接:https://unity3d.com/cn/learn/tutorials/s/space-shooter-tutorial 教程目录 ...

  2. Unity游戏开发官方入门教程:飞机大战(二)——创建飞船对象

    Unity版本:Unity 2018.2.14f1 原视频链接:https://unity3d.com/cn/learn/tutorials/s/space-shooter-tutorial 教程目录 ...

  3. Unity游戏开发官方入门教程:飞机大战(十)——敌人的爆炸和移动

    Unity版本:Unity 2018.2.14f1 原视频链接:https://unity3d.com/cn/learn/tutorials/s/space-shooter-tutorial 教程目录 ...

  4. OUYA游戏开发快速入门教程

     OUYA游戏开发快速入门教程 试读地址:http://pan.baidu.com/s/1o63a3W2 本教程是国内唯一OUYA游戏开发教程.本教程基于Unity全面讲解OUYA游戏开发方式.内容包 ...

  5. C#游戏开发快速入门教程Unity5.5教程

    C#游戏开发快速入门教程Unity5.5教程 试读文档下载地址:http://pan.baidu.com/s/1slwBHoD C#是微软发布的高级程序设计语言,这门语言和C语言一样,已经成为了大学计 ...

  6. 游戏控制杆OUYA游戏开发快速入门教程

    游戏控制杆OUYA游戏开发快速入门教程 1.2.2  游戏控制杆 游戏控制杆各个角度的视图,如图1-4所示,它的硬件规格是本文选自OUYA游戏开发快速入门教程大学霸: 图1-4  游戏控制杆各个角度的 ...

  7. OUYA游戏开发快速入门教程1.2OUYA的硬件规格

    OUYA游戏开发快速入门教程1.2OUYA的硬件规格 从官网上购买回来的OUYA产品,包含游戏主机.游戏控制杆.说明书.电源线.HDMI线.电源线和电池,如图1-2所示.本节就来简要介绍下,游戏主机和 ...

  8. OUYA游戏开发快速入门教程第1章了解OUYA及其设备

    OUYA游戏开发快速入门教程第1章了解OUYA及其设备 OUYA是基于Andorid系统的游戏主机.围绕OUYA游戏机,已经形成一个完整的生态圈.在国外,OUYA已经成为知名的游戏平台.本章会站在玩家 ...

  9. 【教程汇总+持续更新】Unity游戏开发从入门到入坟

    新的一年,本该在年前整理的年终总结被拖到了年后开工.去年大量时间投入在Catlike教程的翻译上,截止目前位置,教程的进度已经完全追平原作者. 去年还有一部分是断断续续的更新SLG实战教程,但遗憾的是 ...

最新文章

  1. SO_SNDTIMEO和SO_RCVTIMEO
  2. RHEL5.4部署中央日志服务器之rsyslog+loganalyzer
  3. 什么是脱离文档流?什么是文档流?
  4. spark如何解决文件不存在_Spark Read.json无法找到文件
  5. u-boot.lds 文件分析
  6. 【016】VS2010连接内置SQL数据库
  7. 命名空间“System.Web”中不存在类型或命名空间名称“Optimization”解决方法
  8. freemarker 如何获得list的索引值
  9. quartus驱动无法识别分析
  10. 精益生产管理专家——安岷老师
  11. GlobalKnowledge: 2013 IT 技能薪水报告
  12. Unhandled exception occurred whilst decorating page java.lang.ArrayIndexOutOfBoundsException: -1
  13. hhkb mac设置_HHKB MAC 配置指南 操作指南 快捷键
  14. 2022年学C++开发好比49年入国军,没什么公司在用C++了?
  15. [UE5蓝图基础一]13.类似”人类一败涂地”掉落一定距离会回到空中 最终着落点还是设定地形上
  16. shell双引号、单引号、反撇号的使用
  17. vue使用vue-video-player实现web视频直播展示m3u8格式
  18. java上机实验作业 编写汽车类car,Java代写:CS103 Car Rental代做留学生SQL实验作业...
  19. 青云、UCloud、阿里云、腾讯云等分别都有哪些特点?
  20. golang入门实战(二)

热门文章

  1. 做好博客平台,互通知识有无
  2. html什么浏览器好,什么浏览器好用
  3. 【物尽其用】ADKEY多按键制作与经验分享
  4. 美国政府CIO倡议创新大数据应用
  5. 数据库的sha1密码破解
  6. Java毕设项目葡萄酒销售管理系统(java+VUE+Mybatis+Maven+Mysql)
  7. Spring+SpringMVC+Hibernate整合操作数据库 概述
  8. 牛客练习赛63 F 牛牛的树行棋 (SG函数+树差分)
  9. 快递查询软件可以自动识别单号的物流公司吗
  10. 全球著名IT公司名字的由来