Hi,
I have being stuck on this and I can't seem to figure it out.
Whenever I click on the enemy the enemy will die and re-spawn, however all enemies seem to do this.
I m not sure why this might be happening, I have copied the AI code in below.
Thanks in advance :D
using UnityEngine;
using System.Collections;
public class AIc : MonoBehaviour {
public Transform target;
int moveSpeed = 3;
public int MinDist = 5;
float maxDist;
float swaySpeed = 2.0f;
float swayMagnitude = 0.15f;
SpawnPoint[] spawnPoints;
// Use this for initialization
void Start () {
spawnPoints = GameObject.FindObjectsOfType ();
maxDist = target.renderer.bounds.size.y;
}
// Update is called once per frame
void FixedUpdate () {
try
{
Attack ();
if(Input.GetMouseButtonDown(0))
{
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if(hit.collider.name == "Enemy")
{
Score.AddPoint();
Respawn();
}
else if(hit.collider.name == "HealthPowerUp")
{
Health.IncreaseHealth();
}
}
}
catch
{
Debug.Log("An error has occured");
}
}
///
/// Checks if the enemy is not in the flower square
/// If not then calls attack method
///
public void Attack()
{
transform.LookAt (target);
if (Vector2.Distance (transform.position, target.position) > maxDist)
{
moveEnemy();
}
else
{
transform.rotation = new Quaternion (transform.rotation.x, 0.0f, transform.rotation.z, 1.0f);
}
}
///
/// Respawns the enemy in a new location based on number of spawn points
///
public void Respawn()
{
swaySpeed = Random.Range (2.0f, 2.75f);
swayMagnitude = Random.Range (0.05f,0.25f);
SpawnPoint mySpot = spawnPoints [Random.Range (0, spawnPoints.Length)];
this.transform.position = mySpot.transform.position;
this.transform.rotation = mySpot.transform.rotation;
}
///
/// Sets the enemy position and forces the enemy towards the flower
/// Sway from left to right would need to be fixed
///
public void moveEnemy()
{
Vector3 temp = this.transform.position;
temp.x = transform.position.x;
temp.x = temp.x + Mathf.Sin (Time.time * swaySpeed) * swayMagnitude;
//Work in progress
//temp.y = transform.position.y;
//temp.y = temp.y + Mathf.Sin(Time.time * 0.05f) * 0.001f;
//Sets the current enemy position
this.transform.position = new Vector3 (temp.x, transform.position.y, transform.position.z);
this.transform.position += transform.forward * moveSpeed * Time.deltaTime;
this.transform.rotation = new Quaternion (transform.rotation.x, 0.0f, transform.rotation.z, 1.0f);
}
///
/// If the enemy trigger is touching the flower calls damage script for flower
/// May re position this method
///
/// Other.
void OnTriggerStay2D(Collider2D other)
{
if (other.gameObject.name == "Flower") {
//Debug.Log("Working... kinda");
Health.Damage();
}
}
}
↧