Unity学习之切水果游戏

本文记录了Unity切水果游戏的源码,包括FireFruit.cs用于发射水果,AnimalHit.cs处理游戏结束条件,FruitHit.cs计算分数,KnifeRay.cs生成水果刀,SceneController.cs管理计分和生命,StarUI.cs负责游戏启动界面。提供了项目源码链接。
摘要由CSDN通过智能技术生成

坚持 成长 每日一篇

项目源码http://download.csdn.net/detail/u014410695/9173379

这里只对源码进行记录,不对项目过程进行分析

FireFruit.cs

功能介绍:从左右发射水果,给每一个水果设置初始速度

using UnityEngine;
using System.Collections;

public class FireFruit : MonoBehaviour {

    // Use this for initialization
    public GameObject[] gameObjectArray;
    //污渍数组
    public GameObject[] splashArray;

    public bool stopFire = false;

    //设置物体y方向的起始速度为20
    float objectSpeedy = 20;

    //用于保存物体上升时间
    float upTime;

    //用于保存物体重力加速度
    float addSpeed;

    //用于计算发射时间
    float time;

    void Start () {
        float time = Time.time;
        Vector3 topPoint = Camera.main.ScreenToWorldPoint (new Vector3 (0, Screen.height, -5));
        Debug.Log (topPoint.y);
        upTime = topPoint.y / (objectSpeedy / 2);
        Debug.Log ("upTime"+upTime);
        addSpeed = objectSpeedy / upTime-10;
        Debug.Log ("addSpeed"+addSpeed);

    }

    // Update is called once per frame
    void Update () {

        if (!stopFire) {
            if (Time.time - time > 1.5f) {
                time = Time.time;   
                fireFruit ();
            }
        }


    }
    void fireFruit(){
        int i = Random.Range (1, 5);
        for (int t = 0; t < i; t++) {
            int index = Random.Range(0,6);
            if(index == 0)
            {
                index = Random.Range(0,6);
            }
            GameObject fireGameObject = gameObjectArray[index];
            fireFruitWithObject (fireGameObject);
        }





    }
    void fireFruitWithObject(GameObject fireGameObject){
        float x = Random.Range (0, Screen.width);
        if (x < Screen.width / 2) {
            float angle = Random.Range (0, 180) * 180 / 3.14f;

            Vector3 worldPosition = Camera.main.ScreenToWorldPoint (new Vector2 (x, 0));
            worldPosition.z = -5;

            GameObject objc = (GameObject)Instantiate (fireGameObject,worldPosition, Quaternion.AngleAxis (angle, Vector3.forward));
            objc.GetComponent<Rigidbody> ().velocity = new Vector2 (5, Random.Range(objectSpeedy-2,objectSpeedy+2));

            Destroy (objc, 3f);
        } else {
            float angle = Random.Range (0, 180) * 180 / 3.14f;
            Vector3 worldPosition = Camera.main.ScreenToWorldPoint (new Vector2 (x, 0));
            worldPosition.z = -5;
            GameObject objc = (GameObject)Instantiate (fireGameObject, worldPosition, Quaternion.AngleAxis (angle, Vector3.forward));
            objc.GetComponent<Rigidbody> ().velocity = new Vector2 (-5,Random.Range(objectSpeedy-2,objectSpeedy+2));
            Destroy (objc, 3f);
        }
        Physics.gravity = new Vector2 (0, -(addSpeed));
    }

}

AnimalHit.cs

功能描述:切到炸弹时候游戏结束

using UnityEngine;
using System.Collections;

public class AnimalHit : MonoBehaviour {

    // Use this for initialization
    public GameObject Animal01;
    public GameObject Animal02;
    //保证水果只被击中一次
    private bool hasHit = false;
    AudioSource audioSource;
    float time;
    void Start () {
        time = Time.time;
        audioSource = GetComponent<AudioSource> ();

    }

    // Update is called once per frame
    void Update () {


    }
    void isHit(){
        Debug.Log ("Screen.width = " + Screen.width + "Screen.height = " + Screen.height);

        GameObject game = GameObject.Find ("Main Camera");
        SceneController cout = game.GetComponent<SceneController>();
        cout.hitAnimals();


        float angle01 = (float)(Random.Range ((float)50.0, (float)180.0) * 180 / 3.14);
        float angle02 = (float)(Random.Range ((float)
  • 4
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值