I'm trying to have an enemy circle the player and shoot bullets at said player. Whenever the enemy shoots though, the bullet is slightly off from the player and continues to shoot further from the players position.
this is the enemies behaviour
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CirclingScript : MonoBehaviour, IPooledObject
{
public Transform tf;
public Transform Target;
public GameObject Ship;
public GameObject WaveMaker;
public EnemyScript enemyscript;
public string BulletName;
ObjectPools objectPooler;
private int I; // Wave number
public int BulletAmount;
public float angle;
public float Radius;
public float RotateSpeed;
private float Timer;
public float Health;
private Quaternion BulletRot;
public void OnObjectSpawn()
{
WaveMaker = GameObject.Find("EnemyWaveMaker");//gets the game object
enemyscript = WaveMaker.GetComponent<EnemyScript>();// gets the scripts for the wave makers
I = enemyscript.i;
Ship = GameObject.Find("Ship");//gets the game object
Target = Ship.GetComponent<Transform>();// gets the Transform
//https://answers.unity.com/questions/1068513/place-8-objects-around-a-target-gameobject.html thank you ^-^
angle = enemyscript.RotSpace * Mathf.PI * 2f / enemyscript.Waves[I].Amount;
RotateSpeed = enemyscript.Waves[I].RotateSpeed;
Radius = enemyscript.Waves[I].Radius;
}
void Start()
{
objectPooler = ObjectPools.Instance;
}
void OnTriggerEnter2D(Collider2D coll)
{
if (coll.gameObject.CompareTag("Bullet"))
{
Health -= coll.GetComponent<DamageScript>().Damage;
}
if (Health <= 0)
{
objectPooler.SpawnFromPool("Boom", tf.position, Quaternion.identity);
gameObject.SetActive(false);
}
}
// Update is called once per frame
void Update()
{
angle += RotateSpeed * Time.deltaTime;
Timer += Time.deltaTime;
Vector2 offset = new Vector2(Mathf.Sin(angle), Mathf.Cos(angle)) * Radius;
tf.position = (Vector2)Target.position + offset;
if (Timer >= 2f)
{
Vector3 difference = Target.position - tf.position;
float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
BulletPatternsModule.ShootArc(angle, BulletAmount, BulletName, tf, rotationZ);
Timer = 0f;
}
}
}
and the code for shooting the bullets
public static void ShootArc(float ArcSize, int BulletAmount, string BulletName, Transform tf, float Offset)//All arcs are in angles, not radians
{
float angle = 0;
angle = Offset;//Offset is to the left
for (int i = 0; i < BulletAmount; i++)
{
float AngleStep = ArcSize / BulletAmount;//Gets the step size for arc
angle += AngleStep;
objectPooler.SpawnFromPool(BulletName, tf.position, Quaternion.Euler(0, 0, angle));//Shoots the bullet
}
}
The offset makes the enemies near useless
Any help would be appreciated :) (more text in order for me to post the question. Isn't this fun?)
It will be easier to make the enemy look at the player using this function
transform.LookAt(target.position);