Разрушение объектов


Для монтировки:

using UnityEngine;
using System.Collections;

public class Crowbar2 : MonoBehaviour {
	public Transform Montirovka;
	public AnimationClip damage1;
	public AnimationClip damage2;
	private int rndAnim;
	private float Timeout;
	public Transform sparks;
	public AudioClip metalhit;
	// Update is called once per frame
	void Update () 
	{
		if(Timeout>0)
		{
			Timeout -= Time.deltaTime;
		}
		if(Input.GetMouseButton(0)&Timeout<=0)
		{
			Timeout = 0.3f;
			Vector3 DirectionRay = transform.TransformDirection(Vector3.forward);
			RaycastHit Hit;
			rndAnim = Random.Range(0,2);
			if(rndAnim==0)
			{
				Montirovka.animation.Play(damage1.name);
			}
			if(rndAnim==1)
			{
				Montirovka.animation.Play(damage2.name);
			}
			if(Physics.Raycast(transform.position, DirectionRay, out Hit, 1.2f))
			{
				if(Hit.rigidbody)
				{
					Hit.rigidbody.AddForceAtPosition(DirectionRay*4000f,Hit.point);
				}
				if(Hit.transform.collider.material.staticFriction == 0.25f)
				{
					Montirovka.audio.PlayOneShot(metalhit);
					Instantiate(sparks,Hit.point,Quaternion.identity);
				}
				if(Hit.transform.GetComponent("ObjectHealth"))
				{
					ObjectHealth oh = (ObjectHealth)Hit.transform.GetComponent("ObjectHealth");
					oh.health -= Random.Range(25,75);
				}
				if(Hit.transform.CompareTag("Crash")&!Hit.rigidbody)
				{
					Hit.transform.gameObject.AddComponent<Rigidbody>();
					Hit.transform.gameObject.rigidbody.mass = 10;
				}
			}
		}
	}
}

Для хелсы:

using UnityEngine;
using System.Collections;

public class ObjectHealth : MonoBehaviour {
	
	public int health = 100;
	public Transform CrashObject;
	// Update is called once per frame
	void Update () 
	{
		if(health<=0)
		{
			Instantiate(CrashObject,transform.position,transform.rotation);
			Destroy(gameObject);
		}
	}
}

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

  • Андрей
    24.09.2013 at 05:22

    Спасиба за полезный урок))


  • 26.01.2014 at 19:15

    огромное спасибо за совет!!!



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