I want to add spread and inacurracy to my top-down shooter. The bullets spread as they should, but my gun kinda wiggles because it thinks I’m changing its Transform. My code so far looks like this:
protected void InstantiateBullet(float spread)
{
Transform clickTransform = transformAtClick;
float deviation = Random.Range(-inaccuracy, inaccuracy);
float actualAngle = clickTransform.eulerAngles.z;
Vector3 euler = Vector3.forward * (actualAngle + spread + deviation);
clickTransform.rotation = Quaternion.Euler(euler);
GameObject bullet = Instantiate(bulletPrefab, firePoint.position, clickTransform.rotation);
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
rb.AddForce(clickTransform.right * bulletSpeed * Time.fixedDeltaTime, ForceMode2D.Impulse);
}
What I think is the problem is that all Transforms store a reference to gameObject.transform, but I’m not sure. Honestly, I don’t know how to provide any further information, so I hope this is enough.