SchultzGames
Para tirar dúvidas, crie um tópico especifico na área "Dúvidas em Geral" e aguarde ser respondido. Evite comentar em tópicos não relacionados a sua dúvida :D
TÓPICOS RECENTES:
Tópico:
Postado em:
Postado por:
[TUTORIAL] AI que cria scripts para você, inclusive scripts para Unity !!!!
[RESOLVIDO] Animando Image na Unity não funciona
Aplicativo para IOS
Hospedar um jogo multiplay na itch.io
Unity -TextMeshProUGUI- 1 das coisas q estão deixando a scene lenta p abrir
Script para detetectar o movimento do player
Alguém ajuda com um erro por favor? PSDImporter.cs
[RESOLVIDO] - Unity - Rotação da Câmera - Mathf.Clamp não funciona
Alterar textura .unity3d
Comportamento do movimento do inimigo. Sair do caminho e Fugir
Procuro uma equipe
Ajuda Com Sala de Jogo Multiplayer
COMO FAZER A PORTA ABRIR SÓ EMPURRANDO ELA COM O PLAYER?
Visual Studio - Vend os Scripts na Aba "Modo de Exibição de Classe" soltos
app fica fechendo após mobile notification
[RESOLVIDO] Controlar dois player dentro de um jogo multiplay usando mirror
Unity - O Jogo fecha quando vai abrir a Scene
[TUTORIAL] Mudando material
Procuro programador Unity para jogo 2D
AJUDA REALISTIC CAR CONTROLLER
Chama para isqueiro
[RESOLVIDO] Ajuda para destruir objeto
Unity - Sistema de Giro da Tela - Ajuda pfv!
Unity - Touch na Tela do Celular
Dawn Of Hell - Horror Game BR (2022-2023)
Unity - Jogo online ou offline?
ANIMAÇÃO INIMIGO ESTÁ INDO PRA TUDO QUANTO É LADO
[RESOLVIDO] Erro de Script no Android
Bundle Assets, Resources, e Addressables
Jogo online (qual a melhor opção?)
Dúvida sobre como criar o inimigo IA!
Unity dando pause no jogo a cada 4 segundos
bom dia amigos eu sou meio lego nos scripts será que alguem pode me ajudar
JOGO "CHALLENGE" FINALIZADO E DISPONÍVEL PARA BAIXAR.
error CS0246
[TUTORIAL]Como Chamar Um Método Usando Uma String como Parametro
[TUTORIAL] Sistema de FPS com Corpo
NullReferenceException: Object reference not set to an instance
dúvida ao criar subscription no play console
Unity - Scene Demorando pra abrir
Erro Slider de Cor
Alguem sabe que tipo de antena é essa ?
Encontrando um ponto de colisão.
Preciso de vocess! Coisinha simples mais me embananei. Meu primeiro game.
[TUTORIAL] Como carregar uma cena em segundo plano?
push notification com firebase falha
PROCURO UMA EQUIPE - Pagamento VIA PIX
Testem Meu Novo Jogo WEBGL ANDROIDD
ajuste de tela automático de celular
como fazer um chat interativo offline?
Música grátis para Jogos Indies [Calma, Relaxante, Flauta e outras coisas]
PROCURO PROGRAMADOR EXPERIENTE PHOTON
problema com canvas ui button
Banco de dados em Unity v2021.3.4f1
Sistema de Yu-Gi-Oh! - LINK PARA DOWNLOAD 2023
Int de dinheiro fica com numeros negativos em vez de travar no "0"
UNITY - FORMAÇÃO DE EQUIPE
[TUTORIAL] Open World Project - Para Unity-
Tutorial + Kit Para Criar seu Game Multiplayer em 5 Minutos
Como fazer movimentação no estilo tunnel Rush
[RESOLVIDO] Ajuste de tela
Debugar erro ao fechar build do projeto
[RESOLVIDO] Como Suavizar transição de uma Animação pra outra
[RESOLVIDO] Fixar tamanho/resolução proporcional da janela da aplicação
Como faço pra chamar a funçao de um script em outro ?
[RESOLVIDO] Alterar variaveis de classes instanciadas no inspector
como fazer o objeto spawnar com uma cor aleatória dentre as que eu quero?
Blender-Cell Fracture - Juntar as Animações de Todos os Fragmentos em 1 só
Oque acham dessa pequena música?
Unity - Faz mal ou tem algum problema se usar mts Layers?
Drag and drop em um rect específico da UI
Inimigo mudar de animação
[RESOLVIDO] Texto aparecer tipo donkey kong
De 0 a 10, qual a chance de isso causar problemas legais ao vender o jogo?
Teaser do meu novo projeto, War land
SCRIPT QUE SALVA A CENA INTEIRA.
datebest.net - visit link to find love or quick sex !
[RESOLVIDO] Dropdown Salvar a Resolução da tela
[RESOLVIDO] Visual Studio - Como saber onde um método está sendo usado
Teste Chat Openai
Como alterar um GameObject dentro de uma array
Baixos Brasil - Jogo de Carro Brasileiro!
Erro il2cpp unity 2022
[RESOLVIDO] "Is Trigger" não funcionando
FORMAÇÃO DE EQUIPE UNITY 3D
Equipamento para usar no Cinemachine
[RESOLVIDO] Destruir objetos com o mesmo id
Salvar seleção do Toggle
Fiz uma música orquestral para uma batalha final contra um Boss
Como você foi Hackeado DENOVO!!!???
PROCURO UMA EQUIPE - Pagamento VIA Pix
PROCURO PROGRAMADOR EXPERIENTE EM MIRROR
Visual Studio - Como abrir um Script com determinada configuração?
Mover objeto no eixo y
EXPORTAR CENA EM FORMATO DE VIDEO?
LANÇAMENTO JOGO DE CARRO E MOTO PARA ANDROID
Como fazer um sistema para trocar musica da cena com DROPDOWN?
SOU MODELADOR
FPS Controller e Menu
Ontem à(s) 11:20 pm
Ontem à(s) 8:52 pm
Ontem à(s) 4:00 pm
Sáb Mar 18, 2023 8:41 pm
Sáb Mar 18, 2023 4:35 pm
Sex Mar 17, 2023 3:12 pm
Sex Mar 17, 2023 1:24 pm
Seg Mar 13, 2023 10:45 pm
Seg Mar 13, 2023 9:48 pm
Seg Mar 13, 2023 8:24 pm
Seg Mar 13, 2023 7:17 pm
Dom Mar 12, 2023 10:27 pm
Dom Mar 12, 2023 7:22 pm
Sex Mar 10, 2023 8:26 pm
Sex Mar 10, 2023 8:00 pm
Qui Mar 09, 2023 9:05 am
Ter Mar 07, 2023 2:27 pm
Sex Mar 03, 2023 6:47 pm
Sex Mar 03, 2023 12:01 am
Qui Mar 02, 2023 4:00 pm
Qui Mar 02, 2023 1:35 pm
Qua Mar 01, 2023 5:47 pm
Qua Mar 01, 2023 2:11 am
Seg Fev 27, 2023 11:08 pm
Seg Fev 27, 2023 7:19 pm
Dom Fev 26, 2023 11:49 pm
Dom Fev 26, 2023 8:29 pm
Dom Fev 26, 2023 7:47 pm
Dom Fev 26, 2023 7:46 pm
Dom Fev 26, 2023 6:08 pm
Sáb Fev 25, 2023 8:16 pm
Seg Fev 13, 2023 7:02 pm
Seg Fev 13, 2023 2:44 am
Dom Fev 12, 2023 9:57 pm
Dom Fev 12, 2023 2:19 pm
Sáb Fev 11, 2023 1:01 pm
Sex Fev 10, 2023 11:14 pm
Sex Fev 10, 2023 8:18 pm
Sex Fev 10, 2023 7:46 pm
Qui Fev 09, 2023 11:29 pm
Qui Fev 09, 2023 11:01 pm
Qui Fev 09, 2023 5:42 pm
Qua Fev 08, 2023 2:20 pm
Ter Fev 07, 2023 11:47 pm
Seg Fev 06, 2023 8:24 pm
Seg Fev 06, 2023 5:40 pm
Dom Fev 05, 2023 12:53 am
Sáb Fev 04, 2023 3:42 pm
Sáb Fev 04, 2023 2:29 pm
Sáb Fev 04, 2023 12:07 am
Sex Fev 03, 2023 10:53 pm
Sex Fev 03, 2023 10:28 pm
Qui Fev 02, 2023 7:52 pm
Qui Fev 02, 2023 8:49 am
Qua Fev 01, 2023 5:56 pm
Ter Jan 31, 2023 1:00 am
Dom Jan 29, 2023 11:12 pm
Sáb Jan 28, 2023 6:50 pm
Sex Jan 27, 2023 8:52 pm
Sex Jan 27, 2023 4:53 am
Qui Jan 26, 2023 10:56 pm
Dom Jan 22, 2023 11:53 am
Dom Jan 22, 2023 3:33 am
Sáb Jan 21, 2023 3:27 pm
Sáb Jan 21, 2023 2:15 pm
Sex Jan 20, 2023 4:30 pm
Sex Jan 20, 2023 2:15 pm
Qua Jan 18, 2023 10:19 pm
Qua Jan 18, 2023 9:47 am
Ter Jan 17, 2023 10:52 pm
Ter Jan 17, 2023 4:08 pm
Ter Jan 17, 2023 2:45 pm
Seg Jan 16, 2023 7:20 pm
Seg Jan 16, 2023 12:18 pm
Dom Jan 15, 2023 3:02 pm
Sáb Jan 14, 2023 7:02 pm
Sex Jan 13, 2023 8:29 am
Qui Jan 12, 2023 5:15 am
Qui Jan 12, 2023 2:31 am
Qua Jan 11, 2023 1:33 pm
Ter Jan 10, 2023 11:55 am
Ter Jan 10, 2023 10:58 am
Ter Jan 10, 2023 3:04 am
Seg Jan 09, 2023 10:43 pm
Seg Jan 09, 2023 6:30 am
Seg Jan 09, 2023 3:18 am
Seg Jan 09, 2023 2:32 am
Sáb Jan 07, 2023 5:55 pm
Sáb Jan 07, 2023 4:01 pm
Sáb Jan 07, 2023 1:38 pm
Sex Jan 06, 2023 9:15 am
Sex Jan 06, 2023 6:33 am
Qui Jan 05, 2023 5:27 pm
Qui Jan 05, 2023 2:50 pm
Qua Jan 04, 2023 3:07 pm
Qua Jan 04, 2023 9:19 am
Sáb Dez 31, 2022 4:38 pm
Sáb Dez 31, 2022 3:18 am
Qui Dez 29, 2022 6:44 pm



































































































Configurar NPCs

+2
MarcosSchultz
AdrianJps
6 participantes

Ir para baixo

DÚVIDA Configurar NPCs

Mensagem por Daniel Dória Seg Jul 30, 2018 3:54 pm

Baixei o Realistic FPS Prefab para concluir um projeto mas ao usar um player[AI] pronto ele não se meche para nada apenas respira mas se eu atirar eles cai e morre. Logo de cara vi que o Asset está todo desconfigurado já arrumei um bocado de coisas mas essa parte dos NPCs não estou conseguindo.



mas quando vou testar uma cena pronta que vem junto tudo funciona normal já tentei importar dela para minha cena mas não adianta eles voltam a ficar parados lá e não saem do lugar. estou a muito tempo procurando arrumar isso é horrível tentar fazer uma coisa o dia inteiro e não conseguir  alguém pode me ajudar?Distressed

Daniel Dória
Daniel Dória
Avançado
Avançado

Masculino PONTOS : 2164
REPUTAÇÃO : 31
Idade : 21
Respeito as regras : Configurar NPCs WvDYdlf

https://planecaos.jimdofree.com/

Ir para o topo Ir para baixo

DÚVIDA Re: Configurar NPCs

Mensagem por AdrianJps Seg Jul 30, 2018 4:02 pm

Deixa a gente ver o Script do Npc
AdrianJps
AdrianJps
Iniciante
Iniciante

Masculino PONTOS : 2337
REPUTAÇÃO : 1
Idade : 24
Respeito as regras : Configurar NPCs WvDYdlf

Ir para o topo Ir para baixo

DÚVIDA Re: Configurar NPCs

Mensagem por Daniel Dória Seg Jul 30, 2018 4:35 pm

AdrianJps escreveu:Deixa a gente ver o Script do Npc
AI:

Código:

http://AI.cs by Azuline Studios©️ All Rights Reserved
//Allows NPC to track and attack targets and patrol waypoints.
using UnityEngine;
using UnityEngine.AI;//
using System.Collections;
using System.Collections.Generic;

public class AI : MonoBehaviour {
   [HideInInspector]
   public bool spawned = false;
   [HideInInspector]
   public GameObject playerObj;
   [HideInInspector]
   public Transform playerTransform;
   [HideInInspector]
   public GameObject NPCMgrObj;
   [HideInInspector]
   public GameObject weaponObj;//currently equipped weapon object of player
   [HideInInspector]
   public FPSRigidBodyWalker FPSWalker;
   [HideInInspector]
   public NPCAttack NPCAttackComponent;
   [HideInInspector]
   public PlayerWeapons PlayerWeaponsComponent;
   private WeaponBehavior WeaponBehaviorComponent;
   [HideInInspector]
   public CharacterDamage TargetCharacterDamageComponent;
   [HideInInspector]
   public CharacterDamage CharacterDamageComponent;
   [HideInInspector]
   public NPCSpawner NPCSpawnerComponent;
   [HideInInspector]
   public NPCRegistry NPCRegistryComponent;
   [HideInInspector]
   public AI TargetAIComponent;
   [HideInInspector]
   public NavMeshAgent agent;
   [HideInInspector]
   public Collider[] colliders;
   private bool collisionState;
   [HideInInspector]
   public Animation AnimationComponent;
   [HideInInspector]
   public Animator AnimatorComponent;
   private float animSpeed;
   [HideInInspector]
   public bool recycleNpcObjOnDeath;
   [Tooltip("The object with the Animation/Animator component which will be accessed by AI.cs to play NPC animations. If none, this root object will be checked for the Animator/Animations component.")]
   public Transform objectWithAnims;
   [Tooltip("Chance between 0 and 1 that NPC will spawn. Used to randomize NPC locations and surprise the player.")]
   [Range(0.0f, 1.0f)]
   public float randomSpawnChance = 1.0f;
   
   //NPC movement speeds
   [Tooltip("Running speed of the NPC.")]
   public float runSpeed = 6.0f;
   [Tooltip("Walking speed of the NPC.")]
   public float walkSpeed = 1.0f;
   [Tooltip("Speed of running animation.")]
   public float walkAnimSpeed = 1.0f;
   [Tooltip("Speed of walking animation.")]
   public float runAnimSpeed = 1.0f;
   private float speedAmt = 1.0f;
   private float lastRunTime;//to prevent rapid walking, then running when following player
   
   [Tooltip("NPC yaw angle offset when standing.")]
   public float idleYaw = 0.0f;
   [Tooltip("NPC yaw angle offset when moving.")]
   public float movingYaw = 0.0f;
   private float yawAmt;

   [Tooltip("Sets the alignment of this NPC. 1 = friendly to player and hostile to factions 2 and 3, 2 = hostile to player and factions 1 and 3, 3 = hostile to player and factions 1 and 2.")]
   public int factionNum = 1;//1 = human(friendly to player), 2 = alien(hostile to player), 3 = zombie(hostile to all other factions)
   [Tooltip("If false, NPC will attack any character that attacks it, regardless of faction.")]
   public bool ignoreFriendlyFire;
   [HideInInspector]
   public bool playerAttacked;//to make friendly NPCs go hostile if attacked by player
   [HideInInspector]
   public float attackedTime;

   //waypoints and patrolling
   [HideInInspector]
   public Transform myTransform;
   private Vector3 upVec;//cache this for optimization;
   [Tooltip("True if NPC will hunt player accross map without needing to detect player first.")]
   public bool huntPlayer;
   [Tooltip("True if NPC should only follow patrol waypoints once.")]
   public bool patrolOnce;
   [Tooltip("True if NPC should walk on patrol, will run on patrol if false.")]
   public bool walkOnPatrol = true;
   private  Transform curWayPoint;
   [Tooltip("Drag the parent waypoint object with the WaypointGroup.cs script attached here. If none, NPC will stand watch instead of patrolling.")]
   public WaypointGroup waypointGroup;
   [Tooltip("The number of the waypoint in the waypoint group which should be followed first.")]
   public int firstWaypoint = 1;
   [Tooltip("True if NPC should stand in one place and not patrol waypoint path.")]
   public bool standWatch;
   [Tooltip("True if NPC is following player.")]
   public bool followPlayer;
   [Tooltip("True if NPC can be activated with the use button and start following the player.")]
   public bool followOnUse;
   [Tooltip("True if this NPC wants player to follow them (wont take move orders from player, only from MoveTrigger.cs).")]
   public bool leadPlayer;
   [HideInInspector]
   public bool orderedMove;
   [HideInInspector]
   public bool playerFollow;//true if this NPC wants player to follow them (wont take move orders from player, only from MoveTrigger.cs)
   private bool animInit;//to bypass normal AI update interval for correct animation initialization
   private bool animInitialized;//to only initialize animations once
   private float commandedTime;
   private float talkedTime;
   private bool  countBackwards = false;
   [Tooltip("Minimum distance to destination waypoint that NPC will consider their destination as reached.")]
   public float pickNextDestDist = 2.5f;
   private Vector3 startPosition;
   private float spawnTime;

   [Tooltip("Volume of NPC's vocal sound effects.")]
   public float vocalVol = 0.7f;
   [Tooltip("Sound to play when player commands NPC to stop following.")]
   public AudioClip stayFx1;
   [Tooltip("Sound to play when player commands NPC to stop following.")]
   public AudioClip stayFx2;
   [Tooltip("Sound to play when player commands NPC to start following.")]
   public AudioClip followFx1;
   [Tooltip("Sound to play when player commands NPC to start following.")]
   public AudioClip followFx2;
   [Tooltip("Sound to play when player commands NPC to move to position.")]
   public AudioClip moveToFx1;
   [Tooltip("Sound to play when player commands NPC to move to position.")]
   public AudioClip moveToFx2;
   [Tooltip("Sound to play when NPC has been activated more than joke activate times.")]
   public AudioClip jokeFx;
   [Tooltip("Sound to play when NPC has been activated more than joke activate times.")]
   public AudioClip jokeFx2;
   [Tooltip("Number of consecutive use button presses that activates joke fx.")]
   public int jokeActivate = 33;
   private float jokePlaying = 0.0f;
   private int jokeCount;
   
   [Tooltip("Sound effects to play when pursuing player.")]
   public AudioClip[] tauntSnds;
   [Tooltip("True if taunt sound shouldn't be played when attacking.")]
   public bool cancelAttackTaunt;
   private float lastTauntTime;
   [Tooltip("Delay between times to check if taunt sound should be played.")]
   public float tauntDelay = 2f;
   [Tooltip("Chance that a taunt sound will play after taunt delay.")]
   [Range(0.0f, 1.0f)]
   public float tauntChance = 0.5f;
   [Tooltip("Volume of taunt sound effects.")]
   public float tauntVol = 0.7f;
   [Tooltip("Sound effects to play when NPC discovers player.")]
   public AudioClip[] alertSnds;
   [Tooltip("Volume of alert sound effects.")]
   public float alertVol = 0.7f;
   private bool alertTaunt;
   
   
   [HideInInspector]
   public AudioSource vocalFx;
   private AudioSource footstepsFx;
   [Tooltip("Sound effects to play for NPC footsteps.")]
   public AudioClip[] footSteps;
   [Tooltip("Volume of footstep sound effects.")]
   public float footStepVol = 0.5f;
   [Tooltip("Time between footstep sound effects when walking (sync with anim).")]
   public float walkStepTime = 1.0f;
   [Tooltip("Time between footstep sound effects when running (sync with anim).")]
   public float runStepTime = 1.0f;
   private float stepInterval;
   private float stepTime;

   //targeting and attacking
   [Tooltip("Minimum range to target to start attack.")]
   public float shootRange = 15.0f;
   [Tooltip("Range that NPC will start chasing target until they are within shoot range.")]
   public float attackRange = 30.0f;
   [Tooltip("Range that NPC will hear player attacks.")]
   public float listenRange = 30.0f;
   [Tooltip("Time between shots (longer for burst weapons).")]
   public float shotDuration = 0.0f;
   [Tooltip("Speed of attack animation.")]
   public float shootAnimSpeed = 1.0f;
   [HideInInspector]
   public float attackRangeAmt = 30.0f;//increased by character damage script if NPC is damaged by player
   [Tooltip("Percentage to reduce enemy search range if player is crouching.")]
   public float sneakRangeMod = 0.4f;
   private float shootAngle = 3.0f;
   [Tooltip("Time before atack starts, to allow weapon to be raised before firing.")]
   public float delayShootTime = 0.35f;
   [Tooltip("Random delay between NPC attacks.")]
   public float randShotDelay = 0.75f;
   [Tooltip("Height of rayCast origin which detects targets (can be raised if NPC origin is at their feet).")]
   public float eyeHeight = 0.4f;
   [Tooltip("Draws spheres in editor for position and eye height.")]
   public bool drawDebugGizmos;
   [HideInInspector]
   public Transform target = null;
   [HideInInspector]
   public bool targetVisible;
   private float lastVisibleTime;
   private Vector3 targetPos;
   [HideInInspector]
   public float targetRadius;
   [HideInInspector]
   public float attackTime = -16.0f;//time last attacked
   private bool attackFinished = true;
   private bool turning;//true if turning to face target
   [HideInInspector]
   public bool cancelRotate;
   [HideInInspector]
   public bool followed;//true when npc is moving to destination defined by MoveTrigger.cs

   private float targetDistance;
   private Vector3 targetDirection;
   private RaycastHit[] hits;
   private bool sightBlocked;//true if sight to target is blocked
   [HideInInspector]
   public bool playerIsBehind;//true if player is behind NPC
   [HideInInspector]
   public float targetEyeHeight;
   private bool pursueTarget;
   [HideInInspector]
   public Vector3 lastVisibleTargetPosition;
   [HideInInspector]
   public float timeout = 0.0f;//to allow NPC to resume initial behavior after searching for target
   [HideInInspector]
   public bool heardPlayer = false;
   [HideInInspector]
   public bool heardTarget = false;
   [HideInInspector]
   public bool damaged;//true if attacked
   private bool damagedState;
   [HideInInspector]
   public float lastDamageTime;

   [HideInInspector]
   public LayerMask searchMask = 0;//only layers to include in target search (for efficiency)
   
   [HideInInspector]
   public RaycastHit attackHit;

   void Start(){
   
      NPCMgrObj = GameObject.Find("NPC Manager");
      NPCRegistryComponent = NPCMgrObj.GetComponent<NPCRegistry>();
      NPCRegistryComponent.Npcs.Add(myTransform.gameObject.GetComponent<AI>());//register this active NPC with the NPCRegistry
      playerObj = Camera.main.transform.GetComponent<CameraControl>().playerObj;
      
      Mathf.Clamp01(randomSpawnChance);

      agent = GetComponent<NavMeshAgent>();

      if(Random.value > randomSpawnChance){
         Destroy(myTransform.gameObject);
      }
   }
   
   void OnEnable(){
      
      myTransform = transform;
      upVec = Vector3.up;;
      startPosition = myTransform.position;
      timeout = 0.0f;
      attackedTime = -16f;//set to negative value to prevent NPCs from having larger search radius briefly after spawning
      if(jokeFx){
         jokePlaying = jokeFx.length * -2f;
      }
      
      collisionState = false;

      //initialize audiosource for footsteps
      footstepsFx = myTransform.gameObject.AddComponent<AudioSource>();
      footstepsFx.spatialBlend = 1.0f;
      footstepsFx.volume = footStepVol;
      footstepsFx.pitch = 1.0f;
      footstepsFx.dopplerLevel = 0.0f;
      footstepsFx.bypassEffects = true;
      footstepsFx.bypassListenerEffects = true;
      footstepsFx.bypassReverbZones = true;
      footstepsFx.maxDistance = 10.0f;
      footstepsFx.rolloffMode = AudioRolloffMode.Linear;
      footstepsFx.playOnAwake = false;
      
      vocalFx = myTransform.gameObject.AddComponent<AudioSource>();
      vocalFx.spatialBlend = 1.0f;
      vocalFx.volume = vocalVol;
      vocalFx.pitch = 1.0f;
      vocalFx.dopplerLevel = 0.0f;
      vocalFx.bypassEffects = true;
      vocalFx.bypassListenerEffects = true;
      vocalFx.bypassReverbZones = true;
      vocalFx.maxDistance = 10.0f;
      vocalFx.rolloffMode = AudioRolloffMode.Linear;
      vocalFx.playOnAwake = false;

      //set layermask to layers such as layer 10 (world collision) and 19 (interactive objects) for target detection
      searchMask = ~(~(1 << 10) & ~(1 << 0) & ~(1 << 13) & ~(1 << 11) & ~(1 << 20));
      
      //if there is no objectWithAnims defined, use the Animation Component attached to this game object
      if(objectWithAnims == null){objectWithAnims = transform;}

      AnimatorComponent = objectWithAnims.GetComponent<Animator>();//set reference to Mecanim animator component
      
      //initialize AI vars
      playerObj = Camera.main.transform.GetComponent<CameraControl>().playerObj;
      playerTransform = playerObj.transform;
      PlayerWeaponsComponent = Camera.main.transform.GetComponent<CameraControl>().weaponObj.GetComponentInChildren<PlayerWeapons>();
      FPSWalker = playerObj.GetComponent<FPSRigidBodyWalker>();
      NPCAttackComponent = GetComponent<NPCAttack>();
      CharacterDamageComponent = GetComponent<CharacterDamage>();

      //Get all colliders for this NPC's body parts
      colliders = GetComponentsInChildren<Collider>();
      
      attackRangeAmt = attackRange;

      AnimatorComponent.SetInteger("AnimState", 0);

      if(!spawned){//if spawned, SpawnNPC function will be called from NPCSpawner.cs. Otherwise, spawn now.   
         StopAllCoroutines();   
         StartCoroutine(SpawnNPC());
      }
      
   }

   //initialize NPC behavior
   public IEnumerator SpawnNPC(){

      yield return new WaitForSeconds(0.1f);//wait for object to initialize

      //initialize navmesh agent
      agent = GetComponent<NavMeshAgent>();
      agent.enabled = false;//Navmesh agent should also be deactivated on prefab to prevent error when spawning
       agent.enabled = true;
      agent.speed = runSpeed;
      agent.acceleration = 60.0f;

      if(!agent.isOnNavMesh){
         yield return null;//wait for navmesh agent to find navmesh and initialize
      }

      if(agent.isOnNavMesh){
         spawnTime = Time.time;
         StartCoroutine(PlayFootSteps());
         if(objectWithAnims != myTransform){
            StartCoroutine(UpdateModelYaw());
         }
         if(!huntPlayer){
            //determine if NPC should patrol or stand watch
            if(!standWatch && waypointGroup && waypointGroup.wayPoints[firstWaypoint - 1]){
               curWayPoint = waypointGroup.wayPoints[firstWaypoint - 1];
               speedAmt = runSpeed;
               startPosition = curWayPoint.position;
               TravelToPoint(curWayPoint.position);
               StartCoroutine(Patrol());
            }else{
               TravelToPoint(startPosition);
               StartCoroutine(StandWatch());
            }
         }else{
            //hunt the player accross the map
            factionNum = 2;
            target = playerTransform;
            targetEyeHeight = FPSWalker.capsule.height * 0.25f;
            lastVisibleTargetPosition = target.position + (target.up * targetEyeHeight);
            attackRange = 2048.0f;
            StartCoroutine(AttackTarget());

            speedAmt = runSpeed;
            TravelToPoint(playerObj.transform.position);
         }
      }else{
         Debug.Log("<color=red>NPC can't find Navmesh:</color> Please bake Navmesh for this scene or reposition NPC closer to navmesh.");
      }
   }

   //draw debug spheres in editor
   void OnDrawGizmos() {
      if(drawDebugGizmos){
         Gizmos.color = Color.yellow;
         Gizmos.DrawSphere(transform.position, 0.2f);
         Gizmos.color = Color.red;
         Gizmos.DrawSphere(transform.position + new Vector3(0.0f, eyeHeight, 0.0f), 0.2f);
      
         Vector3 myPos = transform.position + (transform.up * eyeHeight);
         Vector3 targetPos = lastVisibleTargetPosition;
         Vector3 testdir1 = (targetPos - myPos).normalized;
         float distance = Vector3.Distance(myPos, targetPos);
         Vector3 testpos3 = myPos + (testdir1 * distance);
         
         if (Physics.Linecast(myPos, targetPos)) {
            Gizmos.color = Color.red;
         }else{
            Gizmos.color = Color.green;
         }

         Gizmos.DrawLine (myPos, testpos3);
         Gizmos.DrawSphere(testpos3, 0.2f);

         
         if(target){
            Gizmos.color = Color.yellow;
            Gizmos.DrawLine (myPos, target.position + (transform.up * targetEyeHeight));
         }

         Gizmos.color = Color.green;
         Gizmos.DrawSphere(transform.position + (transform.forward * 0.6f) + (transform.up * eyeHeight), 0.2f);

//         Vector3 dirPerpGizmo = Vector3.Cross (transform.forward, Vector3.forward);
//         dirPerpGizmo.Normalize();
         Vector3 dirPerpGizmo = transform.forward;
         dirPerpGizmo = Quaternion.Euler(0, -90, 0) * dirPerpGizmo;
         agent = GetComponent<NavMeshAgent>();
         Gizmos.color = Color.blue;
         Gizmos.DrawSphere(transform.position + (transform.up * eyeHeight) + (dirPerpGizmo * agent.radius) , 0.2f);
         Gizmos.DrawSphere(transform.position + (transform.up * eyeHeight) - (dirPerpGizmo * agent.radius) , 0.2f);


      }
      
   }

   //For when an NPC starts moving from a stationary/idle stance.
   //This function bypasses normal AI Logic interval of 300ms (for efficiency) and runs the next AI logic pass immediately in the next frame.
   //Doing this prevents NPCs from sliding on the ground when first encountering target or player (prevents delay in movement anim init).
   private void InitializeAnim(){
      if(!animInit && !animInitialized){
         animInit = true;
         animInitialized = true;//to only initialize anims once
      }else{
         animInit = false;
      }
   }
   
   IEnumerator StandWatch(){
      while (true) {

         if(huntPlayer){
            StartCoroutine(SpawnNPC());
            yield break;
         }
         
         //expand search radius if attacked
         if(attackedTime + 6.0f > Time.time){
            attackRangeAmt = attackRange * 6.0f;//expand enemy search radius if attacked to defend against sniping
         }else{
            attackRangeAmt = attackRange;
         }

         //allow player to push friendly NPCs out of their way
         if(playerObj.activeInHierarchy && !collisionState && FPSWalker.capsule){
            foreach(Collider col in colliders){
               Physics.IgnoreCollision(col, FPSWalker.capsule, true);
            }
            collisionState = true;
         }

         CanSeeTarget();
         if ((target && targetVisible) || heardPlayer || heardTarget){
            yield return StartCoroutine(AttackTarget());
         }else{
            if(NPCRegistryComponent){
               NPCRegistryComponent.FindClosestTarget(myTransform.gameObject, this, myTransform.position, attackRangeAmt, factionNum);
            }
         }
         if(attackTime < Time.time){
            if((!followPlayer || orderedMove) && Vector3.Distance(startPosition, myTransform.position) > pickNextDestDist){
               if(!orderedMove){
                  speedAmt = walkSpeed;//walk to next patrol point, designated position, or starting point
               }else{
                  speedAmt = runSpeed;//run to position designated by player
               }
               InitializeAnim();
               TravelToPoint(startPosition);//
            }else if(followPlayer && !orderedMove && Vector3.Distance(playerObj.transform.position, myTransform.position) > pickNextDestDist){
               if(followPlayer && Vector3.Distance(playerObj.transform.position, myTransform.position) > pickNextDestDist * 2f){
                     speedAmt = runSpeed;//run to player if NPC is following and player is far away
                     lastRunTime = Time.time;
               }else{
                  if(lastRunTime + 2.0f < Time.time){
                     speedAmt = walkSpeed;//walk to player if within walking distance
                  }
               }
               InitializeAnim();
               TravelToPoint(playerObj.transform.position);
            }else{
               //play idle animation
               speedAmt = 0.0f;
               agent.isStopped = true;
               SetSpeed(speedAmt);

               if(attackFinished && attackTime < Time.time){
                  AnimatorComponent.SetInteger("AnimState", 0);
               }
               
            }
         }
         if(animInit){
            //only wait one frame till next pass so animations will be initialized now, instead of in 0.3 second normal AI calculation delay
            yield return null;
         }else{
            yield return new WaitForSeconds(0.3f);//wait 0.3 seconds (not every frame) untill next AI calculation (for efficiency)
         }
      }
      
   }
   
   IEnumerator Patrol(){
      while (true) {
      
         if(huntPlayer){
            StartCoroutine(SpawnNPC());
            yield break;
         }
         
         if(curWayPoint && waypointGroup){//patrol if NPC has a current waypoint, otherwise stand watch
            Vector3 waypointPosition = curWayPoint.position;
            float waypointDist = Vector3.Distance(waypointPosition, myTransform.position);
            int waypointNumber = waypointGroup.wayPoints.IndexOf(curWayPoint);

            //if NPC is close to a waypoint, pick the next one
            if((patrolOnce && waypointNumber == waypointGroup.wayPoints.Count - 1)){
               if(waypointDist < pickNextDestDist){
                  speedAmt = 0.0f;
                  startPosition = waypointPosition;
                  StartCoroutine(StandWatch());
                  yield break;//cancel patrol if patrolOnce var is true
               }
            }else{   
               if(waypointDist < pickNextDestDist){
                  if(waypointGroup.wayPoints.Count == 1){
                     speedAmt = 0.0f;
                     startPosition = waypointPosition;
                     StartCoroutine(StandWatch());
                     yield break;//cancel patrol if NPC has reached their only waypoint
                  }
                  curWayPoint = PickNextWaypoint (curWayPoint, waypointNumber);
                  if(spawned && Vector3.Distance(waypointPosition, myTransform.position) < pickNextDestDist){
                     walkOnPatrol = true;//make spawned NPCs run to their first waypoint, but walk on the patrol
                  }
               }
            }

            //expand search radius if attacked
            if(attackedTime + 6.0f > Time.time){
               attackRangeAmt = attackRange * 6.0f;//expand enemy search radius if attacked to defend against sniping
            }else{
               attackRangeAmt = attackRange;
            }
            
            //allow player to push friendly NPCs out of their way
            if(playerObj.activeInHierarchy && !collisionState && FPSWalker.capsule){
               foreach(Collider col in colliders){
                  Physics.IgnoreCollision(col, FPSWalker.capsule, true);
               }
               collisionState = true;
            }

            //determine if player is within sight of NPC
            CanSeeTarget();
            if((target && targetVisible) || heardPlayer || heardTarget){
               yield return StartCoroutine(AttackTarget());
            }else{
               if(NPCRegistryComponent){
                  NPCRegistryComponent.FindClosestTarget(myTransform.gameObject, this, myTransform.position, attackRangeAmt, factionNum);
               }
               // Move towards our target
               if(attackTime < Time.time){
                  if(orderedMove && !followPlayer){
                     if(Vector3.Distance(startPosition, myTransform.position) > pickNextDestDist){
                        speedAmt = runSpeed;
                        TravelToPoint(startPosition);
                     }else{
                        //play idle animation
                        speedAmt = 0.0f;
                        agent.isStopped = true;
                        SetSpeed(speedAmt);

                        if(attackFinished && attackTime < Time.time){
                           AnimatorComponent.SetInteger("AnimState", 0);
                        }
                        
                        StartCoroutine(StandWatch());//npc reached player-designated position, stop patrolling and wait here
                        yield break;
                     }
                  }else if(!orderedMove && followPlayer){
                     if(Vector3.Distance(playerObj.transform.position, myTransform.position) > pickNextDestDist){
                        if(Vector3.Distance(playerObj.transform.position, myTransform.position) > pickNextDestDist * 2f){
                           speedAmt = runSpeed;
                           lastRunTime = Time.time;
                        }else{
                           if(lastRunTime + 2.0f < Time.time){
                              speedAmt = walkSpeed;
                           }
                        }
                        TravelToPoint(playerObj.transform.position);
                     }else{
                        //play idle animation
                        speedAmt = 0.0f;
                        agent.isStopped = true;
                        SetSpeed(speedAmt);

                        if(attackFinished && attackTime < Time.time){
                           AnimatorComponent.SetInteger("AnimState", 0);
                        }
                        
                     }
                  }else{
                     //determine if NPC should walk or run on patrol
                     if(walkOnPatrol){speedAmt = walkSpeed;}else{speedAmt = runSpeed;}
                     TravelToPoint(waypointPosition);
                  }
               }
            }
         }else{
            StartCoroutine(StandWatch());//don't patrol if we have no waypoints
            yield break;
         }
         yield return new WaitForSeconds(0.3f);
      }

   }


   void CanSeeTarget(){
   
      if(spawnTime + 1f > Time.time){//add small delay before checking target visibility
         return;
      }
      
      //stop tracking target if it is deactivated
      if((TargetAIComponent && !TargetAIComponent.enabled) || (target && !target.gameObject.activeInHierarchy)){
         target = null;
         TargetAIComponent = null;
         targetVisible = false;
         heardTarget = false;
         return;
      }

      //target player
      if((factionNum != 1 || playerAttacked) && FPSWalker.capsule){

         float playerDistance = Vector3.Distance(myTransform.position + (upVec * eyeHeight), playerTransform.position + (upVec * FPSWalker.capsule.height * 0.25f));

         //listen for player attacks
         if(!heardPlayer && !huntPlayer && FPSWalker.dropTime + 2.5f < Time.time){
            if(playerDistance < listenRange && (target == playerTransform || target == FPSWalker.leanObj.transform || target == null)){
               if(PlayerWeaponsComponent && PlayerWeaponsComponent.CurrentWeaponBehaviorComponent){
                  WeaponBehaviorComponent = PlayerWeaponsComponent.CurrentWeaponBehaviorComponent;
                  if(WeaponBehaviorComponent.shootStartTime + 2.0f > Time.time && !WeaponBehaviorComponent.silentShots){
                     if(target == FPSWalker.leanObj.transform){
                        targetEyeHeight = 0.0f;
                        target = FPSWalker.leanObj.transform;
                        pursueTarget = true;
                     }else{
                        targetEyeHeight = FPSWalker.capsule.height * 0.25f;
                        target = playerTransform;
                     }
                     timeout = Time.time + 6.0f;
                     heardPlayer = true;
                     return;
                  }
               }
            }
         }

         if(huntPlayer){
            targetEyeHeight = FPSWalker.capsule.height * 0.25f;
            target = playerTransform;
         }

         if(playerDistance < attackRangeAmt){

            //target lean collider if player is leaning around a corner
            if(Mathf.Abs(FPSWalker.leanAmt) > 0.1f
            && playerDistance > 2.0f && target == playerTransform
            && (attackedTime + 6.0f > Time.time || heardPlayer)){//allow player to peek around corners undetected if they haven't attacked this npc
               targetEyeHeight = 0.0f;
               target = FPSWalker.leanObj.transform;
            }
            //target main player object if they are not leaning
            if((Mathf.Abs(FPSWalker.leanAmt) < 0.1f || playerDistance < 2.0f) && target == FPSWalker.leanObj.transform){
               targetEyeHeight = FPSWalker.capsule.height * 0.25f;
               target = playerTransform;
            }

         }

      }

      //calculate range and LOS to target
      if(target == playerTransform || target == FPSWalker.leanObj.transform || (TargetAIComponent && TargetAIComponent.enabled && target != null)){
         Vector3 myPos = myTransform.position + (upVec * eyeHeight);
         targetPos = target.position + (target.up * targetEyeHeight);

         targetDistance = Vector3.Distance(myPos, targetPos);
         targetDirection = (targetPos - myPos).normalized;

         Vector3 targetDirPerp = Vector3.Cross (targetDirection, Vector3.forward);
         targetDirPerp.Normalize();

         if(targetDistance > attackRangeAmt){
            sightBlocked = true;
            targetVisible = false;
            return;//don't continue to check LOS if target is not within attack range
         }

         //check LOS with raycast
         hits = Physics.RaycastAll (myPos, targetDirection, targetDistance, searchMask);

         sightBlocked = false;

         //detect if target is behind NPC
         if(!huntPlayer
         && timeout < Time.time
         && attackedTime + 6.0f < Time.time
         && ((target == playerTransform || target == FPSWalker.leanCol) && !heardPlayer)
         && !FPSWalker.sprintActive){
            Vector3 toTarget = (targetPos - myPos).normalized;
            if(Vector3.Dot(toTarget, transform.forward) < -0.45f){
               sightBlocked = true;
               playerIsBehind = true;
               targetVisible = false;
               return;
            }
         }
         
         playerIsBehind = false;

         //check if NPC can see their target
         for(int i = 0; i < hits.Length; i++){
            if((!hits[i].transform.IsChildOf(target)//hit is not target
            && !hits[i].transform.IsChildOf(myTransform))//hit is not NPC's own colliders
             || (!playerAttacked//attack player if they attacked us (friendly fire)
                && (factionNum == 1 && target != playerObj && (hits[i].collider == FPSWalker.capsule//try not to shoot the player if we are a friendly NPC
                         || hits[i].collider == FPSWalker.leanCol)))
            ){
               sightBlocked = true;
               break;
            }
            if(hits[i].transform.IsChildOf(target)){
               attackHit = hits[i];
               break;
            }
         }
         
         if(!sightBlocked){
            if(target != FPSWalker.leanObj.transform){
               pursueTarget = false;
               targetVisible = true;
               return;
            }else{
               pursueTarget = true;//true when NPC has seen only the player leaning around a corner
               targetVisible = true;
               return;
            }
         }else{
            if(TargetAIComponent && !huntPlayer){
               if(TargetAIComponent.attackTime > Time.time && Vector3.Distance(myTransform.position, target.position) < listenRange ){
                  timeout = Time.time + 6.0f;
                  heardTarget = true;
               }
            }
            targetVisible = false;
            return;
         }
         
      }else{
         targetVisible = false;
         return;
      }
   
   }
   
   IEnumerator Shoot(){

      attackFinished = false;

      //don't move during attack
      speedAmt = 0.0f;
      SetSpeed(speedAmt);
      agent.isStopped = true;


      // Start shoot animation
      AnimatorComponent.SetInteger("AnimState", 3);
      AnimatorComponent.SetTrigger("Attack");


      // Wait until delayShootTime to allow part of the animation to play
      yield return new WaitForSeconds(delayShootTime);
      //attack
      NPCAttackComponent.Fire();
      if(cancelAttackTaunt){
         vocalFx.Stop();
      }
      attackTime = Time.time + 2.0f;
      // Wait for the rest of the animation to finish
      yield return new WaitForSeconds(delayShootTime + Random.Range(shotDuration, shotDuration + 0.75f));

      attackFinished = true;

      AnimatorComponent.SetInteger("AnimState", 0);
      

   }
   
   IEnumerator AttackTarget(){
      while (true) {
      
         if(Time.timeSinceLevelLoad < 1f){//add small delay before checking target visibility
            yield return new WaitForSeconds(1.0f);
         }
         
         // no target - stop hunting
         if(target == null || (TargetAIComponent && !TargetAIComponent.enabled) && !huntPlayer){
            timeout = 0.0f;
            heardPlayer = false;
            heardTarget = false;
            damaged = false;
            TargetAIComponent = null;
            yield break;
         }
         
         //play a taunt if hunting target
         if(lastTauntTime + tauntDelay < Time.time
         && Random.value < tauntChance
         && (alertTaunt || alertSnds.Length <= 0)){
            if(tauntSnds.Length > 0){
               vocalFx.volume = tauntVol;
               vocalFx.pitch = Random.Range(0.94f, 1f);
               vocalFx.spatialBlend = 1.0f;
               vocalFx.clip = tauntSnds[Random.Range(0, tauntSnds.Length)];
               vocalFx.PlayOneShot(vocalFx.clip);
               lastTauntTime = Time.time;
            }
         }
         
         //play alert sound if target detected
         if(!alertTaunt){
            if(alertSnds.Length > 0){
               vocalFx.volume = alertVol;
               vocalFx.pitch = Random.Range(0.94f, 1f);
               vocalFx.spatialBlend = 1.0f;
               vocalFx.clip = alertSnds[Random.Range(0, alertSnds.Length)];
               vocalFx.PlayOneShot(vocalFx.clip);
               lastTauntTime = Time.time;
               alertTaunt = true;
            }
         }
         
         float distance = Vector3.Distance(myTransform.position, target.position);
         
         if(!huntPlayer){
            
            //search for player if their attacks have been heard
            if(heardPlayer && (target == playerTransform || target == FPSWalker.leanObj.transform)){
               InitializeAnim();
               speedAmt = runSpeed;
               SearchTarget(lastVisibleTargetPosition);
            }
            //search for target if their attacks have been heard
            if(heardTarget){
               InitializeAnim();
               speedAmt = runSpeed;
               SearchTarget(lastVisibleTargetPosition);
            }
      
            // Target is too far away - give up   
            if(distance > attackRangeAmt){
               speedAmt = walkSpeed;
               target = null;
               yield break;
            }
            
         }else{
            InitializeAnim();
            target = playerTransform;
            speedAmt = runSpeed;
            TravelToPoint(target.position);
         }

         if(pursueTarget){//should NPC attack player collider or leaning collider?
            lastVisibleTargetPosition = FPSWalker.leanObj.transform.position;
         }else{
            lastVisibleTargetPosition = target.position + (target.up * targetEyeHeight);
         }

         CanSeeTarget();
         if(targetVisible){
            timeout = Time.time + 6.0f;
         
            if(distance > shootRange){
               if(!huntPlayer){
                  SearchTarget(lastVisibleTargetPosition);
               }
            }else{//close to target, rotate NPC to face it
               if(!turning){
                  StopCoroutine("RotateTowards");
                  StartCoroutine(RotateTowards(lastVisibleTargetPosition, 20.0f, 2.0f));
               }
               speedAmt = 0.0f;
               SetSpeed(speedAmt);
               agent.speed = speedAmt;
            }

            InitializeAnim();
            speedAmt = runSpeed;

            Vector3 forward = myTransform.TransformDirection(Vector3.forward);
            Vector3 targetDirection = lastVisibleTargetPosition - (myTransform.position + (myTransform.up * eyeHeight));
            targetDirection.y = 0;
            
            float angle = Vector3.Angle(targetDirection, forward);
            
            // Start shooting if close and player is in sight
            if(distance < shootRange && angle < shootAngle){
               if(attackFinished){
                  yield return StartCoroutine(Shoot());
               }else{
                  speedAmt = 0.0f;
                  SetSpeed(speedAmt);
                  agent.isStopped = true;
               }
            }
            
         }else{
            if(!huntPlayer){
               if(attackFinished || huntPlayer){
                  if(timeout > Time.time){
                     InitializeAnim();
                     speedAmt = runSpeed;
                     SetSpeed(speedAmt);
                     SearchTarget(lastVisibleTargetPosition);
                  }else{//if timeout has elapsed and target is not visible, resume initial behavior
                     heardPlayer = false;
                     heardTarget = false;
                     alertTaunt = false;
                     speedAmt = 0.0f;
                     SetSpeed(speedAmt);
                     agent.isStopped = true;
                     target = null;
                     yield break;
                  }
               }
            }
         }
         
         if(animInit){
            //only wait one frame till next pass so animations will be initialized now, instead of in 0.3 second normal AI calculation delay
            yield return null;
         }else{
            yield return new WaitForSeconds(0.3f);//wait 0.3 seconds (not every frame) untill next AI calculation (for efficiency)
         }

      }
   }

   //look for target at a location
   void SearchTarget( Vector3 position  ){
      if(attackFinished){
         if(target == playerTransform || target == FPSWalker.leanObj.transform || (TargetAIComponent && TargetAIComponent.enabled)){
            if(!huntPlayer){
               speedAmt = runSpeed;
               TravelToPoint(target.position);
            }
         }else{
            timeout = 0.0f;
            damaged = false;
         }
      }
   }

   //rotate to face target
   public IEnumerator RotateTowards( Vector3 position, float rotationSpeed, float turnTimer, bool attacking = true ){
      float turnTime;
      turnTime = Time.time;

      SetSpeed(0.0f);
      agent.isStopped = true;

      while(turnTime + turnTimer > Time.time && !cancelRotate){
         turning = true;

         if(pursueTarget){
            position = FPSWalker.leanObj.transform.position;
         }else{
            if((target && attacking && (target == playerTransform || (TargetAIComponent && TargetAIComponent.enabled)))){
               lastVisibleTargetPosition = target.position + (target.up * targetEyeHeight);
            }else{
               lastVisibleTargetPosition = position;
            }
         }
         
         Vector3 direction = lastVisibleTargetPosition - myTransform.position;
         direction.y = 0;
         if(direction.x != 0 && direction.z != 0){
            // Rotate towards the target
            myTransform.rotation = Quaternion.Slerp (myTransform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
            myTransform.eulerAngles = new Vector3(0, myTransform.eulerAngles.y, 0);
            yield return null;
         }else{
            break;//stop rotating
         }
      }
      cancelRotate = false;
      turning = false;
   }
   
   //Allow tweaking of model yaw/facing direction from the inspector for NPC alignment with attack direction
   private IEnumerator UpdateModelYaw(){
      while(true){
         
         if(stepInterval > 0.0f){
            yawAmt = Mathf.MoveTowards(yawAmt, movingYaw, Time.deltaTime * 180.0f);
         }else{
            yawAmt = Mathf.MoveTowards(yawAmt, idleYaw, Time.deltaTime * 180.0f);
         }

         objectWithAnims.transform.localRotation = Quaternion.Euler(0.0f, yawAmt, 0.0f);

         yield return null;
      }
   }

   //set navmesh destination and set NPC speed
   void TravelToPoint( Vector3 position  ){
      if(attackFinished){
         agent.SetDestination(position);
         agent.isStopped = false;
         agent.speed = speedAmt;
         SetSpeed(speedAmt);
         
      }
   }
   
   //pick the next waypoint and determine if patrol should continue forward or backward through waypoint group
   Transform PickNextWaypoint( Transform currentWaypoint, int curWaypointNumber  ){
      
      Transform waypoint = currentWaypoint;

      if(!countBackwards){
         if(curWaypointNumber < waypointGroup.wayPoints.Count -1){
            waypoint = waypointGroup.wayPoints[curWaypointNumber + 1];
         }else{
            waypoint = waypointGroup.wayPoints[curWaypointNumber - 1];
            countBackwards = true;
         }
      }else{
         if(curWaypointNumber != 0){
            waypoint = waypointGroup.wayPoints[curWaypointNumber - 1];
         }else{
            waypoint = waypointGroup.wayPoints[curWaypointNumber + 1];
            countBackwards = false;
         }
      }
      return waypoint;
   }

   //play animations for NPC moving state/speed and set footstep sound intervals
   void SetSpeed( float speed  ){
      if (speed > walkSpeed && agent.hasPath){
         AnimatorComponent.SetInteger("AnimState", 2);
         stepInterval = runStepTime;
      }else{
         if(speed > 0.0f && agent.hasPath){
            AnimatorComponent.SetInteger("AnimState", 1);
            stepInterval = walkStepTime;
         }else{
            if(attackFinished && attackTime < Time.time){
               AnimatorComponent.SetInteger("AnimState", 0);
            }
            stepInterval = -1.0f;
         }
      }
   }

   IEnumerator PlayFootSteps(){
      while(true){
         if(footSteps.Length > 0 && stepInterval > 0.0f ){
            footstepsFx.pitch = 1.0f;
            footstepsFx.volume = footStepVol;
            footstepsFx.clip = footSteps[Random.Range(0, footSteps.Length)];
            footstepsFx.PlayOneShot(footstepsFx.clip);
         }
         yield return new WaitForSeconds(stepInterval);
      }
   }
   
   //Interact with NPC when pressing use key over them
   public void CommandNPC () {
      if(factionNum == 1 && followOnUse && commandedTime + 0.5f < Time.time){
         orderedMove = false;
         cancelRotate = false;
         commandedTime = Time.time;
         if(attackFinished && !turning){
            StopCoroutine("RotateTowards");
            StartCoroutine(RotateTowards(playerTransform.position, 10.0f, 2.0f, false));
         }
         if(!followPlayer){
            if((followFx1 || followFx2) && ((jokeFx && jokePlaying + jokeFx.length < Time.time) || !jokeFx)){
               if(Random.value > 0.5f){
                  vocalFx.clip = followFx1;
               }else{
                  vocalFx.clip = followFx2;
               }
               vocalFx.pitch = Random.Range(0.94f, 1f);
               vocalFx.spatialBlend = 1.0f;
               vocalFx.PlayOneShot(vocalFx.clip);
            }
            followPlayer = true;
         }else{
            if((stayFx1 || stayFx2) && ((jokeFx && jokePlaying + jokeFx.length < Time.time) || !jokeFx)){
               if(Random.value > 0.5f){
                  vocalFx.clip = stayFx1;
               }else{
                  vocalFx.clip = stayFx2;
               }
               vocalFx.pitch = Random.Range(0.94f, 1f);
               vocalFx.spatialBlend = 1.0f;
               vocalFx.PlayOneShot(vocalFx.clip);
            }
            startPosition = myTransform.position;
            followPlayer = false;
         }
      }
      if(jokeFx && factionNum == 1 && followOnUse){
         if(jokeCount == 0){
            talkedTime = Time.time;
         }
         if(talkedTime + 0.5f > Time.time){
            talkedTime = Time.time;
            jokeCount++;
            if(jokeCount > jokeActivate){
               if(!jokeFx2){
                  vocalFx.clip = jokeFx;
               }else{
                  if(Random.value > 0.5f){
                     vocalFx.clip = jokeFx;
                  }else{
                     vocalFx.clip = jokeFx2;
                  }
               }
               vocalFx.pitch = Random.Range(0.94f, 1f);
               vocalFx.spatialBlend = 1.0f;
               vocalFx.PlayOneShot(vocalFx.clip);
               jokePlaying = Time.time;
               jokeCount = 0;
            }
         }else{
            jokeCount = 0;
         }
      }
   }
   
   //Move an NPC to a specific position
   public void GoToPosition (Vector3 position, bool runToPos) {
      if(runToPos){
         orderedMove = true;
      }else{
         orderedMove = false;
      }
      cancelRotate = true;
      startPosition = position;
   }

//   //used to change the faction of the NPC
   public void ChangeFaction(int factionChange){
      target = null;
      factionNum = factionChange;
   }

}

Vou mandar os outros scripts pera aí
Daniel Dória
Daniel Dória
Avançado
Avançado

Masculino PONTOS : 2164
REPUTAÇÃO : 31
Idade : 21
Respeito as regras : Configurar NPCs WvDYdlf

https://planecaos.jimdofree.com/

Ir para o topo Ir para baixo

DÚVIDA Re: Configurar NPCs

Mensagem por Daniel Dória Seg Jul 30, 2018 4:36 pm

AdrianJps escreveu:Deixa a gente ver o Script do Npc
CHARACTER DAMAGE:

Código:

http://CharacterDamage.cs by Azuline Studios©️ All Rights Reserved
//Applies damage to NPCs
using UnityEngine;
using System.Collections;

public class CharacterDamage : MonoBehaviour {
   public UnityEngine.Events.UnityEvent onDie; // for save addon by PixelCrushers
   private AI AIComponent;
   private RemoveBody RemoveBodyComponent;
   [Tooltip("Number of hitpoints for this character or body part.")]
   public float hitPoints = 100.0f;
   private float initialHitPoints;
   [Tooltip("Force to apply to this collider when NPC is killed.")]
   public float attackForce = 2.75f;
   [Tooltip("Item to spawn when NPC is killed.")]
   public GameObject gunItem;
   [Tooltip("Weapon mesh to hide when NPC dies (replaced with usable gun item).")]
   public Transform gunObj;
   private GameObject gunInst;
   [Tooltip("Rotation of spawed gun object after NPC death.")]
   public Vector3 gunItemRotation = new Vector3(0.0f, 180.0f, 180.0f);
   
   private Rigidbody[] bodies;
   [Tooltip("True if ragdoll mode is active for this NPC.")]
   public bool ragdollActive;
   private bool ragdollState;
   
   [Tooltip("If NPC only has one capsule collider for hit detection, replace the NPC's character mesh with a ragdoll, instead of transitioning instantly to ragdoll.")]
   public Transform deadReplacement;
   private Transform dead;
   [Tooltip("Sound effect to play when NPC dies.")]
   public AudioClip dieSound;
   [Tooltip("Determine if this object or parent should be removed on death. This is to allow for different hit detection collider types as children of NPC parent.")]
   public bool notParent;
   [Tooltip("Should this NPC's body be removed after Body Stay Time?")]
   public bool  removeBody;
   [Tooltip("Time for body to stay in the scene before it is removed.")]
   public float bodyStayTime = 15.0f;
   [Tooltip("Time for dropped weapon item to stay in scene before it is removed.")]
   public float gunStayTime = -1f;
   
   [Tooltip("Chance between 0 and 1 that death of this NPC will trigger slow motion for a few seconds (regardless of the body part hit).")]
   [Range(0.0f, 1.0f)]
   public float sloMoDeathChance = 0.0f;
   [Tooltip("True if backstabbing this NPC should trigger slow motion for the duration of slo mo backstab time.")]
   public bool sloMoBackstab = true;
   [Tooltip("Duration of slow motion time in seconds if slo mo death chance check is successful.")]
   public float sloMoDeathTime = 0.9f;
   [Tooltip("Duration of slow motion time in seconds if this NPC is backstabbed.")]
   public float sloMoBackstabTime = 0.9f;
   
   //vars related to attacker position (for physics, and other effects)
   private Vector3 attackerPos2;
   private Vector3 attackDir2;
   private Transform myTransform;
   private bool explosionCheck;
   private LayerMask raymask = 1 << 13;

   void OnEnable (){
      myTransform = transform;
      RemoveBodyComponent = GetComponent<RemoveBody>();
      Mathf.Clamp01(sloMoDeathChance);
      if(removeBody && RemoveBodyComponent){//remove body timer starts if RwmoveBody.cs script is enabled
         RemoveBodyComponent.enabled = false;
      }
      if(!AIComponent){
         AIComponent = myTransform.GetComponent<AI>();
      }
      initialHitPoints = hitPoints;
      bodies = GetComponentsInChildren<Rigidbody>();
   }
   
   void Update () {
      if(bodies.Length > 1){
         if(!ragdollActive){//deactivate ragdoll mode
            if(!ragdollState){
               foreach(Rigidbody rb in bodies){
                  rb.isKinematic = true;
               }

               AIComponent.AnimatorComponent.enabled = true;
               
               if(gunObj){
                  gunObj.gameObject.SetActive(true);
                  if(gunInst){
                     Destroy(gunInst);
                  }
               }
               ragdollState = true;
            }
         }else{//activate ragdoll mode
            if(ragdollState){

               AIComponent.AnimatorComponent.enabled = false;
               
               foreach(Collider col in AIComponent.colliders){
//                  foreach(Collider col2 in AIComponent.colliders){
//                     Physics.IgnoreCollision(col, col2, false);//ignore collisions with other body part colliders (is this even needed???)
//                  }
                  if(AIComponent.FPSWalker.gameObject.activeInHierarchy){
                     Physics.IgnoreCollision(col, AIComponent.FPSWalker.capsule, true);//ignore collisions with player
                  }
               }
               foreach(Rigidbody rb in bodies){
                  rb.isKinematic = false;
               }
               if(gunObj){//spawn gun pickup object
                  gunObj.gameObject.SetActive(false);
                  gunInst = Instantiate(gunItem, gunObj.position, gunObj.rotation) as GameObject;
                  foreach(Collider col in AIComponent.colliders){
                     Physics.IgnoreCollision(col, gunInst.GetComponent<Collider>(), true);//ignore collisions with other body part colliders
                  }
                  gunInst.transform.Rotate(gunItemRotation);
                  Vector3 tempGunpos = gunInst.transform.position + (transform.forward * 0.45f);
                  gunInst.transform.position = tempGunpos;
                  if(gunStayTime > 0.0f && gunInst.GetComponent<WeaponPickup>()){
                     gunInst.GetComponent<WeaponPickup>().StartCoroutine(gunInst.GetComponent<WeaponPickup>().DestroyWeapon(gunStayTime));
                  }
               }
               ragdollState = false;
            }
         }
      }
   }
   
   //damage NPC
   public void ApplyDamage ( float damage, Vector3 attackDir, Vector3 attackerPos, Transform attacker, bool isPlayer, bool isExplosion, Rigidbody hitBody = null, float bodyForce = 0.0f ){

      if (hitPoints <= 0.0f){
         return;
      }

      if(!AIComponent.damaged
      && !AIComponent.huntPlayer
      && (((hitPoints / initialHitPoints) < 0.65f))//has NPC been damaged significantly?
      && attacker
      && !isExplosion
      ){
         if(!isPlayer){
            if(attacker.GetComponent<AI>().factionNum != AIComponent.factionNum){
               AIComponent.target = attacker;
               AIComponent.TargetAIComponent = attacker.GetComponent<AI>();
               AIComponent.targetEyeHeight = AIComponent.TargetAIComponent.eyeHeight;
               AIComponent.damaged = true;
            }
         }else{
            if(!AIComponent.ignoreFriendlyFire){//go hostile on a friendly if they repeatedly attacked us
               AIComponent.target = AIComponent.playerObj.transform;
               AIComponent.targetEyeHeight = AIComponent.FPSWalker.capsule.height * 0.25f;
               AIComponent.playerAttacked = true;
               AIComponent.TargetAIComponent = null;
               AIComponent.damaged = true;
            }
         }
         
      }
      
      //prevent hitpoints from going into negative values
      if(hitPoints - damage > 0.0f){
         if(AIComponent.playerIsBehind && (AIComponent.PlayerWeaponsComponent.CurrentWeaponBehaviorComponent.meleeSwingDelay > 0 || AIComponent.PlayerWeaponsComponent.CurrentWeaponBehaviorComponent.meleeActive)){
            hitPoints -= damage * 32.0f;//backstab npc
            if(sloMoBackstab){
               AIComponent.PlayerWeaponsComponent.FPSPlayerComponent.StartCoroutine(AIComponent.PlayerWeaponsComponent.FPSPlayerComponent.ActivateBulletTime(sloMoBackstabTime));
            }
         }else{
            hitPoints -= damage;
         }
      }else{
         hitPoints = 0.0f;   
      }
      
      attackDir2 = attackDir;
      attackerPos2 = attackerPos;
      explosionCheck = isExplosion;
      
      //to expand enemy search radius if attacked to defend against sniping
      AIComponent.attackedTime = Time.time;
      
      //Kill NPC
      if (hitPoints <= 0.0f){
         onDie.Invoke(); // for save addon by PixelCrushers
         AIComponent.vocalFx.Stop();
            
         if(sloMoDeathChance >= Random.value && isPlayer){
            AIComponent.PlayerWeaponsComponent.FPSPlayerComponent.StartCoroutine(AIComponent.PlayerWeaponsComponent.FPSPlayerComponent.ActivateBulletTime(sloMoDeathTime));
         }
         if(bodies.Length < 2){//if NPC is only using one capsule collider for collision, instantiate ragdoll, instead of activating existing body part rigidbodies
            Die();
         }else{
            if(!ragdollActive){
               RagDollDie(hitBody, bodyForce);
            }
         }
      }
   }
   
   //this method called if NPC has died and has more than one capsule collider for collision, so transition to ragdoll
   void RagDollDie(Rigidbody hitBody, float bodyForce) {
      if (dieSound){
         PlayAudioAtPos.PlayClipAt(dieSound, transform.position, 1.0f);
      }
      
      AIComponent.NPCRegistryComponent.UnregisterNPC(AIComponent);//unregister NPC from main NPC registry
      if(AIComponent.spawned && AIComponent.NPCSpawnerComponent){
         AIComponent.NPCSpawnerComponent.UnregisterSpawnedNPC(AIComponent);//unregister NPC from spawner registry
      }

      AIComponent.AnimatorComponent.enabled = false;
      
      ragdollActive = true;
      if(AIComponent.NPCAttackComponent.muzzleFlash){
         AIComponent.NPCAttackComponent.muzzleFlash.enabled = false;
      }
      AIComponent.NPCAttackComponent.enabled = false;
      AIComponent.StopAllCoroutines();
      AIComponent.agent.enabled = false;
      AIComponent.enabled = false;
      StartCoroutine(ApplyForce(hitBody, bodyForce));
      //initialize the RemoveBody.cs script attached to the NPC ragdoll
      if(RemoveBodyComponent){
         if(removeBody){
            RemoveBodyComponent.enabled = true;
            RemoveBodyComponent.bodyStayTime = bodyStayTime;//pass bodyStayTime to RemoveBody.cs script
         }else{
            RemoveBodyComponent.enabled = false;
         }
      }
   }
   
   public IEnumerator ApplyForce (Rigidbody body, float force) {
      yield return new WaitForSeconds(0.02f);
      if(!explosionCheck){
         //apply damage force to the ragdoll rigidbody
         body.AddForce(attackDir2 * attackForce, ForceMode.Impulse);
      }else{
         //apply explosive damage force to the ragdoll rigidbodies
         foreach(Rigidbody rb in bodies) {
            rb.AddForce((myTransform.position - (attackerPos2 + (Vector3.up * -2.5f))).normalized * Random.Range(2.5f, 4.5f), ForceMode.Impulse);
         }
      }
   }
   
   //this method called if the NPC dies and only has one capsule collider for collision
   //which will be instantiated in place of the main NPC object (which is removed from the scene)
   void Die() {
      
      RaycastHit rayHit;
      // Play a dying audio clip
      if (dieSound){
         PlayAudioAtPos.PlayClipAt(dieSound, transform.position, 1.0f);
      }

      AIComponent.NPCRegistryComponent.UnregisterNPC(AIComponent);//unregister NPC from main NPC registry
      if(AIComponent.spawned && AIComponent.NPCSpawnerComponent){
         AIComponent.NPCSpawnerComponent.UnregisterSpawnedNPC(AIComponent);//unregister NPC from spawner registry
      }

      AIComponent.agent.isStopped = true;
      AIComponent.StopAllCoroutines();
   
      // Replace NPC object with the dead body
      if (deadReplacement) {
         
         //drop arrows if corpse disappears
         ArrowObject[] arrows = gameObject.GetComponentsInChildren<ArrowObject>(true);
         foreach (ArrowObject arr in arrows) {
            arr.transform.parent = null;
            arr.myRigidbody.isKinematic = false;
            arr.myBoxCol.isTrigger = false;
            arr.gameObject.tag = "Usable";
            arr.falling = true;
         }
      
         dead = Instantiate(deadReplacement, transform.position, transform.rotation) as Transform;
         RemoveBodyComponent = dead.GetComponent<RemoveBody>();
   
         // Copy position & rotation from the old hierarchy into the dead replacement
         CopyTransformsRecurse(transform, dead);
         
         Collider[] colliders = dead.GetComponentsInChildren<Collider>();
         foreach(Collider col in colliders){
            Physics.IgnoreCollision(col, AIComponent.FPSWalker.capsule, true);
         }
         
         //apply damage force to NPC ragdoll
         if(Physics.SphereCast(attackerPos2, 0.2f, attackDir2, out rayHit, 750.0f, raymask)
         && rayHit.rigidbody
         && attackDir2.x !=0){
            //apply damage force to the ragdoll rigidbody hit by the sphere cast (can be any body part)
            rayHit.rigidbody.AddForce(attackDir2 * 10.0f, ForceMode.Impulse);
         
         }else{//apply damage force to NPC ragdoll if being damaged by an explosive object or other damage source without a specified attack direction
         
            Component[] bodies;
            bodies = dead.GetComponentsInChildren<Rigidbody>();
            foreach(Rigidbody body in bodies) {
               if(explosionCheck){
                  //if(body.transform.name == "Chest"){//only apply damage force to the chest of the ragdoll if damage is from non-player source
                     //calculate direction to apply damage force to ragdoll
                     body.AddForce((myTransform.position - (attackerPos2 + (Vector3.up * -2.5f))).normalized * Random.Range(4.5f, 7.5f), ForceMode.Impulse);
                  //}
               }else{
                  if(body.transform.name == "Chest"){//only apply damage force to the chest of the ragdoll if damage is from non-player source
                     //calculate direction to apply damage force to ragdoll
                     body.AddForce((myTransform.position - attackerPos2).normalized * 10.0f, ForceMode.Impulse);
                  }
               }
            }
         }
         
         //initialize the RemoveBody.cs script attached to the NPC ragdoll
         if(RemoveBodyComponent){
            if(removeBody){
               RemoveBodyComponent.enabled = true;
               RemoveBodyComponent.bodyStayTime = bodyStayTime;//pass bodyStayTime to RemoveBody.cs script
            }else{
               RemoveBodyComponent.enabled = false;
            }
         }
         
         //Determine if this object or parent should be removed.
         //This is to allow for different hit detection collider types as children of NPC parent.
         if(notParent){
            Destroy(transform.parent.gameObject);
         }else{
            Destroy(transform.gameObject);
         }
         
      }
   
   }
   
   static void CopyTransformsRecurse ( Transform src , Transform dst ){
      dst.position = src.position;
      dst.rotation = src.rotation;
      
      foreach(Transform child in dst) {
         // Match the transform with the same name
         Transform curSrc = src.Find(child.name);
         if (curSrc)
            CopyTransformsRecurse(curSrc, child);
      }
   }
}

NPC ATTACK

Código:

http://NPCAttack.cs by Azuline Studios©️ All Rights Reserved
//Manages timers for enemy attacks, damage application to other objects, and sound effects.
using UnityEngine;
using System.Collections;

public class NPCAttack : MonoBehaviour {
   private AI AIComponent;
   private   WeaponEffects WeaponEffectsComponent;
   private FPSRigidBodyWalker FPSWalker;
   private GameObject playerObj;

   private Transform myTransform;
   private Vector3 targetPos;
   [Tooltip("Maximum range of NPC attack.")]
   public float range = 100.0f;
   [Tooltip("Random range in units around target that NPC attack will hit (so NPCs won't have perfect aim).")]
   public float inaccuracy = 0.5f;
   [Tooltip("Fire rate of NPC attack.")]
   public float fireRate = 0.097f;
   [Tooltip("Number of attacks to fire in quick succession during NPC attack (for automatic weapons).")]
   public int burstShots = 0;
   [Tooltip("Maximum number of random shots to add to end of attack (so automatic weapons will fire different number of bullets per attack).")]
   public int randomShots = 0;
   [Tooltip("Physics force to apply to collider hit by NPC attack.")]
   public float force = 20.0f;
   [Tooltip("Damage to inflict per NPC attack.")]
   public float damage = 10.0f;
   [Tooltip("True if this is a melee attack (so actions like blocking can identify attack type).")]
   public bool isMeleeAttack;
   private float damageAmt;
//   public int bulletsPerClip = 50;
//   public int ammo = 150;
//   public float reloadTime = 1.75f;
   
   private bool doneShooting = true;
   private int shotsFired;
   private bool randBurstState;
   private int randShotsAmt;
   private bool  shooting = false;
   private bool  reloading = false;
   private bool  mFlashState = false;
   //private bool  noAmmoState = false;

   private Vector3 rayOrigin;
   private Vector3 targetDir;
   private RaycastHit[] hits;
   private bool hitObject;

   [Tooltip("Muzzle flash object to display during NPC attacks.")]
   public Renderer muzzleFlash;
   private Transform muzzleFlashTransform;
   
   [Tooltip("Sound effect for NPC attack.")]
   public AudioClip firesnd;
   [Tooltip("Random pitch chosen between this value and 1.0 for NPC attack sound variety.")]
   public float fireFxRandPitch = 0.86f;
   private AudioSource aSource;
//   public AudioClip reloadsnd;
//   public AudioClip noammosnd;
   
//   private int bulletsLeft = 0;
   
   private float shootStartTime = 0.0f;
   
   void OnEnable (){
      
      myTransform = transform;
      if (muzzleFlash){
         muzzleFlashTransform = muzzleFlash.transform;
      }
      AIComponent = myTransform.GetComponent<AI>();
      WeaponEffectsComponent = AIComponent.playerObj.GetComponent<FPSPlayer>().weaponObj.GetComponent<WeaponEffects>();
      playerObj = Camera.main.transform.GetComponent<CameraControl>().playerObj;
      FPSWalker = playerObj.GetComponent<FPSRigidBodyWalker>();
      aSource = GetComponent<AudioSource>();

      //bulletsLeft = bulletsPerClip;
      shootStartTime = -fireRate * 2;
   }
   
   void Update (){
   
      if(shootStartTime + fireRate < Time.time){
         shooting = false;
      }
      
      //fire more shots per attack if burstShots or randomShots is greater than zero
      if(!doneShooting && shotsFired < (burstShots + randShotsAmt)
         && (AIComponent.target == AIComponent.playerTransform || AIComponent.target == AIComponent.FPSWalker.leanObj.transform || (AIComponent.TargetAIComponent && AIComponent.TargetAIComponent.enabled))){
         Fire();   
         if(!randBurstState){//get random number of shots to add to this burst for variation
            randShotsAmt = Random.Range(0,randomShots);
            randBurstState = true;
         }
      }else{//reset burst shooting vars
         doneShooting = true;
         shotsFired = 0;
         randBurstState = false;
      }
      
   }
   
   void LateUpdate (){
   
      //enable muzzle flash
      if (muzzleFlash){
         if(Time.time - shootStartTime < fireRate * 0.33){
            if(mFlashState){
               muzzleFlash.enabled = true;
               mFlashState = false;
            }
         }else{
            if(!mFlashState){
               // We didn't, disable the muzzle flash
               muzzleFlash.enabled = false;
            }
         }
      }
   
   }
   
   public void Fire (){
   
//      if (bulletsLeft == 0){
//         return;
//      }
      
      //fire weapon
      if(!reloading){
         if(!shooting){
            FireOneShot();
            shootStartTime = Time.time;
            shooting = true;
            doneShooting = false;
         }else{
            if(shootStartTime + fireRate < Time.time){
               shooting = false;
            }
         }
      }else{
         shooting = false;
      }
   
   }
   
   void FireOneShot (){
      RaycastHit hit;
      if(AIComponent.TargetAIComponent){
         if(Vector3.Distance(myTransform.position, AIComponent.lastVisibleTargetPosition) > 2.5f){
            targetPos = new Vector3(AIComponent.lastVisibleTargetPosition.x + Random.Range(-inaccuracy, inaccuracy),
                              AIComponent.lastVisibleTargetPosition.y + Random.Range(-inaccuracy, inaccuracy),
                              AIComponent.lastVisibleTargetPosition.z + Random.Range(-inaccuracy, inaccuracy));
         }else{
            targetPos = new Vector3(AIComponent.lastVisibleTargetPosition.x,
                                      AIComponent.lastVisibleTargetPosition.y,
                                      AIComponent.lastVisibleTargetPosition.z);
         }
      }else{
         if(Vector3.Distance(myTransform.position, AIComponent.lastVisibleTargetPosition) > 2.5f){//only calculate inaccuracy if target is not in close range
            if(FPSWalker.crouched || FPSWalker.prone){
               targetPos = new Vector3(AIComponent.lastVisibleTargetPosition.x + Random.Range(-inaccuracy, inaccuracy),
                                       AIComponent.lastVisibleTargetPosition.y + Random.Range(-inaccuracy, inaccuracy),
                                       AIComponent.lastVisibleTargetPosition.z + Random.Range(-inaccuracy, inaccuracy));
            }else{
               targetPos = new Vector3(AIComponent.lastVisibleTargetPosition.x + Random.Range(-inaccuracy, inaccuracy),
                                       AIComponent.lastVisibleTargetPosition.y + Random.Range(-inaccuracy, inaccuracy),
                                       AIComponent.lastVisibleTargetPosition.z + Random.Range(-inaccuracy, inaccuracy))
                  + (AIComponent.playerObj.transform.up * AIComponent.targetEyeHeight);
            }
         }else{
            targetPos = new Vector3(AIComponent.lastVisibleTargetPosition.x,
                                    AIComponent.lastVisibleTargetPosition.y,
                                    AIComponent.lastVisibleTargetPosition.z);
         }
      }

      rayOrigin = new Vector3(myTransform.position.x, myTransform.position.y + AIComponent.eyeHeight, myTransform.position.z);
      targetDir = (targetPos - rayOrigin).normalized;
      if (muzzleFlashTransform){
         WeaponEffectsComponent.BulletTracers(targetDir, muzzleFlashTransform.position, -3.0f, 0.0f, false);
      }

      hitObject = false;
      hit = new RaycastHit();
      
      // Did we hit anything?
      hits = Physics.RaycastAll (rayOrigin, targetDir, Vector3.Distance(rayOrigin, targetPos), AIComponent.searchMask);
      
      //check if NPC can see their target
      for(int i = 0; i < hits.Length; i++){
         if(!hits[i].transform.IsChildOf(myTransform) ){
            hitObject = true;
            hit = hits[i];
            break;
         }
      }
      
      if (hitObject) {
         // Apply a force to the rigidbody we hit
         if (hit.rigidbody){
            hit.rigidbody.AddForceAtPosition(force * targetDir / (Time.fixedDeltaTime * 100.0f), hit.point);
         }
      
         if(hit.collider){
            //draw impact effects where the weapon hit
            if(hit.collider.gameObject.layer != 20 && hit.collider.gameObject.layer != 11){
               if(!isMeleeAttack){
                  WeaponEffectsComponent.ImpactEffects(hit.collider, hit.point, true, false, hit.normal);
               }else{
                  WeaponEffectsComponent.ImpactEffects(hit.collider, hit.point, true, true, hit.normal);
               }
            }
            
            //calculate damage amount
            damageAmt = Random.Range(damage, damage + damage);   
            
            //call the ApplyDamage() function in the script of the object hit
            switch(hit.collider.gameObject.layer){
               case 0://hit object is an apple
                  if(hit.collider.gameObject.GetComponent<AppleFall>()){
                     hit.collider.gameObject.GetComponent<AppleFall>().ApplyDamage(damageAmt);
                  }   
                  //hit object is a breakable or explosive object
                  if(hit.collider.gameObject.GetComponent<BreakableObject>()){
                     hit.collider.gameObject.GetComponent<BreakableObject>().ApplyDamage(damageAmt);
                  }else if(hit.collider.gameObject.GetComponent<ExplosiveObject>()){
                     hit.collider.gameObject.GetComponent<ExplosiveObject>().ApplyDamage(damageAmt);
                  }else if(hit.collider.gameObject.GetComponent<MineExplosion>()){
                     hit.collider.gameObject.GetComponent<MineExplosion>().ApplyDamage(damageAmt);
                  }
                  break;
               case 13://hit object is an NPC
                  if(hit.collider.gameObject.GetComponent<CharacterDamage>()){
                     hit.collider.gameObject.GetComponent<CharacterDamage>().ApplyDamage(damageAmt, Vector3.zero, myTransform.position, myTransform, false, false);
                  }
                  if(hit.collider.gameObject.GetComponent<LocationDamage>()){
                     hit.collider.gameObject.GetComponent<LocationDamage>().ApplyDamage(damageAmt, Vector3.zero, myTransform.position, myTransform, false, false);
                  }
                  break;
               case 11://hit object is player
                  if(hit.collider.gameObject.GetComponent<FPSPlayer>()){
                     hit.collider.gameObject.GetComponent<FPSPlayer>().ApplyDamage(damageAmt, myTransform, isMeleeAttack);
                  }   
                  if(hit.collider.gameObject.GetComponent<LeanColliderDamage>()){
                     hit.collider.gameObject.GetComponent<LeanColliderDamage>().ApplyDamage(damageAmt);
                  }   
                  break;
               case 20://hit object is player lean collider
                  if(hit.collider.gameObject.GetComponent<FPSPlayer>()){
                     hit.collider.gameObject.GetComponent<FPSPlayer>().ApplyDamage(damageAmt, myTransform, isMeleeAttack);
                  }   
                  if(hit.collider.gameObject.GetComponent<LeanColliderDamage>()){
                     hit.collider.gameObject.GetComponent<LeanColliderDamage>().ApplyDamage(damageAmt);
                  }   
                  break;
               default:
                  break;   
            }
         }
         
      }

      aSource.clip = firesnd;
      aSource.pitch = Random.Range(fireFxRandPitch, 1);
      if(aSource.volume > 0){
         aSource.PlayOneShot(aSource.clip, 0.8f / aSource.volume);
      }
      
      //track ammo and fired shots amount
      //bulletsLeft--;
      shotsFired++;

      mFlashState=true;
      enabled = true;
      
      // Reload gun in reload Time      
//      if (bulletsLeft == 0){
//         Reload();   
//      }
      
   }
   
//   IEnumerator Reload (){
//      
//      if(ammo > 0){
//         audio.volume = 1.0f;
//         audio.pitch = 1.0f;
//         audio.PlayOneShot(reloadsnd, 1.0f / audio.volume);
//         
//         reloading = true;
//         // Wait for reload time first, then proceed
//         yield return new WaitForSeconds(reloadTime);
//         //set reloading var in ironsights script to true after reloading delay
//         reloading = false;
//   
//         // We have ammo left to reload
//         if(ammo >= bulletsPerClip){
//            ammo -= bulletsPerClip - bulletsLeft;
//            bulletsLeft = bulletsPerClip;
//         }else{
//            bulletsLeft += ammo;
//            ammo = 0;
//         }
//      }
//      
//   }
   
//   private int GetBulletsLeft(){
//      return bulletsLeft;
//   }
}

ESSES ACIMA SÃO OS SCRIPS DO PROPRIO NPC AGORA TEM UM COMPLEMENTO CHAMADO "[ZOMBIE NPC]TRIGGER" QUE TEM AMIS UM SCRIPT> Monster Trigger:
Código:

http://MonsterTrigger.cs by Azuline Studios©️ All Rights Reserved
//used to spawn NPCs after player enters trigger to set up traps and ambushes
using UnityEngine;
using System.Collections;

public class MonsterTrigger : MonoBehaviour {
   [Tooltip("NPC objects to deactivate on level load and activate when player walks into trigger.")]
   public GameObject[] npcsToTrigger;
   
   void Start () {
      //deactivate the npcs in the npcsToTrigger array on start up
      for (int i = 0; i < npcsToTrigger.Length; i++){
         if(npcsToTrigger[i]){
            npcsToTrigger[i].SetActive(false);
         }
      }
   }

   void OnTriggerEnter ( Collider col  ){
      if(col.gameObject.tag == "Player"){
         //activate the npcs in the npcsToTrigger array when object is picked up/used by player
         for (int i = 0; i < npcsToTrigger.Length; i++){
            if(npcsToTrigger[i]){
               npcsToTrigger[i].SetActive(true);
            }
         }         
         Destroy(transform.gameObject);
      }
   }
}


ACREDITO QUE ESTE ULTIMO É PARA MOSTRAR ONDE O NPC DEVE NASCER.
Daniel Dória
Daniel Dória
Avançado
Avançado

Masculino PONTOS : 2164
REPUTAÇÃO : 31
Idade : 21
Respeito as regras : Configurar NPCs WvDYdlf

https://planecaos.jimdofree.com/

Ir para o topo Ir para baixo

DÚVIDA Re: Configurar NPCs

Mensagem por MarcosSchultz Seg Jul 30, 2018 4:47 pm

Não temos acesso ao asset, então não tem como saber o que pode estar errado... geralmente é falta de configuração no InputManager.

Tente ler a documentação ou mandar email diretamente para o criador do asset, informando os problemas, pois ele saberá o que é.
MarcosSchultz
MarcosSchultz
Administrador

Masculino PONTOS : 64340
REPUTAÇÃO : 2634
Idade : 26
Áreas de atuação : Administrador do fórum
Respeito as regras : Configurar NPCs Aad8pUi

https://www.schultzgames.com

Ir para o topo Ir para baixo

DÚVIDA Re: Configurar NPCs

Mensagem por Daniel Dória Seg Jul 30, 2018 10:47 pm

Cara então me mande seu email que upo o asset no drive e te envio para você ver e me ajudar, não existe uma documentação explicando como usa o produto e os desenvolvedores não respondem email[Você verá muitas reclamações desse tipo se olhar na loja].
Daniel Dória
Daniel Dória
Avançado
Avançado

Masculino PONTOS : 2164
REPUTAÇÃO : 31
Idade : 21
Respeito as regras : Configurar NPCs WvDYdlf

https://planecaos.jimdofree.com/

Ir para o topo Ir para baixo

DÚVIDA Re: Configurar NPCs

Mensagem por Tiago95 Ter Jul 31, 2018 9:44 am

E se vc tentar importar o seu projeto p dentro da scene q veio o asset q está funcionando?

Tiago95
Avançado
Avançado

PONTOS : 2018
REPUTAÇÃO : 32
Respeito as regras : Configurar NPCs WvDYdlf

Ir para o topo Ir para baixo

DÚVIDA Re: Configurar NPCs

Mensagem por Daniel Dória Qua Ago 01, 2018 12:02 pm

Tiago95 escreveu:E se vc tentar importar o seu projeto p dentro da scene q veio o asset q está funcionando?

Eu testei isso de varias maneiras e conclui que seja qual for o lugar que coloco o zumbi ou soldadinho mal para nascer ele sempre acaba nascendo no lugar onde seria o scene padrão do asset, mesmo que eu apague tudo daquele scene e bote o NPC em um lugar aleatório ele ficara parado nesse lugar fora da zona e quando eu for para o centro da scene ali estará ele flutuando como se estivesse caminhando em uma terra'in, e entrando em casas que não existem mais como se fosse uma área de spawn. Enfim, desisto desse asset estou migrando para o UNITz e ver se finalmente consigo um modo multiplayer ou boot funcional.

Daniel Dória
Daniel Dória
Avançado
Avançado

Masculino PONTOS : 2164
REPUTAÇÃO : 31
Idade : 21
Respeito as regras : Configurar NPCs WvDYdlf

https://planecaos.jimdofree.com/

Ir para o topo Ir para baixo

DÚVIDA Re: Configurar NPCs

Mensagem por Daniel Dória Seg Out 01, 2018 12:47 pm

Eu descobri nesses últimos dias que não importa qual for o asset de npc eles só funciona se a terrain estiver na posição padrão de quando inicia um projeto unity, se por exemplo arrastar ela para mais embaixo deixando a posicao y de 0 para -100 os npcs não se movem mais, a animação de movimentação as vezes podem até funcionar mas os players não se movem. Descobri também que eles atravessam paredes!
Alguem tem uma ideia para fazer funcionar isso seja na posição que for que a terra ou gameobject como chao estiver, e como fazer para não atravessarem paredes?
Daniel Dória
Daniel Dória
Avançado
Avançado

Masculino PONTOS : 2164
REPUTAÇÃO : 31
Idade : 21
Respeito as regras : Configurar NPCs WvDYdlf

https://planecaos.jimdofree.com/

Ir para o topo Ir para baixo

DÚVIDA Re: Configurar NPCs

Mensagem por Callyde Jr Ter Out 02, 2018 8:25 pm

Usando Layers e MeshColliders
Unity - Manual: Layers
Unity - Manual: Mesh Collider




Callyde Jr
Callyde Jr
Programador

Masculino PONTOS : 4537
REPUTAÇÃO : 442
Idade : 44
Áreas de atuação : Iniciante em modelagem Blender,Sketchup
Programador
Idealista

Meu Canal
https://www.youtube.com/ch
Respeito as regras : Configurar NPCs WvDYdlf

http://unnygames.000webhostapp.com/

Ir para o topo Ir para baixo

DÚVIDA Re: Configurar NPCs

Mensagem por Daniel Dória Qui Out 04, 2018 10:03 pm

Então se eu usar layers os inimigos do jogo vai funcionar normal mesmo se a escala do terreno estiver modificado? E quanto ao mesh collider eu ainda não vi o vídeo mas pelo que eu saiba colisores são colocados nos objetos, então um npc não detectaria ja que ele está configurado como um objeto animado que naturalmente quando se movimenta a animação atravessa aquele lugar como se nada existisse. Os npc como falei acima ele só funcionou no terreno na escala padrão de quando pega um terreno pronto de um projeto agora se eu junto esse terreno com um pedaco de Outro terreno quando o npc chegar aquele lugar ele não vai detectar o Outro terreno e vai atravessar o local, assim também com um objeto 3d ou qualquer outra coisa mesmo tendo terrain collider, box collider, mesh ou qualquer outro colisor tudo funciona normal para MEU PLAYER mas PARA OS NPCs nada adianta.
Daniel Dória
Daniel Dória
Avançado
Avançado

Masculino PONTOS : 2164
REPUTAÇÃO : 31
Idade : 21
Respeito as regras : Configurar NPCs WvDYdlf

https://planecaos.jimdofree.com/

Ir para o topo Ir para baixo

DÚVIDA Re: Configurar NPCs

Mensagem por Callyde Jr Sex Out 05, 2018 8:22 pm

Os NPCs se tiverem collisor eles nao atrevessam as paredes eles bate e ficam
Assets prontos sao bons para adiantar nossos projetos mais uns antigos ou novos as vezes vem bugs com scripts e outras coisas.
Eles tem que vim manual,suporte e videos para nos ajudar.
Uma vez baixei um asset de corridas de carros tive que deleta-lo ele veio todo em Java nao tinha suporte,videos nadinha.
Ia da um trabalho para arrumar que desisti e comecei fazer do zero um projeto desse vai demorar mais vale a pena.
Callyde Jr
Callyde Jr
Programador

Masculino PONTOS : 4537
REPUTAÇÃO : 442
Idade : 44
Áreas de atuação : Iniciante em modelagem Blender,Sketchup
Programador
Idealista

Meu Canal
https://www.youtube.com/ch
Respeito as regras : Configurar NPCs WvDYdlf

http://unnygames.000webhostapp.com/

Ir para o topo Ir para baixo

DÚVIDA Re: Configurar NPCs

Mensagem por Daniel Dória Qui Jan 24, 2019 3:06 pm

Callyde Jr escreveu:Os NPCs se tiverem collisor eles nao atrevessam as paredes eles bate e ficam... 
Voltando aqui porque descobri que para os npc não atravessar paredes é necessário ter navmesh obstacle nos objetos, ele funciona como um box collider que só colide entre eles mesmo. 
E quanto ao problema deles não se movimentar descobri que eles só funciona em um terreno sem modificação na escala se tiver mais de um terreno ele vai ignorar e atravessar ou se coloca-los em cima de um objeto continuará sem movimentos um dia estudarei sobre isso também ...
Daniel Dória
Daniel Dória
Avançado
Avançado

Masculino PONTOS : 2164
REPUTAÇÃO : 31
Idade : 21
Respeito as regras : Configurar NPCs WvDYdlf

https://planecaos.jimdofree.com/

Ir para o topo Ir para baixo

DÚVIDA Re: Configurar NPCs

Mensagem por ProBrStalker Dom Jun 20, 2021 4:13 pm

Daniel Dória escreveu:
Callyde Jr escreveu:Os NPCs se tiverem collisor eles nao atrevessam as paredes eles bate e ficam... 
Voltando aqui porque descobri que para os npc não atravessar paredes é necessário ter navmesh obstacle nos objetos, ele funciona como um box collider que só colide entre eles mesmo. 
E quanto ao problema deles não se movimentar descobri que eles só funciona em um terreno sem modificação na escala se tiver mais de um terreno ele vai ignorar e atravessar ou se coloca-los em cima de um objeto continuará sem movimentos um dia estudarei sobre isso também ...
Bom, vamos lá, boa tarde, eu possuo +/- quatro anos de experiencia com o RFPS, e embora este tópico seja antigo, tudo o que você precisa fazer, é criar a NavMesh do terreno, e definir as casas, paredes, murros etc como objetos não andáveis, para que eles não atravessem o que você deseja....
Se eu não me engano, todas as documentações de cada versão do RFPS falam isso...eu li apenas até a 1.9...(não cheguei a ler mais sobre as novas...)
espero ter ajudado =)

*NavMesh obstacle serve apenas para objetos como barris, caixas, etc
ProBrStalker
ProBrStalker
Avançado
Avançado

Masculino PONTOS : 2251
REPUTAÇÃO : 12
Respeito as regras : Configurar NPCs WvDYdlf

https://redskygamesstudios.000webhostapp.com/

Ir para o topo Ir para baixo

Ir para o topo

- Tópicos semelhantes

 
Permissões neste sub-fórum
Não podes responder a tópicos