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
原创粉丝点击