坚持 成长 每日一篇
项目源码http://download.youkuaiyun.com/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)30.0, (