锁链运动
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class suolianPrize : MonoBehaviour {
public GameObject[] gos;
public GameObject suolianPre;
public GameObject[] suoObj=new GameObject[8];
float minAntLongOld=0f;
float minAntLongNew=0f;
int antNum=0;
AntTool suolianAntScript;
int key_StartGrow;
GameObject tempSuolian;
float tempDis;
public Transform target;//目标为奶酪
public Transform[] bone02=new Transform[3];
public GameObject[] go = new GameObject[3];
public Transform suolian2,suolian3;
public GameObject[] col = new GameObject[3];
int key_move=0;
GameObject tmpCamera;
Vector3 oldPo;
public int FirstAngles;
public GameObject preChengtuo;
GameObject tmpTool;
Vector3 bishaPosition;
UISprite tSprite1,tSprite2,tSprite3;
public Transform suopian1,suopian2,suopian3;
// Use this for initialization
void Start () {
//DL();
if (transform.parent.parent!=null)
{
transform.parent.parent.animation["ui"].speed=2f;
transform.parent.parent.animation.Play("ui");
}
key_StartGrow=0;
FirstAngles=Random.Range(35,50);
Invoke("destroyPrize",10f);
bishaPosition=new Vector3(-0.8551919f, 0.7152288f, -10.10089f);
tSprite1=GameObject.Find("Sprite1").GetComponent<UISprite>();
tSprite2=GameObject.Find("Sprite2").GetComponent<UISprite>();
tSprite3=GameObject.Find("Sprite3").GetComponent<UISprite>();
}
void destroyPrize()
{
Destroy(transform.parent.parent.gameObject);
}
// Update is called once per frame
void Update () {
//自动延长
// if (key_StartGrow==1)
// {
// if (tempSuolian!=null)
// {
// if (tempSuolian.transform.localScale.y<=tempDis)
// {
// //tempSuolian.transform.localScale = new Vector3(suolianPre.transform.localScale.x, distance , suolianPre.transform.localScale.y);
// tempSuolian.transform.localScale+=new Vector3 (0, 0.06f, 0);
// }
// }
// }
// if (key_StartGrow==2)
// {
// if (tempSuolian!=null)
// {
// if (tempSuolian.transform.localScale.y<=tempDis)
// {
// //tempSuolian.transform.localScale = new Vector3(suolianPre.transform.localScale.x, distance , suolianPre.transform.localScale.y);
// tempSuolian.transform.localScale+=new Vector3 (0, 0.06f, 0);
// }
// }
// }
// if (key_StartGrow==3)
// {
// if (tempSuolian!=null)
// {
// if (tempSuolian.transform.localScale.y<=tempDis)
// {
// //tempSuolian.transform.localScale = new Vector3(suolianPre.transform.localScale.x, distance , suolianPre.transform.localScale.y);
// tempSuolian.transform.localScale+=new Vector3 (0, 0.06f, 0);
// }
// else
// {
// Destroy(gameObject);
// }
// }
// }
}
void exe()
{
/////////////////////////////必杀///////////////////////////////////////
if (dataAll.laseTool[0]!=3&&!dataAll.ifBiSha)
{
dataAll.laseTool[0]=3; //0万能胶 1保鲜膜 2平底锅 3锁链 4电击 5蛋糕盒 6杀虫剂 7拳头 8地雷 9电风扇
dataAll.laseTool[1]=-1;
dataAll.laseTool[2]=-1;
tSprite1.spriteName="suolian_D";
tSprite2.spriteName="shanping";
tSprite3.spriteName="shanping";
}
else if (dataAll.laseTool[0]==3&&!dataAll.ifBiSha)
{
if (dataAll.laseTool[1]!=3)
{
dataAll.laseTool[1]=3;
dataAll.laseTool[2]=-1;
tSprite2.spriteName="suolian_D";
tSprite3.spriteName="shanping";
}
else if (dataAll.laseTool[1]==3)
{
CancelInvoke("destroyPrize");
dataAll.laseTool[0]=-1;
dataAll.laseTool[1]=-1;
dataAll.laseTool[2]=3;
dataAll.ifBiSha=true;
//GameObject.Find("Main Camera").GetComponent<createAnt>().stopBiSha();
GameObject.Find("Main Camera").GetComponent<createAnt>().closeOropenShanping();
//print("Bisha Start");
//Invoke("stopLabel",0.5f);
StartCoroutine("stopLabel");
//transform.renderer.enabled=false;
transform.parent.renderer.enabled=false;
transform.collider.enabled=false;
tSprite3.spriteName="suolian_D";
return;
}
}
/////////////////////////////必杀///////////////////////////////////////
StartCoroutine("DrawLine");
}
IEnumerator DrawLine()
{
//transform.renderer.enabled=false;
transform.parent.renderer.enabled=false;
transform.collider.enabled=false;
target=GameObject.Find("nailao").transform;
go[0] = (GameObject)Instantiate(suolianPre, transform.position, Quaternion.identity);
go[0].name="suolian1";
//go[0].transform.LookAt(target.position);
go[0].transform.position=new Vector3 (go[0].transform.position.x,0.8232558f,go[0].transform.position.z);
go[0].transform.LookAt(new Vector3(target.position.x,0.8232558f,target.position.z));
//go[0].transform.rotation=Quaternion.LookRotation(new Vector3(target.position.x,0.8032558f,target.position.z));
bone02[0]=go[0].transform.Find("suolian").Find("Bone02");
col[0]=go[0].transform.Find("suolian").Find("CubeSuo").gameObject;
FirstAngles=Random.Range(20,50);
go[0].transform.Translate(Vector3.forward * 0.2f,Space.Self); //move 0.
go[0].transform.RotateAround(go[0].transform.Find("suolian").Find("objTube08").position, Vector3.up,FirstAngles); //绕某节点转动一定度数
//go[0].transform.Find("suolian").Find("Bone02").Find("suopian").eulerAngles=new Vector3(323.4524f,178.3503f,2.64888f);
suopian1=go[0].transform.Find("suolian").Find("Bone02").Find("suopian");
suopian1.parent=go[0].transform;
suopian1.localScale=new Vector3 (4.444048f,5.7615f,5.771763f);
suopian1.LookAt(Camera.mainCamera.transform.position);
yield return new WaitForSeconds(0.3f);
FirstAngles=Random.Range(30,60);
go[1] = (GameObject)Instantiate(suolianPre, bone02[0].position, go[0].transform.rotation);
go[1].name="suolian2";
//go[1].transform.LookAt(target.position);
bone02[1]=go[1].transform.Find("suolian").Find("Bone02");
col[1]=go[1].transform.Find("suolian").Find("CubeSuo").gameObject;
go[1].transform.Translate(Vector3.forward * 0.33f,Space.Self); //move 0.1f
go[1].transform.RotateAround(bone02[0].position, Vector3.up,-FirstAngles);
//go[1].transform.Find("suolian").Find("Bone02").Find("suopian").eulerAngles=new Vector3(323.4524f,178.3503f,2.64888f);
suopian2=go[1].transform.Find("suolian").Find("Bone02").Find("suopian");
suopian2.parent=go[1].transform;
suopian2.localScale=new Vector3 (4.444048f,5.7615f,5.771763f);
suopian2.LookAt(Camera.mainCamera.transform.position);
yield return new WaitForSeconds(0.3f);
FirstAngles=Random.Range(30,60);
go[2] = (GameObject)Instantiate(suolianPre, bone02[1].position, go[0].transform.rotation);
go[2].name="suolian3";
//go[2].transform.LookAt(target.position);
bone02[2]=go[2].transform.Find("suolian").Find("Bone02");
col[2]=go[2].transform.Find("suolian").Find("CubeSuo").gameObject;
go[2].transform.Translate(Vector3.forward * 0.33f,Space.Self); //move 0.1f
go[2].transform.RotateAround(bone02[1].position, Vector3.up,FirstAngles);
//go[2].transform.Find("suolian").Find("Bone02").Find("suopian").eulerAngles=new Vector3(323.4524f,178.3503f,2.64888f);
suopian3=go[2].transform.Find("suolian").Find("Bone02").Find("suopian");
suopian3.parent=go[2].transform;
suopian3.localScale=new Vector3 (4.444048f,5.7615f,5.771763f);
suopian3.LookAt(Camera.mainCamera.transform.position);
// bone02[0]=go[0].transform.Find("Bone02");
// suolian2=go[0].transform.Find("Bone02").Find("suolian");
// col[0]=go[0].transform.Find("Bone02").Find("CubeSuo").gameObject;
// bone02[1]=go[0].transform.Find("Bone02").Find("suolian").Find("Bone02");
// suolian3=go[0].transform.Find("Bone02").Find("suolian").Find("Bone02").Find("suolian");
// col[1]=go[0].transform.Find("Bone02").Find("suolian").Find("Bone02").Find("CubeSuo").gameObject;
// bone02[2]=go[0].transform.Find("Bone02").Find("suolian").Find("Bone02").Find("suolian").Find("Bone02");
// col[2]=go[0].transform.Find("Bone02").Find("suolian").Find("Bone02").Find("suolian").Find("Bone02").Find("CubeSuo").gameObject;
// transform.renderer.enabled=false;
// transform.collider.enabled=false;
// //Destroy(gameObject,3);
//
// //go[0].transform.eulerAngles-=new Vector3(0f,180f,0f);
// FirstAngles=Random.Range(30,70);
// go[0].transform.RotateAround(bone02[0].position, Vector3.up,FirstAngles);
// print("FirstAngles "+FirstAngles);
// bone02[0].Translate(Vector3.down * dataAll.suolianChangdu,Space.Self);
// //go[0].transform.eulerAngles-=new Vector3(70f,0f,0f);
//
// yield return new WaitForSeconds(0.5f);
//
// //suolian2.transform.LookAt(target.position);
// //suolian2.transform.eulerAngles-=new Vector3(0f,180f,0f);
// FirstAngles=Random.Range(30,70);
// suolian2.transform.RotateAround(bone02[1].position, Vector3.up,-FirstAngles);
// bone02[1].Translate(Vector3.down * dataAll.suolianChangdu,Space.Self);
// //suolian2.transform.eulerAngles-=new Vector3(70f,0f,0f);
//
// yield return new WaitForSeconds(0.5f);
//
// //suolian3.transform.LookAt(target.position);
// //suolian3.transform.eulerAngles-=new Vector3(0f,180f,0f);
// FirstAngles=Random.Range(30,70);
// suolian3.transform.RotateAround(bone02[2].position, Vector3.up,FirstAngles);
// bone02[2].Translate(Vector3.down * dataAll.suolianChangdu,Space.Self);
// //suolian3.transform.eulerAngles-=new Vector3(70f,0f,0f);
//
Invoke("stopCol",1f);
//
Destroy(transform.parent.parent.gameObject,2f);
yield return new WaitForSeconds(1f);
}
void stopCol()
{
Destroy(col[0]);
Destroy(col[1]);
Destroy(col[2]);
//print("desssssss");
}
/////////////////////////////必杀///////////////////////////////////////
IEnumerator stopLabel()
{
yield return new WaitForSeconds(0.5f);
tSprite1.spriteName="shanping";
tSprite2.spriteName="shanping";
tSprite3.spriteName="shanping";
yield return new WaitForSeconds(0.2f);
tmpTool=Instantiate(preChengtuo,bishaPosition,Quaternion.identity) as GameObject;
tmpCamera=GameObject.Find("Main Camera");
oldPo=tmpCamera.transform.position;
StartCoroutine("ZhenPing");
Invoke("stopBiSha2",1.5f);
}
void stopBiSha2()
{
//Destroy(tmpTool);
dataAll.ifBiSha=false;
GameObject.Find("Main Camera").GetComponent<createAnt>().closeOropenShanping();
dataAll.laseTool[0]=-1;
dataAll.laseTool[1]=-1;
dataAll.laseTool[2]=-1;
print("stopBiSha2");
Destroy(transform.parent.parent.gameObject,1);
}
IEnumerator ZhenPing()
{
int i=0;
Vector3 rnInt;
//CancelInvoke("destroyPrize");
yield return new WaitForSeconds(0.2f);
//rnInt=new Vector3(Random.Range(0.02f,0.04f),0f,0f);
rnInt=new Vector3(0f,Random.Range(0.02f,0.04f),0f);
tmpCamera.transform.position +=rnInt;
while (true)
{
yield return new WaitForSeconds(0.01f);
tmpCamera.transform.position -=2*rnInt;
yield return new WaitForSeconds(0.01f);
tmpCamera.transform.position +=2*rnInt;
//rnInt=new Vector3(Random.Range(0.02f,0.04f),0f,0f);
rnInt=new Vector3(0f,Random.Range(0.02f,0.04f),0f);
//rnInt=new Vector3(rnInt.x-0.0002f*i,0f,0f);
i++;
if (i>=10)
{
break;
}
}
tmpCamera.transform.position=oldPo;
//Destroy(gameObject,0.1f);
}
/////////////////////////////必杀///////////////////////////////////////
/*
void exe()
{
gos = GameObject.FindGameObjectsWithTag("Ant");
if (GameObject.FindWithTag("Ant")==null)
{
return;
}
minAntLongOld=Vector3.Distance(transform.position,GameObject.FindWithTag("Ant").transform.position);
suoObj[antNum]=gameObject; //第1个点 宝箱
antNum++;
foreach (GameObject go in gos) //第2个点 第1个ant
{
minAntLongNew=Vector3.Distance(transform.position,go.transform.position);
if ((minAntLongNew<dataAll.suolianNormalLong) //遍历中发现一只蚂蚁的距离小于该距离,就可以视为下一个要连接的点
&&(minAntLongNew>dataAll.suolianMinLong) //锁链连接的两只蚂蚁的最小距离,小于该距离的蚂蚁就不连接
&&go.GetComponent<AntTool>().ifSuoLian==false
&&go.GetComponent<AntTool>().ifEnterScreen)
{
suoObj[antNum]=go;
break;
}
else if (minAntLongNew>=dataAll.suolianNormalLong)
{
if (minAntLongOld>=minAntLongNew
&&go.GetComponent<AntTool>().ifSuoLian==false
&&go.GetComponent<AntTool>().ifEnterScreen)
{
minAntLongOld=minAntLongNew;
suoObj[antNum]=go;
}
}
}
if (suoObj[antNum]==null)
{
Destroy(gameObject);
print("no 3 ant");
return;
}
//suolianAnt suolianAntScript=gameObject.GetComponent("suolianAnt") as suolianAnt;
//suoObj[1].transform.SendMessage("suolianEvent",SendMessageOptions.DontRequireReceiver);
suolianAntScript = suoObj[1].GetComponent<AntTool>();
suolianAntScript.ifSuoLian=true;
suolianAntScript.inSuoLian=1;
antNum++; //第3个点 第2个ant
gos = GameObject.FindGameObjectsWithTag("Ant");
minAntLongOld=4;
foreach (GameObject go in gos)
{
minAntLongNew=Vector3.Distance(suoObj[1].transform.position,go.transform.position);
print("name "+go.name+" minAntLongNew "+minAntLongNew);
if ((minAntLongNew<dataAll.suolianNormalLong) //遍历中发现一只蚂蚁的距离小于该距离,就可以视为下一个要连接的点
&&(minAntLongNew>dataAll.suolianMinLong) //锁链连接的两只蚂蚁的最小距离,小于该距离的蚂蚁就不连接
&&suoObj[antNum-1]!=go
&&go.GetComponent<AntTool>().ifSuoLian==false
&&go.GetComponent<AntTool>().ifEnterScreen
&&getAngle(suoObj[antNum-2].transform.position,suoObj[antNum-1].transform.position,go.transform.position))
{
suoObj[antNum]=go;
break;
}
else if (minAntLongNew>=dataAll.suolianNormalLong)
{
if (minAntLongOld>=minAntLongNew&&suoObj[antNum-1]!=go
&&(minAntLongNew>dataAll.suolianMinLong) //锁链连接的两只蚂蚁的最小距离,小于该距离的蚂蚁就不连接
&&go.GetComponent<AntTool>().ifSuoLian==false
&&go.GetComponent<AntTool>().ifEnterScreen
&&getAngle(suoObj[antNum-2].transform.position,suoObj[antNum-1].transform.position,go.transform.position))
{
minAntLongOld=minAntLongNew;
suoObj[antNum]=go;
}
}
}
if (suoObj[antNum]==null)
{
//Destroy(gameObject);
print("no 3 ant");
transform.renderer.enabled=false;
transform.collider.enabled=false;
Destroy(gameObject,2);
StartCoroutine("DL");
return;
}
suolianAntScript = suoObj[2].GetComponent<AntTool>();
suolianAntScript.ifSuoLian=true;
suolianAntScript.inSuoLian=2;
//suoObj[2].transform.SendMessage("suolianEvent",SendMessageOptions.DontRequireReceiver);
antNum++; //第4个点 第3个ant
gos = GameObject.FindGameObjectsWithTag("Ant");
minAntLongOld=4;
foreach (GameObject go in gos)
{
minAntLongNew=Vector3.Distance(suoObj[2].transform.position,go.transform.position);
print("name "+go.name+" minAntLongNew "+minAntLongNew);
if ((minAntLongNew<dataAll.suolianNormalLong) //遍历中发现一只蚂蚁的距离小于该距离,就可以视为下一个要连接的点
&&(minAntLongNew>dataAll.suolianMinLong) //锁链连接的两只蚂蚁的最小距离,小于该距离的蚂蚁就不连接
&&suoObj[antNum-1]!=go
&&suoObj[antNum-2]!=go
&&go.GetComponent<AntTool>().ifSuoLian==false
&&go.GetComponent<AntTool>().ifEnterScreen
&&getAngle(suoObj[antNum-2].transform.position,suoObj[antNum-1].transform.position,go.transform.position))
{
suoObj[antNum]=go;
break;
}
else if (minAntLongNew>=dataAll.suolianNormalLong)
{
if (minAntLongOld>=minAntLongNew
&&(minAntLongNew>dataAll.suolianMinLong) //锁链连接的两只蚂蚁的最小距离,小于该距离的蚂蚁就不连接
&&suoObj[antNum-1]!=go
&&suoObj[antNum-2]!=go
&&go.GetComponent<AntTool>().ifSuoLian==false
&&go.GetComponent<AntTool>().ifEnterScreen
&&getAngle(suoObj[antNum-2].transform.position,suoObj[antNum-1].transform.position,go.transform.position))
{
minAntLongOld=minAntLongNew;
suoObj[antNum]=go;
}
}
}
if (suoObj[antNum]==null)
{
//Destroy(gameObject);
print("no 3 ant");
transform.renderer.enabled=false;
transform.collider.enabled=false;
Destroy(gameObject,2);
StartCoroutine("DL");
return;
}
suolianAntScript = suoObj[3].GetComponent<AntTool>();
suolianAntScript.ifSuoLian=true;
suolianAntScript.inSuoLian=3;
print("suoObj "+suoObj[1].transform.position);
print("suoObj "+suoObj[2].transform.position);
print("suoObj "+suoObj[3].transform.position);
//suoObj[3].transform.SendMessage("suolianEvent",SendMessageOptions.DontRequireReceiver);
//Instantiate(suolianPre,transform.position,Quaternion.identity);
//dataAll.ifBaoxianmo=true;
//GameObject.Find("suolian").transform.localScale+=new Vector3(GameObject.Find("suolian").transform.localScale.x*3,0,0);
//GameObject.Find("suolian").transform.renderer.material.mainTextureScale = new Vector2 (4f,1f);
//print("dis "+Vector3.Distance(GameObject.Find("suolian").transform.position,GameObject.Find("suolianPrize").transform.position));
//DL();
//Destroy(gameObject);
transform.renderer.enabled=false;
transform.collider.enabled=false;
Destroy(gameObject,2);
StartCoroutine("DL");
}
//主要的画法。
IEnumerator DL()
{
GameObject[] go;
go = new GameObject[3];
int j;
//draw line
Vector3 centerPos = Vector3.zero;
Vector3 startPoint,endPoint;
suolianDes suolianDesScript;
for (j = 0; j < 3; j++)
{
if (suoObj[j + 1]==null)
{
break;
}
centerPos = (suoObj[j].transform.position + suoObj[j + 1].transform.position) / 2;
go[j] = (GameObject)Instantiate(suolianPre, centerPos, Quaternion.identity);
go[j].name = "Line" + j + 1;
startPoint = new Vector3(go[j].transform.localPosition.x,0.7532558f,go[j].transform.localPosition.z);
endPoint = new Vector3(suoObj[j].transform.position.x,0.7532558f,suoObj[j].transform.position.z);
go[j].transform.up = (startPoint - endPoint).normalized;
suolianDesScript = go[j].GetComponent<suolianDes>();
suolianDesScript.endPointNew=suoObj[j + 1];
suolianAntScript.ifSuoLian=true;
float distance = Vector3.Distance(suoObj[j].transform.position, suoObj[j + 1].transform.position);
go[j].transform.renderer.material.mainTextureScale = new Vector2 (1f,distance/go[j].transform.localScale.z);
suoObj[j + 1].transform.SendMessage("suolianEvent",SendMessageOptions.DontRequireReceiver);
// key_StartGrow=j+1;
// tempSuolian=go[j];
// tempDis=distance;
//go[j].transform.localScale = new Vector3(suolianPre.transform.localScale.x, 0.01f , suolianPre.transform.localScale.y);
go[j].transform.localScale = new Vector3(suolianPre.transform.localScale.x, distance , suolianPre.transform.localScale.y);
yield return new WaitForSeconds(0.5f);
}
}
bool getAngle(Vector3 point1,Vector3 point2,Vector3 point3) //以第2点为原点,第1点和第3点之间的夹角
{
Vector3 tempDir1=(point1 - point2);
Vector3 tempDir2=(point3 - point2);
float angle=Vector3.Angle(tempDir1,tempDir2);
if (angle>45)
{
return true;
}
else
{
return false;
}
}
*/
//主要的画法。
// void DL()
// {
// GameObject[] go;
// go = new GameObject[3];
// int j;
// //draw line
// Vector3 centerPos = Vector3.zero;
// Vector3 startPoint,endPoint;
// suolianDes suolianDesScript;
// for (j = 0; j < 3; j++)
// {
// centerPos = (suoObj[j].transform.position + suoObj[j + 1].transform.position) / 2;
// go[j] = (GameObject)Instantiate(suolianPre, centerPos, Quaternion.identity);
//
//
// go[j].name = "Line" + j + 1;
// //go[j].transform.up = (go[j].transform.localPosition - suoObj[j].transform.position).normalized;
// //go[j].transform.up = (go[j].transform.position - suoObj[j].transform.position).normalized;
// startPoint = new Vector3(go[j].transform.localPosition.x,0.7532558f,go[j].transform.localPosition.z);
// endPoint = new Vector3(suoObj[j].transform.position.x,0.7532558f,suoObj[j].transform.position.z);
// go[j].transform.up = (startPoint - endPoint).normalized;
//
// suolianDesScript = go[j].GetComponent<suolianDes>();
// suolianDesScript.endPointNew=suoObj[j + 1];
// suolianAntScript.ifSuoLian=true;
//
// float distance = Vector3.Distance(suoObj[j].transform.position, suoObj[j + 1].transform.position);
// go[j].transform.renderer.material.mainTextureScale = new Vector2 (1f,distance/go[j].transform.localScale.z);
// //go[j].transform.localScale+=new Vector3 (distance- go[j].transform.localScale.x, 0,0);
// //go[j].transform.localScale = new Vector3(suolianPre.transform.localScale.x, distance , suolianPre.transform.localScale.y);
// }
// }
}