Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

Unity2D Need Help in Multiple Enemies Shooting Script

$
0
0
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 )

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>