- Home /
Creating a spread of bullets
I have gotten my code so that it fires two bullets, but currently they fire to the exact same position, but I have been trying many different ways (without success) to angle the second bullet slightly to one side at a fixed angle.
My code:
if (lookRight) {
//first bullet
Vector3 sp = Camera.main.WorldToScreenPoint(transform.position);
Vector3 dir = (Input.mousePosition - sp).normalized;
Rigidbody2D bPrefab = Instantiate (staffBulletPrefab, transform.position, Quaternion.identity) as Rigidbody2D;
bPrefab.rigidbody2D.AddForce (dir * 650);
coolDown = Time.time + attackRate;
float rot_z = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
bPrefab.rigidbody2D.transform.rotation = Quaternion.Euler(0f, 0f, rot_z - 45);
//second bullet
Vector3 dir2 = (Input.mousePosition - sp).normalized;
Rigidbody2D bPrefab1 = Instantiate (staffBulletPrefab, transform.position, Quaternion.identity) as Rigidbody2D;
bPrefab1.rigidbody2D.AddForce (dir2 * 650);
coolDown = Time.time + attackRate;
float rot_z1 = Mathf.Atan2(dir2.y, dir2.x) * Mathf.Rad2Deg;
bPrefab1.rigidbody2D.transform.rotation = Quaternion.Euler(0f, 0f, rot_z1 - 45);
}
As always, any help what so ever is greatly appreciated! :)
Answer by LRG · Dec 19, 2014 at 01:20 PM
To spread the bullets randomly you can probably do something like:
Vector3 newDir = (dir.x + Random.Range(0.0f, 0.1f), dir.y + Random.Range(0.0f, 0.1f), dir.z + Random.Range(0.0f, 0.1f)).normalized;
Rigidbody2D bPrefab = Instantiate (staffBulletPrefab, transform.position, Quaternion.identity) as Rigidbody2D;
bPrefab.rigidbody2D.AddForce (dir * 650);
This will simply vary the values a little and normalize it again.
Edit
If you actually want to spread the bullets by a fixed angle you can try something like:
Quaternion rotation = Quaternion.Euler(0f, 0f, rot_z1 - 45);
Vector3 newDir = rotation * dir;
bPrefab.rigidbody2D.AddForce (newDir * 650);
That should rotate the direction vector by your rotation as well.
Though, you might have a simpler alternative. You are (presumably) already rotating your sprite. You can simply add the force locally. Though unfortunately the 2D tools don't seem to have an AddRelativeForce method, you can:
bPrefab.rigidbody2D.AddForce(bPrefab.transform.forward * 650);
This should essentially just propel the bullet forward with respect to itself, which is probably what you want.
Thanks for the reply! But what I am wanting (and didn't state in the question sorry (will edit now)) is that its a fixed angle, so that, for example, 3 bullets are fired on each click, at fixed angles.
Ok! Then probably just doing a pseudo addRelativeForce should do, considering that you are rotating the object already. I've updated the answer.
This is working, but to an extent. There is now a problem based on the mouse position, the closer the mouse to the player, the wider the angle is, and the further away the mouse is, the smaller the angle!
Answer by HarshadK · Dec 19, 2014 at 08:08 AM
You are applying the force on both the bullets in the same direction. Try changing the direction of applying force to second bullet.
bPrefab1.rigidbody2D.AddForce ((dir2 + new Vector3(0.1f, 0f, 0f) )* 650);
In above code the direction of applying is deviated by 0.1f in x axis. Just get the logic and you can apply deviation in any way you want.
The issue with this is that it is causing the bullet to travel much faster then the other projectile, but I believe this is a good start :)
You can actually set the less value for force like you can change 650 to maybe 600 so the force applied balances out.
Your answer
Follow this Question
Related Questions
2D How to fire bullets on the y axis. 0 Answers
2D bullet spray problems 1 Answer
Multiple Cars not working 1 Answer
Transform.translate bullets problem 0 Answers
2d bullet shot at bullet emitter 0 Answers