Unity学习-飞机2D游戏

这篇博客介绍了如何使用Unity创建2D飞机游戏,包括为玩家对象绑定Rigidbody2D,通过Input.GetAxis控制玩家速度,使用Rigidbody2D.velocity实现移动。还提到了一个初始化时子弹向上飘的BUG,原因是赋值了初始位移偏移量。此外,文章涵盖了Transform组件的使用,如位置、旋转和大小的调整,并强调了Time.deltaTime在游戏开发中的重要性。尽管使用标准包方便,但创新才是游戏独特性的关键。最后,作者遇到了调用爆炸粒子效果的bug以及添加射击音效的讨论。
摘要由CSDN通过智能技术生成

http://pixelnest.io/tutorials/2d-game-unity/player-and-enemies/

根据官网的飞机教程


建立好scenes

给player绑定刚体代码

通过方向键控制刚体速度


using UnityEngine;
using System.Collections;

public class PlayerScript : MonoBehaviour {

	// Use this for initialization
	void Start () {
        print("启动!");
    }

    public Vector2 speed = new Vector2(10, 10);
    public Vector2 movement;
    public Rigidbody2D rigidbodyComponent;


    // Update is called once per frame
    void Update () {
        float inputX = Input.GetAxis("Horizontal");
        float inputY = Input.GetAxis("Vertical");

        movement = new Vector2(
            speed.x * inputX,
            speed.y * inputY
            );
    }

    void FixedUpdate()
    {
        if (rigidbodyComponent == null)
            rigidbodyComponent = GetComponent<Rigidbody2D>();

        rigidbodyComponent.velocity = movement;
    }

    void Awake()
    {
        print("我是构造函数");
    }
}

Input.GetAxis是捕获键盘上的操作


Rigidbody2D.velocity 速度

http://wiki.ceeger.com/script/unityengine/classes/rigidbody2d/rigidbody2d.velocity


移动脚本:

using UnityEngine;
using System.Collections;

public class MoveScript : MonoBehaviour {
    public Vector2 movement;
    public Vector2 speed = new Vector2(-2, 0);

    // Use this for initialization
    void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
        movement = new Vector2(
                speed.x * 1,
                speed.y * 0
                );
    }

    void FixedUpdate()
    {

        rigidbody2D.velocity = movement;
    }
}

血量脚本:


                
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值