using UnityEngine;
using System.Collections;
public class WaterControl : MonoBehaviour
{
Transform waterStartTran;//喷口
public float height_y = 0.6f;
private GameObject Obj_ShuiLiu;
private GameObject Obj_ShuiHua;
public int Times;
void Start()
{
waterStartTran = transform.parent;
Obj_ShuiHua = transform.Find("ShuiHua").gameObject;
Obj_ShuiLiu = transform.Find("ShuiLiu").gameObject;
disableFall();
}
// Update is called once per frame
void Update()
{
Ray ray = new Ray(waterStartTran.position, new Vector3(waterStartTran.forward.x, waterStartTran.forward.y * 0.6f, waterStartTran.forward.z) * 15);
RaycastHit hitinfo;
if (Physics.Raycast(ray, out hitinfo))
{//当射线射到罐槽时,说明水炮打到水罐上了,产生水流特效
activeFall(hitinfo, hitinfo.collider.name);
}
else
{
disableFall();
}
}
void activeFall(RaycastHit hitinfo, string name)
{
Ray rayShuiHua;
#region 球罐
if (name.Contains("XF_QG"))
{
if (hitinfo.point.y > -0.9 && hitinfo.point.y < 2.7f)
{
Obj_ShuiLiu.transform.position = hitinfo.point;
if (hitinfo.point.y > 0)
{
rayShuiHua = new Ray(hitinfo.point, calShuiHuaPos(hitinfo) * 15);
checkShuiHuaPos(rayShuiHua);
}
if (hitinfo.point.y == 0)
{
rayShuiHua = new Ray(hitinfo.point, calShuiHuaPos(hitinfo));
checkShuiHuaPos(rayShuiHua);
}
if (hitinfo.point.y < 0)
{
rayShuiHua = new Ray(hitinfo.point, new Vector3(0, hitinfo.point.y, 0) * 15);
checkShuiHuaPos(rayShuiHua);
}
}
else
{
disableFall();
}
}
#endregion
#region 浮顶罐
if (name.Contains("XF_WFD"))
{//浮顶罐
if (hitinfo.point.y > -3 && hitinfo.point.y <= -2)
{
checkShuiHuaPos(new Ray(hitinfo.point, new Vector3(0, hitinfo.point.y, 0)));
Obj_ShuiLiu.SetActive(false);
}
else if (hitinfo.point.y > -2 && hitinfo.point.y < 0)
{
Vector3 vec_Land = (hitinfo.point - waterStartTran.position);
Ray rayLand = new Ray(hitinfo.point, new Vector3(0, hitinfo.point.y, 0) * 15);
RaycastHit hitLand;
if (Physics.Raycast(rayLand, out hitLand))
{
if (hitLand.collider.name.Contains("XF_DM_2"))
{//"XF_DM"----地面
Vector3 vec_Repeat = hitLand.point - waterStartTran.position;
Vector3 vec = vec_Repeat + vec_Land;
for (int i = 0; i < 3; i++)
{
vec = vec + vec_Land;
}
Ray rayFallFix = new Ray(waterStartTran.position, vec.normalized * 12);
RaycastHit hitFallFixed;
if (Physics.Raycast(rayFallFix, out hitFallFixed))
{
if (hitFallFixed.collider.name.Contains("XF_WFD"))
{
Obj_ShuiLiu.transform.position = hitFallFixed.point;
checkShuiHuaPos(new Ray(hitFallFixed.point, new Vector3(0, hitFallFixed.point.y, 0)));
}
else
{
disableFall();
}
}
}
}
else
{
disableFall();
}
}
else if (hitinfo.point.y >= 0 && hitinfo.point.y < 1.42)
{
Obj_ShuiLiu.transform.position = hitinfo.point;
rayShuiHua = new Ray(hitinfo.point, calShuiHuaPos(hitinfo) * 12);
checkShuiHuaPos(rayShuiHua);
}
else
{
disableFall();
}
}
#endregion
#region 拱顶罐
if (name.Contains("guan"))
{
if (hitinfo.point.y > 0)
{
rayShuiHua = new Ray(hitinfo.point, new Vector3(0, -hitinfo.point.y, 0) * 20);
Debug.DrawRay(hitinfo.point, new Vector3(0,- hitinfo.point.y, 0) * 10, Color.yellow, 1f);
}
else {
rayShuiHua = new Ray(hitinfo.point, new Vector3(0, hitinfo.point.y, 0) * 20);
Debug.DrawRay(hitinfo.point, new Vector3(0, hitinfo.point.y, 0) * 10, Color.yellow, 1f);
}
RaycastHit hitLand;
if (Physics.Raycast(rayShuiHua, out hitLand))
{
if (hitLand.collider.name.Contains("XF_DM_2"))
{
Obj_ShuiHua.transform.position = hitLand.point;
}
}
if (hitinfo.point.y > -3 && hitinfo.point.y < -1.5)
{
Obj_ShuiLiu.transform.position = hitinfo.point;
Obj_ShuiLiu.SetActive(true);
Obj_ShuiHua.SetActive(true);
}
else if (hitinfo.point.y >= -1.5 && hitinfo.point.y < 4.0)
{
Vector3 vec;
if (Physics.Raycast(rayShuiHua, out hitLand))
{
if (hitLand.collider.name.Contains("XF_DM_2"))
{
Vector3 vec_Vertical = (hitinfo.point - hitLand.point).normalized;
Vector3 vec_Once = hitinfo.point - waterStartTran.position;
vec = vec_Once*10+ vec_Vertical;
Ray rayShuiHuaFix = new Ray(waterStartTran.position, vec);
Debug.DrawRay(waterStartTran.position, vec, Color.blue, 1f);
RaycastHit hitShuiHuaFix;
if (Physics.Raycast(rayShuiHuaFix,out hitShuiHuaFix))
{
if (hitShuiHuaFix.collider.name.Contains("guan"))
{
Obj_ShuiLiu.transform.position = hitShuiHuaFix.point;
Obj_ShuiLiu.SetActive(true);
Obj_ShuiHua.SetActive(true);
}
}
}
}
}
else
{
disableFall();
}
Debug.DrawRay(waterStartTran.position, (hitinfo.point - waterStartTran.position), Color.red, 1f);
}
#endregion
}
///
/// 不开启水花
///
void disableFall()
{//关闭水流组件
Obj_ShuiHua.SetActive(false);
Obj_ShuiLiu.SetActive(false);
}
///
/// 确定水炮射击球罐时下落水花的最终位置
///
///
void checkShuiHuaPos(Ray ray)
{
RaycastHit hitinfoShuiHua;
if (Physics.Raycast(ray, out hitinfoShuiHua))
{
if (hitinfoShuiHua.collider.name.Contains("XF_DM_2"))
{//"XF_DM_2"----地面
Obj_ShuiHua.transform.position = hitinfoShuiHua.point;
}
}
Obj_ShuiHua.gameObject.SetActive(true);
Obj_ShuiLiu.gameObject.SetActive(true);
}
///
/// 水花的最终落点位置
///
///
///
Vector3 calShuiHuaPos(RaycastHit hitinfo)
{
Vector3 vec;
Vector3 vec_Repeat = new Vector3(0, -hitinfo.point.y, 0);
Vector3 vec_Mother = (waterStartTran.position - hitinfo.point);
if (hitinfo.point.y > 0)
{
Times = 3;
vec = vec_Repeat.normalized + vec_Mother.normalized;
}
else
{
vec = vec_Mother + vec_Repeat;
Times = 5;
}
for (int i = 0; i < Times; i++)
{
vec = vec + vec_Repeat.normalized;
}
return vec;
}
}