11 разрушаемость, взрывы, полет ракеты


Для установки коллайдеров (SetColliders):

using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class SetColliders : MonoBehaviour 
{
	public bool _SetColliders;
	void Update () 
	{
		if(_SetColliders)
		{
			foreach(Transform child in transform)
			{
				child.gameObject.AddComponent<BoxCollider>();
			}
		}
	}
}

Для физического взрыва (Explosion):

using UnityEngine;
using System.Collections;

public class Explosion : MonoBehaviour 
{
	public float Radius;
	public float Force;
	void Update () 
	{
		Collider[] hitColliders = Physics.OverlapSphere(transform.position, Radius);
		for(int i=0; i<hitColliders.Length; i++)
		{
			if(hitColliders[i].GetComponent<Wall>())
			{
				hitColliders[i].GetComponent<Wall>().Dead();
			}
			if(hitColliders[i].GetComponent<Propan>())
			{
				hitColliders[i].GetComponent<Propan>().Dead();
			}
			if(hitColliders[i].CompareTag("CanBeRigidbody"))
			{
				if(!hitColliders[i].rigidbody)
				{
				hitColliders[i].gameObject.AddComponent<Rigidbody>();
				}
				hitColliders[i].rigidbody.AddExplosionForce(Force, transform.position, Radius, 0.0F);
			}
		}
		Destroy(gameObject,0.2f);
	}
	void OnDrawGizmos()
	{
		Gizmos.color = Color.red;
		Gizmos.DrawWireSphere(transform.position,Radius);
	}
}

Для ракеты (Rocket):

using UnityEngine;
using System.Collections;

public class Rocket : MonoBehaviour 
{
	public bool Activate;
	public float Speed = 5;
	private float timeout;
	private int scatter;
	public int Scatter;
	public Transform Explosion;
	public Transform GraphicsExplosion;
	public Transform Smoke;
	private float LifeTime;
	void Start () 
	{
		transform.parent = null;
		scatter = Random.Range(-Scatter,Scatter);
	}
	void Update () 
	{
		if(Activate)
		{
			timeout+= Time.deltaTime;
			if(timeout>2f)
			{
				scatter = Random.Range(-Scatter,Scatter);
				timeout = 0;
			}
			transform.Translate(Vector3.forward * Time.deltaTime*Speed);
			transform.RotateAround(transform.position, transform.TransformDirection(Vector3.right), scatter*Time.deltaTime);
			transform.RotateAround(transform.position, transform.TransformDirection(Vector3.up), scatter*Time.deltaTime);
		}
		LifeTime+=Time.deltaTime;
		if(LifeTime>10)
		{
			Dead();
		}
		if(Physics.Raycast(transform.position,transform.TransformDirection(Vector3.forward),1.4f))
		{
			Dead();
		}
		
	}
	void Dead()
	{
			Smoke.parent = null;
			Smoke.particleEmitter.emit = false;
			Destroy(Smoke.gameObject,9f);
			Instantiate(Explosion,transform.position+transform.TransformDirection(Vector3.forward) * 1.4f,transform.rotation);
			Instantiate(GraphicsExplosion,transform.position+transform.TransformDirection(Vector3.forward) * 1.4f,transform.rotation);
			Destroy(gameObject);
	}
	void OnDrawGizmos()
	{
		Gizmos.color = Color.red;
		Gizmos.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 1.4f);
	}
}

Скрипт для разрушаемых объектов (CanBeDestroyed):

using UnityEngine;
using System.Collections;

public class CanBeDestroyed : MonoBehaviour 
{
	public Transform destroyed;
	public void Dead()
	{
		Instantiate(destroyed,transform.position,transform.rotation);
		Destroy(gameObject);
	}
}

Скрипт визуализации взрыва (ExplosiveAnimation):

using UnityEngine;
using System.Collections;

public class ExplosiveAnimation : MonoBehaviour 
{
	private float ramptime;
	private float alphatime = 1;
	public float loopduration;
	void Start () 
	{
	Destroy(gameObject,7);
	}
	void Update () 
	{
		ramptime+=Time.deltaTime;
		alphatime-=Time.deltaTime/2f;
		float r = Mathf.Sin((Time.time / loopduration) * (2 * Mathf.PI)) * 0.5f + 0.25f;
		float g = Mathf.Sin((Time.time / loopduration + 0.33333333f) * 2 * Mathf.PI) * 0.5f + 0.25f;
		float b = Mathf.Sin((Time.time / loopduration + 0.66666667f) * 2 * Mathf.PI) * 0.5f + 0.25f;
		float correction = 1 / (r + g + b);
		r *= correction;
		g *= correction;
		b *= correction;
		renderer.material.SetVector("_ChannelFactor", new Vector4(r,g,b,0));
		renderer.material.SetVector("_Range", new Vector4(ramptime,0.2f,0,0));
		renderer.material.SetFloat("_ClipRange", alphatime);
	}
}

Шейдер для объемного взрыва:

Shader "Custom/Explosion"
{
    Properties 
    {
        _RampTex ("Color Ramp", 2D) = "white" {}
        _DispTex ("Displacement Texture", 2D) = "gray" {}
        _Displacement ("Displacement", Range(0, 1.0)) = 0.1
        _ChannelFactor ("ChannelFactor (r,g,b)", Vector) = (1,0,0)
        _Range ("Range (min,max)", Vector) = (0,0.5,0)
        _ClipRange ("ClipRange [0,1]", float) = 0.8
    }
 
    SubShader 
    {
        Tags { "RenderType"="Opaque" }
        Cull Off
        LOD 300
 
        CGPROGRAM
        #pragma surface surf Lambert vertex:disp nolightmap
        #pragma target 3.0
        #pragma glsl
 
        sampler2D _DispTex;
        float _Displacement;
        float3 _ChannelFactor;
        float2 _Range;
        float _ClipRange;
 
        struct Input 
        {
            float2 uv_DispTex;
        };
 
        void disp (inout appdata_full v)
        {
            float3 dcolor = tex2Dlod (_DispTex, float4(v.texcoord.xy,0,0));
            float d = (dcolor.r*_ChannelFactor.r + dcolor.g*_ChannelFactor.g + dcolor.b*_ChannelFactor.b);
            v.vertex.xyz += v.normal * d * _Displacement;
        }
 
        sampler2D _RampTex;
 
        void surf (Input IN, inout SurfaceOutput o) 
        {
            float3 dcolor = tex2D (_DispTex, IN.uv_DispTex);
            float d = (dcolor.r*_ChannelFactor.r + dcolor.g*_ChannelFactor.g + dcolor.b*_ChannelFactor.b) * (_Range.y-_Range.x) + _Range.x;
            clip (_ClipRange-d);
            half4 c = tex2D (_RampTex, float2(d,0.5));
            o.Albedo = c.rgb;
            o.Emission = c.rgb*c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

Текстуры для взрыва:
explosion displace texture

explosion ramp texture

Мои настройки частиц:
ExplosionSmokeExplosionSparksFireSmoke
Урок по шейдеру: http://stevencraeynest.wordpress.com/2013/03/29/easy-volumetric-explosion-in-unity3d/

Комментарии к этому посту

  • Михаил
    18.07.2014 at 20:57

    Все хорошо,но у меня создаеться сразу несколько стен разбитых на кусочки,в итоге получаеться что кусочки разлетаються ,а пробоен нет



Оставить комментарий