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

Boss ai help EoW

$
0
0
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(); 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(); 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; } } } }

Viewing all articles
Browse latest Browse all 1488

Trending Articles