Unity3D——个人游戏Car的游戏中控制

/2015/08/25///

by XBW/

///环境 unity4.6.1///

终于弄完了这个停车游戏,先看一下效果图

这个游戏呢,就是在赛道内收集三枚以上氮气,然后再返回起点,在赛车损坏程度内完成就算胜利,我们只做了安卓版,用陀螺仪控制赛车的前进以及左右,碰撞的检测以及UI我写在了Control.cs中,直接挂在给塞车了,其中赛车的tag为Player,氮气的tag为danqi,下面直接上代码了;

using UnityEngine;
using System.Collections;

public class Control : MonoBehaviour
{

    // Use this for initialization
    private int danqi = 0;
    private float life = 100;
    private int right = 1;
    private int jieshu = 1;
    public GUISkin GUIskin;
    public GUISkin GUIskin2;
    public Texture2D img1;
    public Texture2D img2;
    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "danqi")
        {
            Destroy(other.gameObject);
            danqi++;
            
        }
    }
    void Start()
    {
        

    }

    // Update is called once per frame
    void Update()
    {
        if (transform.position.x > 138 && transform.position.x < 139 && transform.position.z > 151 && transform.position.z<154 && danqi >= 3)
        {
            right = 0;
            //Destroy(GameObject.FindGameObjectWithTag("Player"));
            Time.timeScale = 0;
            
        }
        life -=Time.deltaTime * 0.5f;
    }
    void OnGUI()
    {
        GUIStyle go = new GUIStyle();

        go.fontSize = 30;

        go.normal.textColor = new Color(0, 0, 0);

        GUI.skin = GUIskin;
        GUI.DrawTexture(new Rect(Screen.width * 0.01f, Screen.height * 0.02f, Screen.width * 0.1f, Screen.height * 0.15f), img1);
        GUI.DrawTexture(new Rect(Screen.width * 0.72f, Screen.height * 0.02f, Screen.width * 0.1f, Screen.height * 0.15f), img2);
        GUI.Label(new Rect(Screen.width * 0.8f, Screen.height * 0.1f, 100, 100), "氮气  "+ danqi.ToString(), go);
        GUI.Label(new Rect(Screen.width * 0.1f, Screen.height * 0.1f, 100, 100), "损坏  "+ life.ToString(), go);
        GUI.skin = GUIskin2;
        if (GUI.Button(new Rect(Screen.width - Screen.width * 0.3f, Screen.height - Screen.height * 0.15f, Screen.width * 0.3f, Screen.height * 0.15f), "再试一次"))
        {
            Application.LoadLevel(Application.loadedLevelName);
        }
        if (GUI.Button(new Rect(0, Screen.height - Screen.height * 0.15f, Screen.width * 0.3f, Screen.height * 0.15f), "不想玩了"))
        {
            Application.LoadLevel("Menu");
        }
        //if (GUI.Button(new Rect(Screen.width - Screen.width * 0.2f, Screen.height*0.4f, Screen.width * 0.2f, Screen.height * 0.1f), "悬挂 +"))
        //{
        //    GameObject.FindGameObjectWithTag("Player").GetComponent<WheelSuspension_Script>().SpringLength+=0.1f;
        //}
        if(life<=0)
        {
            GUI.skin = GUIskin;
            GUI.Label(new Rect(Screen.width * 0.4f, Screen.height * 0.3f, 100, 100), "YOU LOSE", go);
            Time.timeScale = 0;
            GUI.skin = GUIskin2;
            if (GUI.Button(new Rect(Screen.width * 0.35f, Screen.height * 0.4f, Screen.width * 0.3f, Screen.height * 0.15f), "再试一次"))
            {
                Application.LoadLevel(Application.loadedLevelName);
            }
            if (GUI.Button(new Rect(Screen.width * 0.35f, Screen.height * 0.6f, Screen.width * 0.3f, Screen.height * 0.15f), "返回主菜单"))
            {
                Application.LoadLevel("Menu");
            }
        }
        if (right == 0)
        {
            GUI.skin = GUIskin;
            GUI.Label(new Rect(Screen.width * 0.4f, Screen.height * 0.3f, 100, 100), "YOU WIN", go);
            Time.timeScale = 0;
            GUI.skin = GUIskin2;
            if (GUI.Button(new Rect(Screen.width * 0.35f, Screen.height * 0.4f, Screen.width * 0.3f, Screen.height * 0.15f), "再试一次"))
            {
                Application.LoadLevel(Application.loadedLevelName);
            }
            if (GUI.Button(new Rect(Screen.width * 0.35f, Screen.height * 0.6f, Screen.width * 0.3f, Screen.height * 0.15f), "返回主菜单"))
            {
                Application.LoadLevel("Menu");
            }
        }
    }
}
就这么简单,有不懂的地方或者有错误的地方请留言给我,相互学习一下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值