Need help getting player bullets to come out in the right direction

Hi All,

How do I script the rotation of an object?

I have a simple game where the player controls a ship. You shoot pressing the space bar. Everything is working great except the power up. When the player moves over a pink balls (representing a stand in for the power up) He is able to fire 2-5 more additional lasers. The first player bullet instantiates where expected to. (In the front center of the ship) The additional lasers are supposed to instantiate to the left or right of the player’s default weapon. Instead they appear to come out of the back of the ship. Upon investigation, it seems like the players model’s transform is reversed. The “Front” of the ship is actually facing the tail. I tried to reverse this but to no avail.

I then tried to fix the error with script. The idea was that I can just change the rotation of the power up bullets upon instantion. Despite my efforts, it STILL appears to come out of the ship’s rear. How do I code the proper behavior of all bullets coming out of the front of the player’s ship? Thanks in advance.

Code:

 if (Input.GetKeyDown("space"))
			
		{
			//creates the missle
			
			if ( bullettype == 0 ){
			var newbullet : GameObject = Instantiate (bulletprefab, transform.position, transform.rotation);	
			}
			
			else if (bullettype == 1 ){
			Instantiate (bulletprefab, tranform.position,  Quaternion.Euler (Vector3(0, -20,0) ) );
			Instantiate (bulletprefab, transform.position, transform.rotation  );
			Instantiate (bulletprefab, transform.position , Quaternion.Euler(Vector3(-180,  20,0)  ) );
			}
			else if (bullettype == 2 ){
			Instantiate (bulletprefab, transform.position, Quaternion.Euler (Vector3(0, -20,0) ) );
			Instantiate (bulletprefab, transform.position, transform.rotation);
			Instantiate (bulletprefab, transform.position, Quaternion.Euler (Vector3(90,  20,0) ) );
			Instantiate (bulletprefab, transform.position, Quaternion.Euler (Vector3(90,  -40,0) ) );
			Instantiate (bulletprefab, transform.position, Quaternion.Euler (Vector3(90,  40,0) ) );
			
			}
		}
}

Here’s a picture of what the desired direction should be.

http://www.flickr.com/photos/36249344@N07/5211616385/lightbox/

Hope that was clear. Let me know if there’s questions. I appreciate any insight you fine folks have.

I can’t be sure, but it appears you are keeping the position the same and the rotation the same, so no wonder they go out the back. Don’t you want the rotation the same but the position different (just left and right of initial missile) ?

Hi Kranky,

Yes, I want the additional missles to appear to the left or right of the initial one upon pickup. The initial missle also keeps the rotation and position the same, and it still comes out of the front. I did try to use transorm.position.x + localscale.x /2 to change the instatiation position of the 2nd and 3rd missles. This didn’t seem to work, the missles still came out of the rear. so I changed it back to what appears above.

To get something to “spawn” beside the ship use : transform.TransformPoint(Vector3.right * 1.2f); and adjust the 1.2 for distance.

For instance, for a spell I spawn I use:

        Vector3 spellPosition = spellCaster.gameObject.transform.TransformPoint(Vector3.forward * 1.2f);
 
        //Create flight prefab effect
        spell.cloneFlight = (GameObject)Instantiate(spell.SpellBeingCast.FlightPrefab, spellPosition, Quaternion.identity);

this place the spawn point in front of the player no matter what direction he is facing.

Thanks kranky,

For some reason, I can’t get this to work. would I subsitute SpellCaster with “player”? The name of my player object? Thanks.

SpellCaster for me is a component/script, so that is why I move up using “gameObject” to the actual GameObject. So for you SpellCaster would be the ship object as that is where they are relatively spawning from.

thanks! Worked like a charm.

Great!!! Glad I could help.