Unity游戏开发官方入门教程:飞机大战(五)——实现飞船控制脚本
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游戏开发官方入门教程:飞机大战(十一)——游戏控制
新建脚本控制飞船移动
- 在Assets中新建文件夹Script,用于存放脚本文件
- Player->Add component->New Script->命名为PlayerController
- 将PlayerController拖拽进Script
- 点击"Open"编辑该脚本
- 脚本内容如下:
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;}
}
- 保存脚本,运行游戏,现在可以用方向键或ASWD键控制飞船移动了。
加入速度控制
飞船的速度变量尚未设置,因此移动速度较为缓慢,现加入速度控制。
- 在脚本中增加一个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;}
}
- 在Inspector中可见public变量speed,将speed修改为10
- 保存并运行,可见速度变为原来的10倍:
加入边界控制
为了防止飞船飞出游戏边界,现加入边界控制。
- 增加一个边界类Boundary
- 在边界类中增加4个public变量xMin, xMax, zMin, zMax,用于控制边界的4个顶点
- 在开头加上[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));}
}
- 拖拽飞船的position的x,可知x的范围大概是(-6,6)
- 同理可知,z的范围大概是(-4,8)
- 在Inspector中修改xMin, xMax, zMin, zMax:
- 运行游戏,成功实现边界控制:
加入飞船倾斜效果
左右移动飞船的效果太生硬,现加入倾斜效果。
- 新增public变量tilt,用于设置旋转幅度
- 使用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);}
}
- 在Inspector中将tilt的值修改为4:
- 飞船倾斜效果如下:
参考资料:https://unity3d.com/cn/learn/tutorials/s/space-shooter-tutorial
Unity游戏开发官方入门教程:飞机大战(五)——实现飞船控制脚本相关推荐
- Unity游戏开发官方入门教程:飞机大战(六)——创建子弹
Unity版本:Unity 2018.2.14f1 原视频链接:https://unity3d.com/cn/learn/tutorials/s/space-shooter-tutorial 教程目录 ...
- Unity游戏开发官方入门教程:飞机大战(二)——创建飞船对象
Unity版本:Unity 2018.2.14f1 原视频链接:https://unity3d.com/cn/learn/tutorials/s/space-shooter-tutorial 教程目录 ...
- Unity游戏开发官方入门教程:飞机大战(十)——敌人的爆炸和移动
Unity版本:Unity 2018.2.14f1 原视频链接:https://unity3d.com/cn/learn/tutorials/s/space-shooter-tutorial 教程目录 ...
- OUYA游戏开发快速入门教程
OUYA游戏开发快速入门教程 试读地址:http://pan.baidu.com/s/1o63a3W2 本教程是国内唯一OUYA游戏开发教程.本教程基于Unity全面讲解OUYA游戏开发方式.内容包 ...
- C#游戏开发快速入门教程Unity5.5教程
C#游戏开发快速入门教程Unity5.5教程 试读文档下载地址:http://pan.baidu.com/s/1slwBHoD C#是微软发布的高级程序设计语言,这门语言和C语言一样,已经成为了大学计 ...
- 游戏控制杆OUYA游戏开发快速入门教程
游戏控制杆OUYA游戏开发快速入门教程 1.2.2 游戏控制杆 游戏控制杆各个角度的视图,如图1-4所示,它的硬件规格是本文选自OUYA游戏开发快速入门教程大学霸: 图1-4 游戏控制杆各个角度的 ...
- OUYA游戏开发快速入门教程1.2OUYA的硬件规格
OUYA游戏开发快速入门教程1.2OUYA的硬件规格 从官网上购买回来的OUYA产品,包含游戏主机.游戏控制杆.说明书.电源线.HDMI线.电源线和电池,如图1-2所示.本节就来简要介绍下,游戏主机和 ...
- OUYA游戏开发快速入门教程第1章了解OUYA及其设备
OUYA游戏开发快速入门教程第1章了解OUYA及其设备 OUYA是基于Andorid系统的游戏主机.围绕OUYA游戏机,已经形成一个完整的生态圈.在国外,OUYA已经成为知名的游戏平台.本章会站在玩家 ...
- 【教程汇总+持续更新】Unity游戏开发从入门到入坟
新的一年,本该在年前整理的年终总结被拖到了年后开工.去年大量时间投入在Catlike教程的翻译上,截止目前位置,教程的进度已经完全追平原作者. 去年还有一部分是断断续续的更新SLG实战教程,但遗憾的是 ...
最新文章
- SO_SNDTIMEO和SO_RCVTIMEO
- RHEL5.4部署中央日志服务器之rsyslog+loganalyzer
- 什么是脱离文档流?什么是文档流?
- spark如何解决文件不存在_Spark Read.json无法找到文件
- u-boot.lds 文件分析
- 【016】VS2010连接内置SQL数据库
- 命名空间“System.Web”中不存在类型或命名空间名称“Optimization”解决方法
- freemarker 如何获得list的索引值
- quartus驱动无法识别分析
- 精益生产管理专家——安岷老师
- GlobalKnowledge: 2013 IT 技能薪水报告
- Unhandled exception occurred whilst decorating page java.lang.ArrayIndexOutOfBoundsException: -1
- hhkb mac设置_HHKB MAC 配置指南 操作指南 快捷键
- 2022年学C++开发好比49年入国军,没什么公司在用C++了?
- [UE5蓝图基础一]13.类似”人类一败涂地”掉落一定距离会回到空中 最终着落点还是设定地形上
- shell双引号、单引号、反撇号的使用
- vue使用vue-video-player实现web视频直播展示m3u8格式
- java上机实验作业 编写汽车类car,Java代写:CS103 Car Rental代做留学生SQL实验作业...
- 青云、UCloud、阿里云、腾讯云等分别都有哪些特点?
- golang入门实战(二)