- Home /
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 :)
One of the problems is that it creates prefab clones. It is these that I need to get the freeze on. Any ideas?
Answer by IgnoranceIsBliss · Nov 13, 2012 at 01:50 AM
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.
Yeah I tried the PlatformFire.rigidbody.velocity = Vector3.zero; but it didn't work. However, I didn't implement it like that so I will try it now. I have tried but the code I used was redundant so I removed it, in future I will include it, let me have a go with this :) Thanks for the reply
To restart once you've frozen the bullet, you'll just need to put some more logic in. If PlatformFire is set, but it's physics is disabled, then you should destroy PlatformFire, set it to NULL and prepare the player to shoot again.
I implemented a counter, but it's still not stopping it once I have fired. I updated the code, can you see what's wrong?
2 Things...
What is 'PlatformFire'? I can't see it declared anywhere.
Reverse the order of your 'if' statements, or 'return' out of them before you close the braces. What is happening is that you're increasing the counter to 2, then the next statement automatically gets run! You're running EVERY 'if' statement in the one 'Update' run.
Don't worry, that sort of mistake is VERY common. The best way to beat it is always go from the HIGHEST number to the LOWEST when putting in a chain of IF statements, or use a 'switch' ins$$anonymous$$d.
Your answer
Follow this Question
Related Questions
Importing from Maya 2 Answers
New User, New Project 0 Answers
unity freezes repeatedly 1 Answer