SpaceShooter打飞机教程笔记(二)
来源:互联网 发布:linux php 提权 编辑:程序博客网 时间:2024/05/17 04:07
下面的代码是每一单节中对应的代码,会在最后贴完整的代码。
代码的注释是个好东西,看了好些时间视频才把注释写好,也是一种劳动成果。
五、角色移动
1.添加Player脚本(PlayerManager.cs),移动velocity方法,限制position功能,倾斜(rotation)飞机,使用范围Mathf.clamp限制。
2.添加倾斜代码,加入倾斜代码后可以使飞机倾斜躲避子弹,增加游戏趣味和观赏性。
控制旋转的代码如下:
GetComponent().rotation=Quaternion.Euler(0.0f,0.0f,GetComponent().velocity.x * -tilt);
PlayerManager.cs
using UnityEngine;using System.Collections;[System.Serializable]//序列化,可以展开选项,没有的话看不到public class Boundary//规定移动范围{ public float xMin; public float xMax; public float zMin; public float zMax;}public class PlayerManager : MonoBehaviour { public int speed; //移动速度 public float tilt; //旋转时的参数,控制旋转幅度 public Boundary boundary; void FixedUpdate() { //移动控制代码 float moveHorizontal = Input.GetAxis("Horizontal"); //水平方向 float moveVertical = Input.GetAxis("Vertical"); //垂直方向 Vector3 moveMent = new Vector3(moveHorizontal, 0.0f, moveVertical); GetComponent<Rigidbody>().velocity = moveMent * speed; //位置限制使用Mathf.clamp范围 GetComponent<Rigidbody>().position = new Vector3(Mathf.Clamp(GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax), 0.0f, Mathf.Clamp(GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)); //飞行时的飞机旋转由水平方向速度控制旋转幅度 GetComponent<Rigidbody>().rotation = Quaternion.Euler(0, 0, GetComponent<Rigidbody>().velocity.x * -tilt); }}
挂在Player上的脚本参数
六、创建射击
1.创建空命名为bolt(父类一般的都需要重置一下),创建子类Quad命名VXF,X轴(Rotation)旋转90,添加子弹材质贴图(fx_lazer_cyan_dff),去掉子弹的Mesh Collider网格,在父类bolt中添加胶囊网格(Capsule Collider),胶囊的方向选择(Direction:Z-Axis),胶囊的大小适中,添加Rigidbody,添加移动代码(Mover.cs)。
(创建Material,透明选择Particles -> Additive或者Mobile)
Transform.forward *speed Y轴移动
Mover.cs
using UnityEngine;using System.Collections;public class Mover : MonoBehaviour { public float speed; //子弹的速度 //在游戏唤醒的时候赋予速度,如果在Update中编写,速度方向会随旋转的角度变化,陨星不能正常向下。 void Awake() { GetComponent<Rigidbody>().velocity = transform.forward * speed; }}
1 0
- SpaceShooter打飞机教程笔记(二)
- SpaceShooter打飞机教程笔记(一)
- SpaceShooter打飞机教程笔记(三)
- SpaceShooter打飞机教程笔记(四)
- SpaceShooter打飞机教程笔记(五)
- Unity-官方教程-SpaceShooter-个人开发笔记
- quick-cocos2d-x 飞机大战(打飞机)小demo教程------主角飞机类
- cocos2d-x 2.x打飞机(二)
- quick-cocos2d-x 飞机大战(打飞机)小demo教程------敌人类
- quick-cocos2d-x 飞机大战(打飞机)小demo教程------子弹类
- quick-cocos2d-x 飞机大战(打飞机)小demo教程------碰撞检测以及胜负
- unity3d开发 打飞机小游戏(二)(飞机动画设置)
- 第三十八天 一乐在其中—Android的小游戏打飞机(二)移动飞机
- libgdx 打飞机游戏实现教程
- Java小游戏之打飞机(二)
- 打飞机
- 打飞机
- 打飞机
- 使用JNI将C结构体转为JAVA实体类
- Android Studio 快捷键
- 剑指offer——二叉树的镜像
- 【POJ1562】Oil Deposits
- linux使用GDB 调试
- SpaceShooter打飞机教程笔记(二)
- 简单链表
- spark实战:对日志进行查询
- POJ3420 Quad Tiling
- 从头说12种排序算法:原理、图解、动画视频演示、代码以及笔试面试题目中的应用
- 问题锦囊之WS_MOBILE_PAY_SDK_BASE
- Android子线程中更新UI的3种方法
- HDU-5763-Another Meaning-hash加DP
- 一种基于TS包比例占用的固定码率TS流复用算法