Boss ai help EoW
I've looked around, but I can't quite seem to get this working:
I'm trying to make an Eater of worlds inspired worm boss, from terraria. I've followed a couple tutorials and put things together as I believe it should work, but when I add the script to an object, nothing happens.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Worm : MonoBehaviour
{
public Transform player;
public float moveSpeed;
private Rigidbody2D rb;
private Vector2 movement;
public int attackDamage;
public float dashTime;
private float dashTimer;
public float dashSpeed;
public float attackSpeed;
public float rotateSpeed;
public float radius;
private float angle;
private Vector2 centre;
public int closeToCircle;
[Space]
private float stateChangeTime;
public float stateChangeTimeMin;
public float stateChangeTimeMax;
private int randomNumber;
[Space]
public bool followState;
public bool attackState;
public bool circleState;
public bool dashState;
// Start is called before the first frame update
void Start()
{
rb = this.GetComponent<Rigidbody2D>();
randomNumber = Random.Range(1, 5);
stateChangeTime = Random.Range(stateChangeTimeMin, stateChangeTimeMax) + Time.time;
centre = transform.position;
}
void onTriggerEvent(Collider2D other)
{
if (other.gameObject.tag == "Player")
{
PlayerHealth playerHealth = other.gameObject.GetComponent<PlayerHealth>();
if (playerHealth != null)
{
playerHealth.TakeDamage(attackDamage);
}
}
}
// Update is called once per frame
void Update()
{
if (randomNumber == 1)
{
followState = true;
attackState = false;
circleState = false;
dashState = false;
}
else if (randomNumber == 2)
{
followState = false;
attackState = true;
circleState = false;
dashState = false;
}
else if (randomNumber == 3 && ((player.transform.position - this.transform.position).sqrMagnitude < closeToCircle * closeToCircle))
{
followState = false;
attackState = false;
circleState = true;
dashState = false;
}
else if (randomNumber == 3)
{
followState = true;
attackState = false;
circleState = false;
dashState = false;
}
else if (randomNumber == 4)
{
followState = false;
attackState = false;
circleState = false;
dashState = true;
}
Vector3 direction = player.position - transform.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
rb.rotation = angle;
direction.Normalize();
movement = direction;
angle += rotateSpeed * Time.deltaTime;
}
private void FixedUpdate()
{
follow(movement);
}
void follow(Vector2 direction)
{
if (followState == true)
{
rb.MovePosition((Vector2)transform.position + (direction * moveSpeed * Time.deltaTime));
if (stateChangeTime < Time.time)
{
randomNumber = Random.Range(1, 5);
stateChangeTime = Random.Range(stateChangeTimeMin, stateChangeTimeMax) + Time.time;
}
}
}
void dash(Vector2 direction)
{
if (dashState == true)
{
dashTimer = dashTime + Time.time;
rb.MovePosition((Vector2)transform.position + (direction * moveSpeed * Time.deltaTime));
if (dashTime < Time.time)
{
rb.constraints = RigidbodyConstraints2D.FreezeRotation;
rb.MovePosition((Vector2)transform.position + (direction * moveSpeed * dashSpeed * Time.deltaTime));
}
else
{
rb.constraints = RigidbodyConstraints2D.None;
}
if (stateChangeTime < Time.time)
{
randomNumber = Random.Range(1, 5);
stateChangeTime = Random.Range(stateChangeTimeMin, stateChangeTimeMax) + Time.time;
}
}
}
void attack(Vector2 direction)
{
if (attackState == true)
{
rb.MovePosition((Vector2)transform.position + (direction * moveSpeed * attackSpeed * Time.deltaTime));
//spray bullets or fire lazer
if (stateChangeTime < Time.time)
{
randomNumber = Random.Range(1, 5);
stateChangeTime = Random.Range(stateChangeTimeMin, stateChangeTimeMax) + Time.time;
}
}
}
void circle(Vector2 direction)
{
if (circleState == true)
{
var offset = new Vector2(Mathf.Sin(angle), Mathf.Cos(angle)) * radius;
transform.position = centre + offset;
if (stateChangeTime < Time.time)
{
randomNumber = Random.Range(1, 5);
stateChangeTime = Random.Range(stateChangeTimeMin, stateChangeTimeMax) + Time.time;
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
How to have multiple enemies (currently having to kill them in order) 1 Answer
Enemy animation gets weird when going to set position 0 Answers
Trying to add a tracker to enemy AI either prefab or with reference 0 Answers
I can't get a "victim" (can be seen as enemy) to follow the player within a certain range 0 Answers