Question by
unity_772A4BD2401AFB601928 · May 22 at 05:24 AM ·
top down shooter
How to make bullet spread in 3D top-down shooting game?
I am trying to make a shotgun script, but the bullet only spread if the character is facing the z-axis and it gets more concentrated as it turns to face the x-axis.
I already looked at some of the similar questions in the community but most of them are FPS game and coded differently. Does anyone have any idea how to fix this?
Here is my code:
float x = Random.Range(-spread, spread);
float y = Random.Range(-spread, spread);
float z = Random.Range(-spread, spread);
//Calculate Direction with the spread
Vector3 direction = attackPoint.forward + new Vector3 (x, y, z);
bulletPrefab.GetComponent<PrefabBullet>().setShootDirection(direction);
bulletPrefab.GetComponent<PrefabBullet>().setBulletSpeed(bulletspeed);
Instantiate(bulletPrefab, attackPoint.position, attackPoint.rotation);
The bulletPrefab only have one line of code in the start function:
public float bulletspeed;
public Rigidbody rb;
public Vector3 dir;
public void setBulletSpeed(float BulletSpeed)
{
bulletspeed = BulletSpeed;
}
public void setShootDirection(Vector3 Shootdir)
{
dir = Shootdir;
}
void Start()
{
rb.velocity = dir * bulletspeed;
}
Comment
Your answer
Follow this Question
Related Questions
How can I rotate a Parent GameObject so a Child points towards the cursor? 0 Answers
General 2D Top Down Help 0 Answers
How to make bullet shoot in same direction as player faces in 2D Top Down Shooter 0 Answers
Can't get animator to activate parameter more than once (Unity2D). 0 Answers
Top Down help 1 Answer