- Home /
2D shooting problem please help!
Hi, i have created a simple Javascript that allows the player to shoot in the direction they are facing, with the left mouse. But i have a problem that i cant fix. I have also created a basic pause game script (JS also), but whenever i pause the game and click the GUI buttons (Like options etc), the game character fires the bullets while paused! (The projectiles remain static but build on each click). On resume, the bullets shoot in the direction i'm facing, depending on how many times i have clicked in the pause menu screen. Is there anyway to deactivate the shooting script when the escape key is press (When paused)?
Scripts used:
var Bullet:Transform;
var shootForce:float;
function Update() {
if(Input.GetButtonDown("Fire1")) {
var instanceBullet = Instantiate(Bullet, transform.position, Quaternion.identity);
instanceBullet.rigidbody.AddForce(transform.forward * shootForce);
}
}
And:
var paused : boolean = false;
var myCheck : boolean = false;
function Update () {
if(Input.GetButtonDown("Pause")) {
if(!paused){
Time.timeScale = 0; paused = true;
} else {
Time.timeScale = 1;
paused = false;
}
}
}
function OnGUI() {
if(paused) {
if(GUI.Button (Rect (10, 50, 100, 30), "Resume")) {
Time.timeScale = 1.0f;
paused = false;
}
if(GUI.Button (Rect (10, 90, 100, 30), "Options")) {
//Pause Game Options
}
if(GUI.Button (Rect (10, 130, 100, 30), "Exit")) {
Application.Quit();
}
}
}
I just formatted your code. You can do this by highlighting all your code, then clicking the 10101 button at the top of the edit window =]
There are 2 ways. Put your first script in a FixedUpdate() function.
Or : you already have a boolean, so use that. Change var paused : boolean = false; to
public static var paused : boolean = false;
then on the shoot script :
if ( Input.GetButtonDown("Fire1") && NameOfPauseScriptHere.paused == false ) {
Seems like the OP has left this cute little question all alone in the sea of questions. What will happen to poor little question?
Answer by Shrandis · Sep 20, 2012 at 03:26 PM
Setting the timescale to 0 will not prevent your code from getting inputs and instantiating projectiles. You also need to check if the game is paused or not in your bullet shooting script before checking for mouse input and instantiating the projectile.
void Update()
{
if (!paused)
//check for mouse input and shoot bullet
}
Hope it helps. Also, next time use the 101010 button to make your code easier to read.
Hey there, that was my first comment too until I realized the inputs are on 2 different scripts. So this could work if the scripts were combined, but for one boolean variable to be shared, and being a direct reference to the timeScale, all scripts could benefit from a static variable isPaused.
I assumed the OP would know about passing variables between different scripts. But you're right, I should have written in detail. I'm new to unity answers so forgive me :)
Hey, no worries, totally cool. And look - you receive the new user award for using the comment button ! I posted this for you from the moderator que, so the question was fresh in my $$anonymous$$d. Happy Coding =]
Answer by lighting · Sep 20, 2012 at 03:30 PM
Can't you just check the area, where the mouse cursor is being placed and exclude the buttons' area ? I mean - sth like: if(Input.GetButtonDown("Fire1") && Input.mousePosition.y > yourValue && input.mousePosition.x > yourValue2) { ... }
Your answer
Follow this Question
Related Questions
Looking and firing towards mouse position, 2d game. 3 Answers
Top down 2d shooting - XZ rotation 0 Answers
Stop shooting infinite bullets with touch 3 Answers
hinge joint 2d is disconnecting/detaching/tearing 1 Answer
Transform.translate bullets problem 0 Answers