Hello! So I’m trying to make a top down shooter controller that uses object pooling for the bullet prefabs, some of the scripts are from this tutorial:
I’m failing to make the calculations of the Euler Angles correctly, the PlayerBulletPoolAttack function is the one that I need to make some change. I need it to calculate the startAngle and endAngle relative to the players mouse position, how can I do that??
I’m using these scripts:
BulletPoolScript
public class BulletPool : MonoBehaviour
{
public static BulletPool bulletPoolInstance;
[SerializeField]
public GameObject pooledBullet;
private bool notEnoughBulletsInPool = true;
private List<GameObject> bullets;
private void Awake()
{
bulletPoolInstance = this;
}
void Start()
{
bullets = new List<GameObject>();
}
public GameObject GetBullet()
{
if(bullets.Count > 0)
{
for(int i = 0; i < bullets.Count; i++)
{
if (!bullets*.activeInHierarchy)*
{
return bullets*;*
}
}
}
if (notEnoughBulletsInPool)
{
GameObject bul = Instantiate(pooledBullet);
bul.SetActive(false);
bullets.Add(bul);
return bul;
}
return null;
}
}
## PlayerBulletScript ##
public class DefaultPlayerProjectile : MonoBehaviour
{
private Vector2 moveDirection;
public float moveSpeed = 200f;
private Rigidbody2D rb2D;
private void Start()
{
rb2D = GetComponent();
}
private void FixedUpdate()
{
rb2D.velocity = moveDirection * moveSpeed * Time.deltaTime;
}
public void SetMoveDirection(Vector2 dir)
{
moveDirection = dir;
}
private void DestroyPoolObject()
{
gameObject.SetActive(false);
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag(“Enemy”))
{
Debug.log(“Enemy Hit!”);
}
DestroyPoolObject();
}
}
## PlayerAttackFunction ##
[SerializeField]
private int bulletsAmount = 10;
[SerializeField]
private float startAngle = 0f, endAngle = 0f, centerAngle = 0f;
private Vector2 bulletMoveDirection;
private void Awake()
{
instance = this;
}
private void Update()
{
centerAngle = (PlayerAimController.instance.weaponRotation.eulerAngles.z);
}
public void PlayerBulletPoolAttack(Transform firePoint)
{
float angleStep = (endAngle - startAngle) / bulletsAmount;
float angle = startAngle;
for (int i = 0; i < bulletsAmount + 1; i++)
{
float bulDirX = transform.position.x + Mathf.Sin((angle * Mathf.PI) / 180f);
float bulDirY = transform.position.y + Mathf.Cos((angle * Mathf.PI) / 180f);
Vector3 bulMoveVector = new Vector3(bulDirX, bulDirY, 0f);
Vector2 bulDirection = (bulMoveVector - transform.position).normalized;
GameObject bul = BulletPool.bulletPoolInstance.GetBullet();
bul.transform.position = firePoint.position;
bul.transform.rotation = firePoint.rotation;
bul.SetActive(true);
bul.GetComponent().SetMoveDirection(bulDirection);
angle += angleStep;
}
}
##PlayerAimController##
public class PlayerAimController : MonoBehaviour
{
public static PlayerAimController instance;
public bool isFlipped;
private Camera mainCamera;
public Transform weaponRotationPoint;
private void Awake()
{
instance = this;
}
private void Start()
{
mainCamera = Camera.main;
}
private void Update()
{
GetPlayerAimInput();
}
void GetPlayerAimInput()
{
Vector3 mousePos = Input.mousePosition;
Vector3 screenPoint = mainCamera.WorldToScreenPoint(transform.localPosition);
//ROTATING THE PLAYER
if (mousePos.x < screenPoint.x)
{
transform.localScale = new Vector3(-1f, 1f, 1f);
weaponRotationPoint.localScale = new Vector3(-1f, -1f, 1f);
isFlipped = true;
}
else
{
transform.localScale = Vector3.one; // transform.localScale = new Vector3(1f, 1f, 1f);
weaponRotationPoint.localScale = Vector3.one;
isFlipped = false;
}
//ROTATING THE GUN
Vector2 offset = new Vector2(mousePos.x - screenPoint.x, mousePos.y - screenPoint.y);
//RETURNS ANGLE BETWEEN X AND Y
float angle = Mathf.Atan2(offset.y, offset.x) * Mathf.Rad2Deg;
weaponRotationPoint.rotation = Quaternion.Euler(0, 0, angle);
}
}
## ShootingFunction ##
public void ProjectileShot()
{
PlayerRangedAttack.instance.PlayerBulletPoolAttack(shotPoint);
}
I tried creating a variable to store the player’s weapon rotation(centerAngle). But when I try to apply that to the PlayerAttack function the angles are always weird, I need some help finding the correct formula to calculate this. Can please someone help?
Thank you so much for reading this.