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

Problem spawning prefab enemy

$
0
0
So I am currently trying to create a script to spawn enemies every time the character respawns, but every time a clone of the prefab gets created the transform it should point to (The player) does not get updated. Does anyone have an idea on how to go about this? Thanks in advance HERE IS THE CODE FOR MY NPC: using UnityEngine; using System.Collections; public class RatController : MonoBehaviour { /********** NPC's VARIABLES **********/ float startingPos; public Animator ratAnim; public int damageValue = 1; public int enemyLife = 3; bool facingRight = true; float endPos; public int unitToMove = 5; public int moveSpeed = 4; bool moveRight = true; static float gracePeriod = 0; public Transform playersTransform; bool onCyclicMovement; public AudioClip bloodSound; float move; public Transform inmap; /********** INITIALIZATION ***********/ void Awake () { startingPos = transform.position.x; endPos = startingPos + unitToMove; } void Update(){ /********** FOLLOW CHARACTER IF CLOSE ENOUGH **********/ transform.LookAt(playersTransform); if(Vector3.Distance(transform.position,playersTransform.position) < 7f){ onCyclicMovement = false; transform.position += transform.forward*(moveSpeed * 2)*Time.deltaTime; transform.position = new Vector3(transform.position.x,transform.position.y,-5f); move = (transform.position.x < playersTransform.position.x )? 1f : -1f; }else { onCyclicMovement = true; } transform.rotation = Quaternion.Euler(0, 0, 0); if (onCyclicMovement){ if(moveRight){ rigidbody2D.velocity = new Vector2 (moveSpeed, rigidbody2D.velocity.y); } if (transform.position.x >= endPos) { moveRight = false; } if(!moveRight){ rigidbody2D.velocity = new Vector2 (-moveSpeed, rigidbody2D.velocity.y); } if (transform.position.x <= startingPos) { moveRight = true; } } /********** NPC's GRACE PERIOD **********/ if (gracePeriod > 0) { this.GetComponent ().enabled = !this.GetComponent ().enabled; gracePeriod -= Time.deltaTime; } else { this.GetComponent ().enabled = true; } } /********** COLLLIDER ATTACK **********/ void OnTriggerEnter2D (Collider2D other){ if (other.gameObject.tag == "character" && GameManager.gracePeriod <= 0) { GameManager.playersHealth -=damageValue; GameManager.gracePeriod = 2.0f; } } //Set the animator properties void Start () { ratAnim = GetComponent(); } /********** NPC's MOVEMENT **********/ void FixedUpdate () { ratAnim = GetComponent(); if (onCyclicMovement) move = rigidbody2D.velocity.x; ratAnim.SetFloat ( "S", Mathf.Abs(move)); if (move > 0 &&!facingRight) Flip (); else if (move < 0 && facingRight) Flip (); } void Flip() { facingRight = !facingRight; Vector4 theScale = transform.localScale; theScale.x *= -1; transform.localScale = theScale; } void GracePeriod(float passedTime){ gracePeriod = passedTime; } IEnumerator Die(){ ratAnim.SetBool ("dead", true); yield return new WaitForSeconds (0.01f);; ratAnim.SetBool ("dead", false); AudioSource.PlayClipAtPoint(bloodSound, transform.position,2f); yield return new WaitForSeconds (0.5f); Destroy (gameObject); } void EnemyDamaged(int damage){ if (enemyLife > 0) enemyLife --; if (enemyLife <= 0) { enemyLife = 0; gracePeriod = 0f; StartCoroutine (Die()); } } } HERE IS THE CODE I US FOR RESPAWN OF THE NPC: /********** RESPOND NPC'S SCRIPT **********/ public void RespondDeadNpcs(){ foreach(RespawnableNPCs npc in respawnableNPCs){ if(!npc.npcIsAlive){ RatController bPrefab = Instantiate (( npcObjectPrefabs[0] ), npc.npcLocation.position, Quaternion.identity) as RatController; bPrefab.playersTransform = playerT; } } } `

Viewing all articles
Browse latest Browse all 1488

Trending Articles



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