Bullet 3 way Spray

Hello,

I was wondering if anyone could take the time to help me create a 3 way spray of bullets firing?

I know how to do a single bullet, code:

var bullet : Rigidbody;
var shootSpeed : float = 10;

function Update()
{
	if(Input.GetMouseButtonDown(0))
	{
		print("fire!");
		
		var clone : Rigidbody = Instantiate(bullet, Vector3(transform.position.x,transform.position.y,transform.position.z),transform.rotation);
		clone.velocity = transform.TransformDirection (Vector3.forward * shootSpeed);
	}
}

But Id like to learn how to do it like in the image thats attached. If possible could you please show and example and explain any code you post here so I can learn how it works. Id rather not just copy and paste but learn whats going on and why.

I did try rotating the transform that the bullet comes out of but the bullet still goes in the same direction so Im guessing that the bullet would have to be rotated in code somehow, well not the bullet but the direction of which the bullet goes and Im not at that level yet :frowning: Im not really sure where to start either?

Oh… in case you need to know this is a 3rd person shooter game and the transform will be out in front of the character.
I thank anyone who can help in advance!

one way to try to work out doing threee sprays is to make maybe three different spawn points for the bullets since you have one to spawn a bullet forward if you try to add two more in there they are probably going to go all forward.

on the secont bullet for example at the conde for velocity just add another direction to go for example:
clone.velocity = transform.TransformDirection (Vector3.forward * shootSpeed); (that’s the original code and for the second add)
clone.velocity = transform.TransformDirection (Vector3.forward * Vector3.right * shootSpeed); pretty much add another vectorto make it add up so forward plus right will make it go diagonaly right. and for left do a -Vector3.right make it negative.

try it out see if it works.

Is there another way to use the Vecyor3.forward and the Vector3.right to get something in the middle of the two. The reason I ask is because I get the Error:
“Operator ‘*’ cannot be used with a left hand side of type ‘UnityEngine.Vector3’ and a right hand side of type ‘UnityEngine.Vector3’.”
When I try and use:

clone.velocity = transform.TransformDirection (Vector3.forward * Vector3.right * shootSpeed);

and if I take out the Vector3.forward to just use the Vector3.right the bullet goes extreme right. So is there another way?

Im trying to research and I did find this:

// Create a rotation
var rotation = Quaternion.identity;
// Assign a rotation 30 degrees around the y axis
rotation.eulerAngles = Vector3(0, 30, 0);
// print the rotation around the y-axis
print(rotation.eulerAngles.y);

But I dont know how to incorporate it into the script above to see if its something that might work? Any chance you know how? or know of another way?

So I found this as well, but this puts a random range in between so it seems to bounce back and forth between forward and left, not quite giving me the diagonal shot of fixed angle between forward and left.

Any Help would be greatly appreciated. This is a lot closer but not quite right yet :frowning:

var bulletPrefab : Transform;
var strayFactor : int;

function Update() 
{
    if(Input.GetMouseButtonDown(0)) 
    {

        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);
    }
}

Hi, I your thread last night and it looks pretty interesting, I wish I had a perfect answer for you, however I did just now stumble across this script in the references:

// Computes the angle between the target transform and this object
var angleBetween = 0.0;
var target : Transform;
function Update () {
var targetDir = target.position - transform.position;
angleBetween = Vector3.Angle (transform.forward, targetDir);
}

It’s not exactly what you want, but I thought Vector3.angle would be worth a look, like you could have three empty objects parented to the gun, on in the middle, and two on either side of it, then have one bullet shoot at the middle one, and the two on the sides shoot inbetween the middle one and the ones on their side. Just a thought, it looks like your getting closer already though, hopefully this will draw some pro’s to the forum :slight_smile: Good luck.

var bullet : Rigidbody;
var shootSpeed : float = 10;

function Update()
{
	if(Input.GetMouseButtonDown(0))
	{
		print("fire!");
		var clone : Rigidbody;
		// forward shot
		clone = Instantiate(bullet, transform.position,transform.rotation);
		clone.velocity = transform.TransformDirection (transform.forward * shootSpeed);

		// left shot
		clone = Instantiate(bullet, transform.position,transform.rotation);
		clone.velocity = transform.TransformDirection ((transform.forward + transform.left) * shootSpeed*1.414);

		// right shot
		clone = Instantiate(bullet, transform.position,transform.rotation);
		clone.velocity = transform.TransformDirection ((transform.forward - transform.left) * shootSpeed*1.414);
	}
}

This should do it, you may want to push them away from each other, as they are all created at the same place.

1.414 is root 2 as forward and left are unit vectors so this is a quick normalize.

Side note:

Depending on what you are developing on, it is best not to Instantiate bullets. Try to use a pool of them and disable them when not in use. Also, use Ray Traces if you can, as this is faster than instantiating.

Hey Guys I really appreciate the Reply’s “Awesome! Stuff”. Im getting Very Close, I can feel it, lol

@foxter888
I tried it but the left hand side type thing is throwing me off.

@wesleyborthwick
Your alright! I never thought it would be this tough to get a 3 way shot effect lol.

@gobbledeegeek
I really thought your solution was going to work but I get this error:

'left' is not a member of 'UnityEngine.Transform'

I thank you all for the Input!!! Greatly appreciated! and the good news is with the help of all of these posts I finally got some results :slight_smile:

var bulletPrefab : Transform;

var rotateX = 0;
var rotateY = -10;
var rotateZ = 0;

function Update() 
{
    if(Input.GetMouseButtonDown(0)) 
    {
    	var bullet = Instantiate(bulletPrefab, transform.position, transform.rotation);
        bullet.transform.Rotate(rotateX, rotateY, rotateZ);
        bullet.rigidbody.AddForce(bullet.transform.forward * 10000); 
    }
}

Now I have one bullet that goes to the left inbetween Left and Foward :slight_smile: and I can change the spread with the variable rotateY :slight_smile:
Next Up is 2 create 2 more bullets more for forward and right within the same script.

Should be a piece of cake now :slight_smile: Again thank you all for the input! I’ll post my results from the getting all 3 bullets to fire within this script a little later today, you guys have been AWESOME, I gotta run.

Hello,

IceShaft07 is there any chance you can explain how to use the pool? or give an example?

I found this link but couldn’t get it to work???
http://vonlehecreative.com/video-games/unity-resource-gameobjectpool/

Id like to use a pool system with the above Bullet Script I got working as well as with my enemy instantiated script here:

var wave1: GameObject;

var seconds : float = 3;
var howMany : float = 10; 
var destroyAfterOneLess : float = 9;

function OnTriggerEnter (other : Collider) 
{
	if (other.gameObject.tag == "myrobot")
	{
		Spawn();
	}
}
function Spawn() 
{
   for (howMany=0; howMany<10; howMany++) 
   { 
      yield WaitForSeconds(seconds); 
      Instantiate(wave1, transform.position, transform.rotation);
      
      if(howMany >= destroyAfterOneLess)
      {
		destroy();
      }     
   } 
}

//this destroys the script so it doesn't keep getting triggered
function destroy ()
{
	Destroy(this, 0.0);
	Debug.Log("Script Destroyed");
}

If you could give an example that would be awesome.

And Just in case anyone else out there has an issue with trying to get a 3-way type of spray or shooting effect here’s how I did it, with the help of the awesome people here :slight_smile:

var bulletPrefab : Transform;

var bulletLeft = -10;
var bulletCenter = 0;
var bulletRight = 10;

function Update() 
{
    if(Input.GetMouseButtonDown(0)) 
    {
    	var bullet = Instantiate(bulletPrefab, transform.position, transform.rotation);
        bullet.transform.Rotate(0, bulletLeft, 0);
        bullet.rigidbody.AddForce(bullet.transform.forward * 10000);    	
    	
    	var bullet2 = Instantiate(bulletPrefab, transform.position, transform.rotation);
        bullet2.transform.Rotate(0, bulletCenter, 0);
        bullet2.rigidbody.AddForce(bullet2.transform.forward * 10000);
        
        var bullet3 = Instantiate(bulletPrefab, transform.position, transform.rotation);
        bullet3.transform.Rotate(0, bulletRight, 0);
        bullet3.rigidbody.AddForce(bullet3.transform.forward * 10000);
    }
}

yeah It should be .right not .left

silly me.

glad you got a solution.