Freeze a bullet on second click

Hey guys, just wondering how I could do this. Basically I have my script that fires a bullet (click the left mouse button to fire the bullet). Everything is fine and the bullet fires like normal. However, I want another even to occur. Once the bullet is travelling, if the left mouse button is pressed for a second time, I want the bullet to stop moving (freeze) and stay in the environment permanently. So first click = fire a bullet. Second click freeze the force and keep the bullet in the scene.

After this event has occurred, I need the third click to go back to the start and fire another bullet. What’s the best way of doing this guys? Here’s what I have so far:

var bulletPrefab : Transform;	// Declares the bullet prefab.
var shotDelay1 : boolean = false;
var counter : int = 1;
     
// If the space bar is pressed, fire a bullet with a force of 5000 along the Z axis. Once fired, initiate the delay and disable the function for 2 seconds.

   function Update () 
   	{

    		
        	if (Input.GetMouseButtonDown(0) && shotDelay1 == false)
        	{
        	
				counter ++;
       			PlatformFire = Instantiate(bulletPrefab, GameObject.Find("SpawnPoint").transform.position, transform.rotation);
        		PlatformFire.rigidbody.AddForce(PlatformFire.forward * 10000);
        		shotDelay1 = true;
				shotDelay();
				
if (counter == 2 && Input.GetMouseButtonDown(0)) 
{
    PlatformFire.rigidbody.velocity = Vector3.zero;
}

if (counter > 2)
	{
	counter = 1;
}

}
}

function shotDelay()
	{
if (shotDelay1 == true)
	{
		
        		yield WaitForSeconds (0.6);
				shotDelay1 = false;
				}
        		
}

Thanks for any tips :slight_smile:

What have you tried? The code you have there only shows creating the bullet.

Your best way to get answers is to try and come up with a solution yourself - then someone can help you. Otherwise you’re just asking people to write code for you.

But let’s take a look at what you DO have. You’ve got your ‘PlatformFire’ (which seems to be your bullet) when you shoot - why not do something really simple, like…

if (PlatformFire != null) 
{
    PlatformFire.rigidbody.velocity = Vector3.zero;
}

This way, if you’ve already created PlatformFire (PlatformFire != null), it will set its velocity to zero, which should stop it dead.

However, this will NOT shut down the effect of gravity. You may also want to do…

 PlatformFire.rigidbody.enabled = false;

…which should also shut down physics for that object.