简单的酷跑游戏制作思路

酷跑游戏是比较简单的,一个好的酷跑游戏,主要在于游戏场景的色调配置,以及动画制作的效果。酷跑游戏的简单的说就是手势的检测。
下面是我的在游戏物体上绑定的两个脚本

对手势检测的脚本

using UnityEngine;
using System.Collections;
public class SwipeInputController : MonoBehaviour
{
    private float fingerActionSensitivity = Screen.width * 0.05f; //手指动作的敏感度,这里设定为 二十分之一的屏幕宽度.
    private float fingerBeginX;
    private float fingerBeginY;
    private float fingerCurrentX;
    private float fingerCurrentY;
    private float fingerSegmentX;
    private float fingerSegmentY;
    private int fingerTouchState;
    private int FINGER_STATE_NULL = 0;
    private int FINGER_STATE_TOUCH = 1;
    private int FINGER_STATE_ADD = 2;
    private float fingerTime = 0;
    private bool isBengin = false;
    private float fingerSpeed = 0.08f;

    private Road roadType;
    public enum Road
    {
        Yellow = 1<<0,
        Red = 1<<2,
        Blue = 2<<3
    }

    void Start()
    {
        roadType = Road.Red;
        fingerActionSensitivity = Screen.width * 0.05f;
        fingerBeginX = 0;
        fingerBeginY = 0;
        fingerCurrentX = 0;
        fingerCurrentY = 0;
        fingerSegmentX = 0;
        fingerSegmentY = 0;
        fingerTouchState = FINGER_STATE_NULL;
    }

    void Update()
    {
        if (isBengin)
        {
            fingerTime += Time.deltaTime;
            //print(fingerTime);
        }
        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            isBengin = true;
            if (fingerTouchState == FINGER_STATE_NULL)
            {
                fingerTouchState = FINGER_STATE_TOUCH;
                fingerBeginX = Input.mousePosition.x;
                fingerBeginY = Input.mousePosition.y;
            }
        }
        if (fingerTouchState == FINGER_STATE_TOUCH)
        {
            fingerCurrentX = Input.mousePosition.x;
            fingerCurrentY = Input.mousePosition.y;
            fingerSegmentX = fingerCurrentX - fingerBeginX;
            fingerSegmentY = fingerCurrentY - fingerBeginY;
        }
        if (fingerTouchState == FINGER_STATE_TOUCH)
        {
            float fingerDistance = fingerSegmentX * fingerSegmentX + fingerSegmentY * fingerSegmentY;
            if (fingerDistance > (fingerActionSensitivity * fingerActionSensitivity))
            {
                if (fingerTime < fingerSpeed)
                {
                    toAddFingerAction();
                }

            }
        }
        if (Input.GetKeyUp(KeyCode.Mouse0))
        {
            isBengin = false;
            if (fingerTime > fingerSpeed)
            {
                fingerTime = 0;
            }
            fingerTouchState = FINGER_STATE_NULL;
        }
    }
    private void toAddFingerAction()
    {
        fingerTouchState = FINGER_STATE_ADD;
        if (Mathf.Abs(fingerSegmentX) > Mathf.Abs(fingerSegmentY))
        {
            fingerSegmentY = 0;
        }
        else
        {
            fingerSegmentX = 0;
        }
        if (fingerSegmentX == 0)
        {
            if (fingerSegmentY > 0)
            {

                Debug.Log("up");
                transform.position += Vector3.forward;
                GetComponent().velocity = Vector3.up * 5;
            }
            else
            {
                transform.position += Vector3.down;
                Debug.Log("down");
            }
        }
        else if (fingerSegmentY == 0)
        {
            if (fingerSegmentX > 0)
            {
                if (!roadType.Equals(Road.Blue))
                {
                    print("****" + !roadType.Equals(Road.Yellow));
                    transform.position += Vector3.right;
                    Debug.Log("right");
                    if (roadType.Equals(Road.Yellow))
                    {
                        roadType = Road.Red;
                    }
                    else
                    {
                        roadType = Road.Blue;
                    }
                } 
            }
            else
            {
                if (!roadType.Equals(Road.Yellow))
                {
                    print("****" + !roadType.Equals(Road.Yellow));
                    transform.position -= Vector3.right;
                    Debug.Log("left");
                    if (roadType.Equals(Road.Red))
                    {
                        roadType = Road.Yellow;
                    }
                    else
                    {
                        roadType = Road.Red;
                    }
                }
            }
        }
        fingerTime = 0;
    }
}

下面是游戏物体自身移动的脚本

using UnityEngine;
using System.Collections;

public class Move : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        transform.Translate(Vector3.forward * 10 * Time.deltaTime);
    }
}

简单的酷跑游戏制作思路_第1张图片

你可能感兴趣的:(Unity)