SpaceShooter打飞机教程笔记(二)

下面的代码是每一单节中对应的代码,会在最后贴完整的代码。
代码的注释是个好东西,看了好些时间视频才把注释写好,也是一种劳动成果。
五、角色移动
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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值