unity3d 制作打飞机小游戏

  

作为刚入门小游戏,在制作过程中遇到一些问题,挑重点记录下。

第一:摇杆的制作

使用了官方的joystick类,但是官方没有提供c#版的joystick,这就导致了我不会将js脚本和c#脚本进行通信。最后解决办法是找到网络大牛翻译回来的joystick  c#版本,顺利实现摇杆。附上c#版joystick

using UnityEngine;

/**

 * File: MPJoystick.cs

 * Author: Chris Danielson of (monkeyprism.com)

 * 

// USED TO BE: Joystick.js taken from Penelope iPhone Tutorial

//

// Joystick creates a movable joystick (via GUITexture) that 

// handles touch input, taps, and phases. Dead zones can control

// where the joystick input gets picked up and can be normalized.

//

// Optionally, you can enable the touchPad property from the editor

// to treat this Joystick as a TouchPad. A TouchPad allows the finger

// to touch down at any point and it tracks the movement relatively 

// without moving the graphic

*/
[RequireComponent(typeof(GUITexture))]

public class MPJoystick : MonoBehaviour
{

    class Boundary
    {

        public Vector2 min = Vector2.zero;

        public Vector2 max = Vector2.zero;

    }
    private static MPJoystick[] joysticks;	 // A static collection of all joysticks

    private static bool enumeratedJoysticks = false;

    private static float tapTimeDelta = 0.3f;	 // Time allowed between taps
    public bool touchPad;

    public Vector2 position = Vector2.zero;

    public Rect touchZone;

    public Vector2 deadZone = Vector2.zero;	 // Control when position is output

    public bool normalize = false; // Normalize output after the dead-zone?

    public int tapCount;

    private int lastFingerId = -1;	 // Finger last used for this joystick

    private float tapTimeWindow;	 // How much time there is left for a tap to occur

    private Vector2 fingerDownPos;

    //private float fingerDownTime;

    //private float firstDeltaTime = 0.5f;
    private GUITexture gui;

    private Rect defaultRect;	 // Default position / extents of the joystick graphic

    private Boundary guiBoundary = new Boundary();	 // Boundary for joystick graphic

    private Vector2 guiTouchOffset;	 // Offset to apply to touch input

    private Vector2 guiCenter;	 // Center of joystick
    void Start()
    {

        gui = (GUITexture)GetComponent(typeof(GUITexture));
        defaultRect = gui.pixelInset;

        defaultRect.x += transform.position.x * Screen.width;// + gui.pixelInset.x; // -  Screen.width * 0.5;

        defaultRect.y += transform.position.y * Screen.height;// - Screen.height * 0.5;
        transform.position = Vector3.zero;
        if (touchPad)
        {

            // If a texture has been assigned, then use the rect ferom the gui as our touchZone

            if (gui.texture)

                touchZone = defaultRect;

        }
        else
        {

            guiTouchOffset.x = defaultRect.width * 0.5f;

            guiTouchOffset.y = defaultRect.height * 0.5f;
            // Cache the center of the GUI, since it doesn‘t change

            guiCenter.x = defaultRect.x + guiTouchOffset.x;

            guiCenter.y = defaultRect.y + guiTouchOffset.y;
            // Let‘s build the GUI boundary, so we can clamp joystick movement

            guiBoundary.min.x = defaultRect.x - guiTouchOffset.x;

            guiBoundary.max.x = defaultRect.x + guiTouchOffset.x;

            guiBoundary.min.y = defaultRect.y - guiTouchOffset.y;

            guiBoundary.max.y = defaultRect.y + guiTouchOffset.y;

        }

    }
    public Vector2 getGUICenter()
    {

        return guiCenter;

    }
    void Disable()
    {

        gameObject.active = false;

        //enumeratedJoysticks = false;

    }
    private void ResetJoystick()
    {

        gui.pixelInset = defaultRect;

        lastFingerId = -1;

        position = Vector2.zero;

        fingerDownPos = Vector2.zero;

    }
    private bool IsFingerDown()
    {

        return (lastFingerId != -1);

    }
    public void LatchedFinger(int fingerId)
    {

        // If another joystick has latched this finger, then we must release it

        if (lastFingerId == fingerId)

            ResetJoystick();

    }
    void Update()
    {

        if (!enumeratedJoysticks)
        {

            // Collect all joysticks in the game, so we can relay finger latching messages

            joysticks = (MPJoystick[])FindObjectsOfType(typeof(MPJoystick));

            enumeratedJoysticks = true;

        }
        int count = Input.touchCount;
        if (tapTimeWindow > 0)

            tapTimeWindow -= Time.deltaTime;

        else

            tapCount = 0;
        if (count == 0)

            ResetJoystick();

        else
        {

            for (int i = 0; i < count; i++)
            {

                Touch touch = Input.GetTouch(i);

                Vector2 guiTouchPos = touch.position - guiTouchOffset;
                bool shouldLatchFinger = false;

                if (touchPad)
                {

                    if (touchZone.Contains(touch.position))

                        shouldLatchFinger = true;

                }

                else if (gui.HitTest(touch.position))
                {

                    shouldLatchFinger = true;

                }
                // Latch the finger if this is a new touch

                if (shouldLatchFinger && (lastFingerId == -1 || lastFingerId != touch.fingerId))
                {
                    if (touchPad)
                    {

                        //gui.color.a = 0.15;

                        lastFingerId = touch.fingerId;

                        //fingerDownPos = touch.position;

                        //fingerDownTime = Time.time;

                    }
                    lastFingerId = touch.fingerId;

                    // Accumulate taps if it is within the time window

                    if (tapTimeWindow > 0)

                        tapCount++;

                    else
                    {

                        tapCount = 1;

                        tapTimeWindow = tapTimeDelta;

                    }
                    // Tell other joysticks we‘ve latched this finger

                    //for (  j : Joystick in joysticks )

                    foreach (MPJoystick j in joysticks)
                    {

                        if (j != this)

                            j.LatchedFinger(touch.fingerId);

                    }

                }
                if (lastFingerId == touch.fingerId)
                {

                    // Override the tap count with what the iPhone SDK reports if it is greater

                    // This is a workaround, since the iPhone SDK does not currently track taps

                    // for multiple touches

                    if (touch.tapCount > tapCount)

                        tapCount = touch.tapCount;
                    if (touchPad)
                    {

                        // For a touchpad, let‘s just set the position directly based on distance from initial touchdown

                        position.x = Mathf.Clamp((touch.position.x - fingerDownPos.x) / (touchZone.width / 2), -1, 1);

                        position.y = Mathf.Clamp((touch.position.y - fingerDownPos.y) / (touchZone.height / 2), -1, 1);

                    }
                    else
                    {

                        // Change the location of the joystick graphic to match where the touch is

                        Rect r = gui.pixelInset;

                        r.x = Mathf.Clamp(guiTouchPos.x, guiBoundary.min.x, guiBoundary.max.x);

                        r.y = Mathf.Clamp(guiTouchPos.y, guiBoundary.min.y, guiBoundary.max.y);

                        gui.pixelInset = r;

                    }
                    if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)

                        ResetJoystick();

                }

            }

        }
        if (!touchPad)
        {

            // Get a value between -1 and 1 based on the joystick graphic location

            position.x = (gui.pixelInset.x + guiTouchOffset.x - guiCenter.x) / guiTouchOffset.x;

            position.y = (gui.pixelInset.y + guiTouchOffset.y - guiCenter.y) / guiTouchOffset.y;

        }
        // Adjust for dead zone

        var absoluteX = Mathf.Abs(position.x);

        var absoluteY = Mathf.Abs(position.y);

        if (absoluteX < deadZone.x)
        {

            // Report the joystick as being at the center if it is within the dead zone

            position.x = 0;

        }

        else if (normalize)
        {

            // Rescale the output after taking the dead zone into account

            position.x = Mathf.Sign(position.x) * (absoluteX - deadZone.x) / (1 - deadZone.x);

        }
        if (absoluteY < deadZone.y)
        {

            // Report the joystick as being at the center if it is within the dead zone

            position.y = 0;

        }

        else if (normalize)
        {

            // Rescale the output after taking the dead zone into account

            position.y = Mathf.Sign(position.y) * (absoluteY - deadZone.y) / (1 - deadZone.y);

        }
    }
}

  第二,屏幕边界处理,还没有学会ngui,只能用最笨拙的方法处理超屏问题。

//屏幕限制范围

        Vector2 screenpos = Camera.main.WorldToScreenPoint(m_transform.position);
        float x = screenpos.x;
        float y = screenpos.y;
        float cross_x = Screen.width;
        float cross_y = Screen.height;
        float xx=-1.0f;
        float yy=5.0f;
        float cross_xx=5.0f;
        float cross_yy=15.0f;

        if (x < 0)
        {
            m_transform.position = new Vector3(xx, m_transform.position.y, m_transform.position.z);
        }
        else if (x > cross_x)
            m_transform.position = new Vector3(cross_xx, m_transform.position.y, m_transform.position.z);
        if (y < 0)
            m_transform.position = new Vector3(m_transform.position.x, m_transform.position.y, yy);
        else if (y > cross_y)
            m_transform.position = new Vector3(m_transform.position.x, m_transform.position.y, cross_yy);

  

unity3d 制作打飞机小游戏

时间: 2024-08-05 19:32:02

unity3d 制作打飞机小游戏的相关文章

使用AxureRP7.0制作经典数独小游戏原型,axure游戏原型下载

之前,有同学在Q群中提问,如何使用axure制作经典数独解谜小游戏,当时由于时间关系没有来得及亲手制作,而是给同学们提供了Axure6.5版本的一个数独解谜游戏的原型,本教程由axure原型库网站录制,转载请注明出处!但是那个原型做的太过繁杂,所以仅供大家参考交流:在此,金乌老师特地抽时间给同学们使用AxureRP7.0制作了一下,感觉对实战逻辑分析和axure变量的掌握比较有考验,所以就放出来供大家学习交流使用. 在学习的过程中,如果你仅凭自己现有的对axure的掌握,无法准确分析并组织出原型

C语言射击类打飞机小游戏

使用c语言编写一个打飞机小游戏,使用键盘按键来进行游戏,击中敌机可获得积分,被敌机撞中死亡一次,每次游戏有3次机会. 在网上查询资料并且和同学讨论之后,对原来的代码有了一些改进, 改进:增加了颜色函数,在你所需要改变窗口颜色的位置调用函数 system("color xx") xx分别指的是背景颜色和文字(前景)颜色.x为一位16进制数,即1-f都可以使用.可以随意组合.增加了终止函数,使游戏在死亡三次后自动结束游戏,并且可以选择是否重新开始游戏:另外增添了设置函数,使得可以对游戏进行

原生js打飞机小游戏

最近为了巩固一下原生的知识,然后拿了一个js小游戏来入手.主要也是为了学习和练手. js代码如下: 1 window.onload = function(){ 2 var oBtn = document.getElementById('gameBtn'); 3 oBtn.onclick = function(){ 4 this.style.display = 'none'; 5 Game.init('div1');//把父级传进去 6 }; 7 }; 8 //创建Game单体 9 10 var

Python编写微信打飞机小游戏(十一)

在这篇博文中,我们准备为打飞机小游戏添加一个暂停的功能,即用户在游戏过程中随时可以通过单击屏幕右上方的一个暂停按钮来暂停和恢复游戏.这个功能看似比较简单,但其中涉及了鼠标操作.图片切换.代码结构的重置等等,接下来我们一一进行介绍. 1.加载暂停按钮图标 在image文件夹下一共有四张暂停按钮的图片,分别为深色和浅色两组,首先在main()函数中加载相关图片资源并初始化暂停/开始标志位: paused = False # 标志是否暂停游戏 pause_nor_image = pygame.imag

利用Python制作一个连连看小游戏,边学边玩!

导语 今天我们将制作一个连连看小游戏,让我们愉快地开始吧~ 开发工具 Python版本:3.6.4 相关模块: pygame模块: 以及一些Python自带的模块 环境搭建 安装Python并添加到环境变量,pip安装需要的相关模块即可. 先睹为快 在cmd窗口运行"Game15.py"文件即可. 效果如下: 原理简介 游戏规则: 玩家通过鼠标交换相邻的拼图,若交换后水平/竖直方向存在连续三个相同的拼图,则这些拼图消失,玩家得分,同时生成新的拼图以补充消失的部分,否则,交换失败,玩家不

Python编写微信打飞机小游戏(一)

最近开始学习Python语言,发现Python有一个神奇的Pygame模块,在编写小游戏时显得非常方便,于是参照教学视频编写了一个微信打飞机的小游戏,网上有很多相关的博客,但都不是很详细,大都是直接贴代码,于是决定沉下心来把编写程序的过程记录下来,与大家分享. 首先声明一点,这篇博客完全参照了小甲鱼的<零基础入门学习Python>教学视频,为了尊重原创,特将原作者的视频地址公布如下:<零基础入门学习Python> Python编译器和Pygame模块的安装网上的教程很多,这里不再赘

Python编写微信打飞机小游戏(四)

之前的工作已经基本上将我方飞机的图形显示工作做的差不多了,这篇博客中我们将开始添加敌方飞机——小型敌机.中型敌机(直升机)和大型敌机(坦克).新建一个enemy.py文件,导入pygame和random模块,开始编写吧(还是要注意文件编码问题,以后就不再啰嗦了). 敌方飞机类与我方飞机模块有一定的相似性,但不会左右移动,不会发射子弹等等.小型敌机是敌方飞机中最基本的类型,一击毙命,没有血量.没有出场音效.中型敌机有一定血量,损毁时附带特殊音效.大型敌机血量最多,出场和损毁时都有特殊音效,游戏中中

用C#制作推箱子小游戏

思路分析: 一.制作一个地图 二.地图中放置墙.箱子.人.目标等 三.让小人动起来完成推箱子动作 游戏制作: 1.按照上述地图制作一个地图  (12行×13列) 地图可以看做是行和列组成的,即可以看做是由二维数组组成的 2.实体化:将0转换为空格,1转换为黑色方块 3.设置箱子.人.目标点 4.先让小人动起来,实现小人向上移动,因为坐标的交换,所以箱子向下移动了,同理,改变坐标可以实现向左.向下.向右移动 向下移动:y+1;向左移动:x-1;向右移动:x+1; 5.实现推箱子,以向上移动为例,其

Python编写微信打飞机小游戏(八)

现在飞机已经能够带着血条突突突飞下来让玩家虐,于是我们给用户一点打飞机的成就感——计分系统以及难度递增机制. 1.计分系统 首先,我们在main()函数中添加全局变量(score)并初始化为零用以统计当前用户的打飞机得分: score = 0 # 统计用户得分 接下来只需要在敌机销毁时对score进行累加就可以了,我们在这里先给敌机标个价:小型敌机一架值500分,中型敌机一架值2000分,大型敌机一架值6000分,然后在敌机损毁时根据敌机类型选择加多少分,对于小型敌机: for each in