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

Enemy BulletSpread

$
0
0
Hey Im developing a top down rpg and am currently making an AI and I need to make the bullets spread like a shotgun. I've watched many tutorials but those only explain how to make this for the player, and it doesnt work for me. Sorry in advance about the comments if u cant understand them This is where I have tried to make the bullet spread using System.Collections; using System.Collections.Generic; using UnityEngine; public class GolemFollowPlayer : MonoBehaviour { private float speed; public float lineOfSite; private Transform player; public float shootingRange; public GameObject bullet; public GameObject bulletParent; public float fireRate = 1f; private float nextFireTime; private bool right = true; public Animator animator; public Golem golem; public int bulletCount; public float spreadAngle; List bullets; public float bulletSpeed; void Awake() { bullets = new List(bulletCount); for (int i = 0; i < bulletCount; i++) { bullets.Add(Quaternion.Euler(Vector2.zero)); } } //Esta função é chamada logo que o script é ativado antes de qualquer função de update void Start() { //Encontrar o player player = GameObject.FindGameObjectWithTag("Player").transform; } //Esta função é executada em cada frame void Update() { do { //Variável igual à distância entre o player e o inimigo float distanceFromPlayer = Vector2.Distance(player.position, transform.position); //Para o inimigo começar a andar a partir de uma certa distância com o player if (distanceFromPlayer < lineOfSite && distanceFromPlayer > shootingRange) { //nova velocidade do inimigo speed = 0.7f; //Inimigo começa a andar em direção ao player transform.position = Vector2.MoveTowards(this.transform.position,player.position,speed * Time.deltaTime); } //Para o inimigo parar e poder disparar a partir de uma certa distância com o player else if(distanceFromPlayer <= shootingRange & nextFireTime < Time.time) { fire(); nextFireTime = Time.time + fireRate; } //Para que o inimigo não tenha nenhuma animação enquanto estiver a uma certa distância do player else if(distanceFromPlayer > lineOfSite) { //nova velocidade do inimigo speed = 0f; } //Para rodar o inimigo no eixo do y dependendo da posição do player e do inimigo if(player.position.x > transform.position.x && !right) { flip(); } else if (player.position.x < transform.position.x && right) { flip(); } //Velocidade do inimigo é igual a um parâmetro da animação animator.SetFloat("enemySpeed", speed); }while (golem.golemHealth > 0); } //Função para desenhar as circunferências de detecção do inimigo private void OnDrawGizmosSelected() { //Cor da circunferência Gizmos.color = Color.green; //Circunferência de detecção para começar a andar Gizmos.DrawWireSphere(transform.position, lineOfSite); //Circunferência de detecção para parar e disparar Gizmos.DrawWireSphere(transform.position, shootingRange); } //Função para rodar no eixo do y void flip() { //Igual ao seu contrário right = !right; //Rodar 180 graus no eixo do y transform.Rotate(0f, 180f, 0f); } void fire() { for (int i = 0; i < bulletCount; i++) { float ang = i * spreadAngle * 3; float random = Random.Range(1, 3); GameObject p = Instantiate(bullet, bulletParent.transform.position, Quaternion.Euler(ang, ang, ang)); p.GetComponent().AddForce(bulletParent.transform.right * bulletSpeed, ForceMode2D.Impulse); if(i > 0) { p.GetComponent().AddForce(p.transform.position * random); } } } } This is my bullet script using System.Collections; using System.Collections.Generic; using UnityEngine; public class GolemBullet : MonoBehaviour { //Declaração de variáveis GameObject target; public float speed; Rigidbody2D bulletRB; public Collider2D col2d; public Animator animator; //Esta função é chamada logo que o script é ativado antes de qualquer função de update void Start() { //Inicializar o rigidbody da bala bulletRB = GetComponent(); //Encontrar o alvo target = GameObject.FindGameObjectWithTag("Player"); } //Função de quando se entra em colisão private void OnCollisionEnter2D(Collision2D collision2D) { //Ao entrar numa colisão com o player if (collision2D.gameObject.tag == "Player") { //Dano ao player através de um evento collision2D.gameObject.GetComponent().TakeDamage(1); //Destruir o collider Destroy(col2d); //Bool é true animator.SetBool("onImpact", true); //Destruir o rigidbody Destroy(GetComponent()); //Destruir a bala com um delay de 0.3 segundos this.Wait(0.3f, Delete); } //Ao entrar em colisão com a parede if (collision2D.gameObject.tag == "Walls") { //Bool é true animator.SetBool("onImpact", true); //Destruir o rigidbody Destroy(GetComponent()); //Destruir a bala com um delay de 0.3 segundos this.Wait(0.3f, Delete); } } //Função de destruir a bala void Delete() { //Destruir a bala Destroy(gameObject); } }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



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