Páginas

Unity 5 - Sistema de headshot estilo Sniper Elite









Esse script é um complemento do Sistema de tiro com raycast portanto você terá que ter o script Atirar...

1° - Coloque o script Inimigo na cabeça do seu inimigo e marque a opção Cabeça,Coloque no Inim o corpo completo do inimigo e no Osso o esqueleto do inimigo...

2° - Faça um prefab com uma camera e coloque o script Bala nele.

3° - Ajuste o ConfigHS a sua necessidade..

4° - Configure o osso do modo q você quiser... no Vídeo o shader do osso é o GUI Text... se estiver com o standard e o osso do inimigo dentro do corpo não irá aparecer...

Obs:Não deixe a bala em cena,Dará um erro no console de um inimigo para o outro mas não bugará o sistema.... Pretendo arrumar mas não agora... ;-; por pura preguiça ;-;

Script Inimigo

Code   
  1. using UnityEngine;
  2. using System.Collections;
  3. public class Inimigo : MonoBehaviour {
  4.  public static GameObject CabecaReal, Cano, BalaD, Osso;
  5.  public GameObject Inim, Esqueleto;
  6.  public float vida = 100;
  7.  public bool Cabeca;
  8.  bool CabecaMorte,Ignore;
  9.  bool chamouMorte = false;
  10.  void Start (){
  11.  Esqueleto.SetActive (false);
  12.  if (Cabeca == true) {//
  13.  vida = 1; // Remova essa linha caso não queira que seja apenas 1 tiro
  14.  }//
  15.  }
  16.  void Update () {
  17.  if (Cabeca == false) {
  18.  if (vida <= 0) {
  19.  vida = 0;
  20.  if (chamouMorte == false) {
  21.  chamouMorte = true;
  22.  StartCoroutine ("Morrer");
  23.  }
  24.  }
  25.  }
  26.  if (Cabeca == true) {
  27.  if (vida <= 0) {
  28.  vida = 0;
  29.  if (CabecaMorte == false) {
  30.  CabecaMorte = true;
  31.  Ignore = true;
  32.  Instantiate (BalaD, new Vector3 (Cano.transform.position.x, Cano.transform.position.y, Cano.transform.position.z), Quaternion.identity);
  33.  CabecaReal = (this.gameObject);
  34.  Osso = Esqueleto;
  35.  }
  36.  }
  37.  }
  38.  if (Ignore == true) {
  39.  if (Vector3.Distance (transform.position, Bala.ProJB.transform.position) < Bala.Distancia) {
  40.  StartCoroutine ("Falecer");
  41.  }
  42.  }
  43.  }
  44.  IEnumerator Falecer(){
  45.  yield return new WaitForSeconds (Bala.Distancia);
  46.  Destroy (Inim);
  47.  Ignore = false;
  48.  }
  49.  IEnumerator Morrer(){
  50.  GetComponent<MeshRenderer> ().material.color = Color.red;
  51.  yield return new WaitForSeconds (2);
  52.  Destroy (Inim);
  53.  }
  54. }


Script Bala

Code   
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Bala : MonoBehaviour {
  5.  public static float Velocidade, Distancia;
  6.  bool Perto;
  7.  float VelocidadeSalva;
  8.  public static GameObject ProJB;
  9.  void Start (){
  10.  ProJB = this.gameObject;
  11.  VelocidadeSalva = Velocidade;
  12.  }
  13.  void Update () {
  14.  transform.position = Vector3.Lerp (transform.position, Inimigo.CabecaReal.transform.position, Time.deltaTime * Velocidade);
  15.  if (Vector3.Distance (transform.position, Inimigo.CabecaReal.transform.position) < Distancia) {
  16.  StartCoroutine ("Destruir");
  17.  Inimigo.Osso.SetActive (true);
  18.  Velocidade = Velocidade - 10; // Você pode mudar o 10 para ficar + lento ao chegar no inimigo...
  19.  } else {
  20.  Inimigo.Osso.SetActive (false);
  21.  }
  22.  }
  23.  IEnumerator Destruir (){
  24.  yield return new WaitForSeconds (Distancia);
  25.  Velocidade = VelocidadeSalva;
  26.  Destroy (this.gameObject);
  27.  }
  28. }

Script ConfigHS

Code   
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class ConfigHS : MonoBehaviour {
  5.  public GameObject LocalTiro, Projetil;
  6.  public float VelTiro, DisEsqueleto;
  7.  void Update () {
  8.  Bala.Velocidade = VelTiro;
  9.  Inimigo.BalaD = Projetil;
  10.  Inimigo.Cano = LocalTiro;
  11.  Bala.Distancia = DisEsqueleto;
  12.  
  13.  }
  14. }