Hello Guys,
I am new to unity and i am having some problem with my enemy script, i made my enemy in a way that it has a trigger collider attached to it and it my player comes into the trigger the enemy shoots the player. Unfortunately, when i have only one instance of my enemy everything works great, but when i duplicate my enemy they do nothing (they walk fine but not shoot player).
Here are my scripts:
Script for Enemy Finding player
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OctrokFindingPlayer : MonoBehaviour {
public bool playerInSight;
public void OnTriggerEnter2D (Collider2D other)
{
if(other.gameObject.tag == "Player") {
playerInSight = true;
}
}
public void OnTriggerStay2D (Collider2D other){
if(other.gameObject.tag == "Player") {
playerInSight = true;
}
}
public void OnTriggerExit2D (Collider2D other){
if(other.gameObject.tag == "Player"){
playerInSight = false;
}
}
}
Script for enemy shooting player
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OctorokThrowingRocks : MonoBehaviour
{
private Rigidbody2D rbody;
private Animator anim;
private OctrokFindingPlayer octrokSpottingPlayer;
public Transform throwPosition;
public GameObject rockThrowObject;
public GameObject player;
public float ThrowWaitTime;
public float ThrowTimer;
// Use this for initialization
void Start (){
rbody = GetComponent();
anim = GetComponent();
player = GameObject.FindWithTag ("Player");
octrokSpottingPlayer = FindObjectOfType();
}
void Update()
{
// If Player is Sighted
if (octrokSpottingPlayer.playerInSight)
{
GameObject projectile = (GameObject)Instantiate(rockThrowObject);
projectile.transform.position = throwPosition.position;
}
}
}
Please help me, Thanks in advance
( octorok is name of my enemy )
↧