- Home /
Why does only one object respond, while more objects have exact same script?
I have some enemies who are suppose to attack when a certain amount of triggerpoints have been reached. The triggerpoints are added by collision of player and item which is held in a different script.
They start Unaware, always facing the player with their backs. If the player colliders with an item 2x they should become alerted (this is kept track of with the triggerpoints). If they continue to collide with items they will attack.
I have the same script on my enemy, and i have several of them spread throught the rooms. The problem is only one of them looks at the player when they switch to alerted.
[code] using UnityEngine; using System.Collections;
public class EnemyAI : MonoBehaviour { public enum MonsterState // de states { unaware, alerted, attack
} ;
//______variabelen________________________
public Transform target; // vergeet niet de player een tag te geven
public int moveSpeed;
public int maxDistance;
public int medDistance;
public int minDistance;
public MonsterState gameState; // dit is een variable om de state aan te roepen als parameter ofzo
private Transform myTransform;
private CharacterController controller; // anders beweegt ie niet, is als component aan de enemy toegevoegd
private int triggerPoint;
private int unawareTrigger;
private int alertedTrigger;
private int attackTrigger;
public int deKamer;
// audio
public AudioClip au_suspense;
private bool switchState;
private bool tooclose;
private float monstCooldown;
private float monstTimer;
public int kamerNummer;
public AudioClip au_monsterClose;
//_____awake______________________________
void Awake ()
{
myTransform = transform; // wordt gecached zodat hij altijd de transform info heeft
controller = GetComponent();
}
// _ OnGUID __________________________
// void OnGUI () // { // // GUI.Label (new Rect(10,110,300,30),"Triggers: " + triggerPoint + "/4"); //dit laat de score zien in tekst // //
// }
//_____start______________________________
void Start ()
{
//wanneer hetspel begint vindt de enemy de speler eerst
GameObject go = GameObject.FindGameObjectWithTag ("Player"); // tag de enemy en de player in inspector
target = go.transform; // dit is weer een variable die later wordt gebruikt en hier wordt de target opgeslagen van de findtag //go is de naam van de gameobject
minDistance = 2;
medDistance = 3; // dit is een andere distance omdat ie anders vanaf alerted meteen switched naar attack, nu geven we de speler een kans terug te gaan
maxDistance = 4; //zo blijven ze op afstand
unawareTrigger = 1;
attackTrigger = 4;
alertedTrigger = attackTrigger-1;
switchState = true;
moveSpeed = 1;
monstTimer = 0;
monstCooldown = 5.0f;
tooclose = true;
controller = GetComponent ();// dit is nodig omdat deze variabele straks wordt gebruikt, en dit roept het aan
}
//_____update_____________________________
void Update ()
{
if (monstTimer > 0)
monstTimer -= Time.deltaTime; // every time attacktimer is greater then 0 it will cooldown in that time
if (monstTimer < 0)
monstTimer = 0;
if (monstTimer == 0)
tooclose = true;
deKamer = GameObject.Find("De_checker").GetComponent().deKamer();
//if toggle is on
//de states worden continue gechecked of ze van toepassing zijn
switch (gameState)
{
case MonsterState.unaware: // let op dubbele punt niet punt komma
Unaware (); // hier wordt de functie ingevoerd die actief is in deze state, voer hier alle andere functies en if statements in
break;
case MonsterState.alerted:
Alerted ();
break;
case MonsterState.attack:
Attack ();
break;
}
}
// de triggerpoint unawareTrigger =1; alertedTrigger = attacktrigger -1; attackTrigger = 4; totalTrigger = 0;
public void Reset()
{
triggerPoint = 0;
Debug.Log ( "Doet dit het");
}
public void TriggerCount ()
{
triggerPoint++;
// sound feedback 1x played
if (triggerPoint == unawareTrigger+1 && switchState == true)
{
audio.PlayOneShot(au_suspense);
audio.PlayOneShot(au_monsterClose);
switchState = false;
}
if ( triggerPoint == attackTrigger && switchState == true)
{
audio.PlayOneShot(au_suspense);
audio.PlayOneShot(au_monsterClose);
switchState = false;
}
// trigger points which switch to states
if(triggerPoint > unawareTrigger && triggerPoint < attackTrigger) // 2 3
{
gameState = MonsterState.alerted;
Debug.Log ("switch to alerted");
switchState = true;
}
else if(triggerPoint > alertedTrigger ) // 4 +
{
gameState = MonsterState.attack;
Debug.Log ("switch to attack");
}
else // 0 1 2
{
gameState = MonsterState.unaware;
Debug.Log ("switch to unaware");
}
}
private void Unaware()
{
//idle
// de movement tijdens unaware - monster blijft vanaf maxdistance altijd met zijn rug naar de speler
if (Vector3.Distance (target.position, myTransform.position) < medDistance)
{
if (tooclose == true)
{
audio.PlayOneShot(au_monsterClose);
monstTimer = monstCooldown;
tooclose = false;
}
Vector3 moveDirection = target.transform.position - transform.position;
moveDirection.Normalize ();
moveDirection.y = 0;
transform.LookAt (transform.position + moveDirection);
gameState = MonsterState.alerted;
}
// if the distance is greater the player wont be noticed
if (Vector3.Distance (target.position, myTransform.position) > maxDistance)
{
Vector3 moveDirection = target.transform.position - transform.position;
moveDirection.Normalize ();
moveDirection.y = 0; // zo gaat ie niet draaien
transform.LookAt (transform.position - moveDirection);
}
}
private void Alerted()
{
// kijkt naar de speler - maakt geluid - muziek wordt spannender - licht en donker?
Vector3 moveDirection = target.transform.position - transform.position;
moveDirection.Normalize ();
moveDirection.y = 0; // zo gaat ie niet draaien
transform.LookAt (transform.position + moveDirection);
// het monster kijkt al naar de speler: als de speler nu nog dichterbij komt dan gaat ie aanvallen
if (Vector3.Distance (target.position, myTransform.position) < minDistance || Vector3.Distance (target.position, myTransform.position) < medDistance && GameObject.Find("PlayerSpotLight").GetComponent().lampcheck() )
{
gameState = MonsterState.attack;
}
}
private void Attack ()
{
// dit berekent de move tussen enemy en player - bepalend op de maxdistance
// de movement tijdens alerted - het monster gaat naar de speler toe (helaas tot op zijn hoofd)
if (deKamer == 2 || deKamer == 5)
{
Vector3 moveDirection = target.transform.position - transform.position;
moveDirection.Normalize ();
moveDirection.y = 0; // zo gaat ie niet draaien
controller.Move (moveDirection * moveSpeed * Time.deltaTime); // hier vlgt/beweegt ie de controller functie boven aangeven met getcomponent
transform.LookAt (transform.position + moveDirection); //hierdoor blijft ie gericht op de Lookat target en draaitt ie langzaam die richting op
}
}
}
[/code]
Theres comments in dutch.. sorry...
im not actually using the last section where it says (de$$anonymous$$amer == 2 || de$$anonymous$$amer == 5 ) I just put that there to test so they might activate in the rooms (dekamer = theroom). The test failed btw.
So to recap the question:
Why does only one gameobject reacts on the script while they all have the exact same script ( theyre even prefabs but i now broke the prefab conncetion to see if that works. )
Your answer
Follow this Question
Related Questions
Switch() problem [C#] 1 Answer
Space Shooter Hazards Not Spawning 0 Answers
Distribute terrain in zones 3 Answers