Переписал свой скрипт стрельбы под c#, вдруг кому пригодится
Код
//-------------------By CHapaeFF------------------------ //---------------zheka20012@yandex.ru------------------- //---------------Creative Commons CC 0------------------ //-----------------------2015---------------------------
using UnityEngine; using System.Collections;
[RequireComponent (typeof(AudioSource))] public class Shoot : MonoBehaviour {
public string shootAnimation; public string reloadAnimation; public float range; public float fireRate; public float force; public int damage; public int bulletsPerMag; public int magazines; public float reloadTime; public float accuracy; public fireMode currentMode = fireMode.semi;
public AudioClip soundFire; public AudioClip soundReload; public AudioClip soundEmpty; public GameObject bulletHolePrefab; public GameObject bulletHolePrefabWall; public GameObject bulletHolePrefabBlood; public GameObject bulletHolePrefabMetal; public LayerMask layerMask; public Renderer muzzleFlash;
public enum fireMode { none, semi, auto } private int bulletsLeft = 0; private int allBullets; private float nextFireTime = 0.0f; private GameObject mainCamera; private AudioSource sound; private bool reload; Animation animation;
Debug.DrawLine(mainCamera.transform.position, direction * range, Color.red); //для отладки
// Проверяем попала ли куда-нибудь пуля if (Physics.Raycast(mainCamera.transform.position, direction, out hit, range, layerMask.value)) { Quaternion rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
// если пуля попадает в физический объект то двигаем его if (hit.rigidbody) hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
// если пуля попала в объект "Untagged" то сделать на нем дырку и приклеить ее к объекту if (hit.transform.tag == "Untagged") { GameObject bulletHole = Instantiate(bulletHolePrefab, hit.point, rotation) as GameObject; bulletHole.transform.parent = hit.transform; hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver); }
if (hit.transform.tag == "Concrete") { GameObject bulletHoleWall = Instantiate(bulletHolePrefabWall, hit.point, rotation) as GameObject; bulletHoleWall.transform.parent = hit.transform; hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
if (hit.transform.tag == "Enemy") { GameObject bulletHoleBlood = Instantiate(bulletHolePrefabBlood, hit.point, rotation) as GameObject; bulletHoleBlood.transform.parent = hit.transform; hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
if (hit.transform.tag == "Metal") { GameObject bulletHoleMetal = Instantiate(bulletHolePrefabMetal, hit.point, rotation) as GameObject; bulletHoleMetal.transform.parent = hit.transform; hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
}
sound.PlayOneShot(soundFire);
animation.Rewind(shootAnimation);
animation.CrossFade(shootAnimation);
bulletsLeft--;
StartCoroutine(ShowMuzzleFlash());
}
// перезарядка IEnumerator Reload() {
if (allBullets > 0 && bulletsLeft != bulletsPerMag) {
if (allBullets > bulletsPerMag) {
reload = true;
sound.PlayOneShot(soundReload);
animation.CrossFade(reloadAnimation);
yield return new WaitForSeconds(reloadTime);
allBullets -= bulletsPerMag - bulletsLeft;
bulletsLeft = bulletsPerMag;
reload = false;
} else {
reload = true;
sound.PlayOneShot(soundReload);
yield return new WaitForSeconds(reloadTime);
var bullet = Mathf.Clamp(bulletsPerMag, allBullets, bulletsLeft + allBullets);
Создать пустышку, под нее оружие с анимациями, все настроить, потом запускать и играть! --------------------------------------------------------------------------------------- Throw Exception (ノಠ益ಠ)ノ彡┻━┻ Catch Exception ┬──┬ ノ( ゜-゜ノ)
Сообщение отредактировал chapaeff - Вт, 23 Июн 2015, 22:56