- Home /
How to make my bullets spread when fired
Alright i am making a action/arcade game, where you are a Tank, and you have to go around shooting enemies to gain gems. As it is a arcade type game, i made a power-up that upgrades your gun, from being a single shot, to shooting 3 bullets at the same time. I got that whole part worked out, the problem is, the bullets are instantiated on 3 different SpawnSpots (game object), and they all fly forward of the player. The thing that i want to do is have it so one goes stright forward, the one on the right goes right at a angle, and the one on the left goes at angle to the left. I have no idea how to do this, i figure it is part of rigidbody.AddForce but i do not know which would work, so i could turn, and it would always shoot forward of me, yet slightly angled so it spreads.
Answer by Sebas · Feb 17, 2010 at 08:22 PM
You would have to apply a rotation to the bullets after they are instantiated but before you add a force to them. I have a script laying around which gives a bit of a random spread. You can easily modify this so that you have a fixed angle to the left and a fixed angle to the right for those bullets.
var bulletPrefab:Transform; var strayFactor : int;
function Update() { if(Input.GetButtonDown("Jump")) {
var randomNumberX = Random.Range(-strayFactor, strayFactor);
var randomNumberY = Random.Range(-strayFactor, strayFactor);
var randomNumberZ = Random.Range(-strayFactor, strayFactor);
var bullet = Instantiate(bulletPrefab, transform.position, transform.rotation);
bullet.transform.Rotate(randomNumberX, randomNumberY, randomNumberZ);
bullet.rigidbody.AddForce(bullet.transform.forward * 10000);
}
}
So the bullet is instantiated in the exact rotation which you are facing (given that you attach the script to your main camera) and then it adds a rotation (in this case random) and shoots the bullet off by adding the force.
Yeah this worked perfectly. Well it with a little modification it worked, but at least it pointed me in the right direction! Thanks so much for your help with this, and my other questions Sebas.
I wouldn't bother giving a bullet a rotation around the Z (forward) axis, it wont make any difference to its direction. You might as well save a calculation and just make it 0.0f
Answer by Me · Dec 17, 2010 at 04:29 AM
Any Chance you could post you modification?
ahh it's good to see someone is working on the same thing as me but i already fixed this subject but what im now trying to do is reducing the spread when i press/hold right mouse button this is ai$$anonymous$$g down sight
i know it's a question in an question but i cant find it anywhere and this is the fifth time i came to this question so if someone can help me
if (Input.Get$$anonymous$$ouseButtonDown(1)) {
direction.x += Random.Range(-$$anonymous$$inspread,$$anonymous$$inspread);
direction.y += Random.Range(-$$anonymous$$inspread,$$anonymous$$inspread);
direction.z += Random.Range(-$$anonymous$$inspread,$$anonymous$$inspread);
}
else { ....................... }
fixed it check above could be usefull for more realistic gun fire
Answer by acsended · Nov 16, 2011 at 12:22 PM
var Maxspread: float = 0.03;
var Mimspread: float = 0.01;
var Spread: float = 0.01;
//.....
// the total spread along the x y and z
//....
direction.x += Random.Range(-Maxspread,Maxspread);
direction.y += Random.Range(-Maxspread,Maxspread);
direction.z += Random.Range(-Maxspread,Maxspread);
if (Input.GetButton("Fire2"))
{
Maxspread = Minspread;
}
else {
Maxspread = Spread;
}
@@@@@@@@@@@@@@@@@@@@@@@@@
if u put this in the fireoneshot script or what function u have then the bullets should spread and if u press right mouse button it should spread less handy for if u can aim down sight a little with ur tank
Your answer
Follow this Question
Related Questions
make bullet move forward. 2 Answers
rigidbody addforce behaves different every time 2 Answers
Bullet Launcher Script 1 Answer