Shooting Gallery Help

var particle : GameObject;
var reticle : Texture2D;

function OnGUI() {

var pos = iPhoneInput.GetTouch(0).position;
var texRect = new Rect(pos.x - reticle.width / 2, +pos.y - reticle.height / 2, reticle.width, reticle.height);

GUI.DrawTexture(texRect, reticle);
for (var i = 0; i < iPhoneInput.touchCount; ++i) {
if (iPhoneInput.GetTouch(i).phase == iPhoneTouchPhase.Began)
// Construct a ray from the current touch coordinates
var ray = Camera.main.ScreenPointToRay (iPhoneInput.GetTouch(i).position);
if (Physics.Raycast (ray)) {
// Create a particle if hit
Instantiate (particle, transform.position, transform.rotation);
}
}

}

The script above is not working right. It’s spawning the game object - but it’s not acting like a gun firing bullets. In other words they just fall flat. Help please

You have to affect the transform of the bullet. If you’re not telling it how fast to move, or which direction to move it’s not going to go anywhere. You’re just telling it to spawn, and then do nothing if you read your code

to get it to move:

look into Rigidbody.velocity.z
or the Translate function
or bullet.transform.position.z += speed*Time.deltaTime

to stop from falling:

Also turn OFF the gravity on the rigid body on your bullets or they will fall…

@Aaron - good look yo! Thanks for the feedback. You have me in the right track. I decided not to do the instantiate object… too much lag. I am going to stick with the raycast.

Whats slow is probably the ONGUI that your using, not the instantiate. I use it in one of my games and it works fine. ONGUI is really slow and crappy on iPhone i wouldnt even use that.

Sorry i should mention alternative to using OnGUI. You can use this:

var getGUITexture : GUITexture; //MAKE WHATEVER BUTTON YOU USE TO SHOOT INTO A GUITexture
var isSpawningBullet : boolean;

Update(){

if (((Input.GetMouseButtonDown (0)) (isSpawningBullet == false))
{

if (getGUITexure.GetScreenRect().Contains(Input.mousePosition)) // IF YOU TOUCH THE GUITEXTURE
{
//PUT YOUR BULLET SPAWN CREATE CODE HERE, OR A SWITCH TO START IT…
}//END GUIRECT

isSpawningBullet = true;//TURNS OFF YOUR ABILITY TO MAKE A NEW BULLET UNTIL AFTER THE FIRST ONE IS SPAWNED
//SET TO FALSE WHEN THE BULLET IS DONE BEING CREATED THIS WILL PREVENT BULLETS SPEWING OUT OF CONTROL

}//END MOUSE INPUT

}//END UPDATE