I have my script that I created, but when I right click everything goes well, it plays the aim animation and stop when it ends, but when you release it, it re play's the animation. Can anyone help?
#pragma strict
function Update () {
if (Input.GetMouseButtonDown(0)) {
animation.Play("Shotgun_Shoot");
}
if (Input.GetMouseButton(1)){
animation.Play("Shotgun_Aim");
}
else if(Input.GetMouseButtonUp(1)){
animation.Rewind("Shotgun_Aim");
}
}
I believe it is because of
else if(Input.GetMouseButtonUp(1)){
animation.Rewind("Shotgun_Aim");
}
This happens when the user releases the button
It says in the Unity API for this function
Returns true during the frame the user releases the given mouse button.
You need to call this function from the Update function, since the state gets reset each frame. It will not return true until the user has pressed the mouse button and released it again. button values are 0 for left button, 1 for right button, 2 for the middle button.
So you don't want that last else if
EDIT:
How about telling the animation also to stop if the mouse buttons are released?
Animation.Stop -- Stops all playing animations that were started with this Animation.
Stopping an animation also Rewinds it to the Start.
Related
I'm developing an android app, and I'm using the free touch joystick (This one) from the asset store. For the most part it works well, but I'm running into an issue. I have it set up so that you use Input.GetButtonDown("Fire1") to trigger actions on the scene. However just using the joystick is triggering the fire1 action by default when first pressing on the screen to move the character. I need the joystick to not send a touch input when that happens, is that possible?
This is the code that gets triggered:
void Update()
{
if (canActivate && Input.GetButtonDown("Fire1") && !DialogManager.instance.dialogBox.activeInHierarchy)
{
DialogManager.instance.ShowDialog(lines);
}
}
Is there a way of returning back to my previous game application when I close unity/close game application on button press. Basically I have a scene with a button in it, if the player press the button, it will bring them to a new level (scene 1). This is where I want to save this scene (scene 2) using something like playerprefs to keep tabs on it so that if I close the game application, or close unity and then I re-open and play the application (regardless of what scene I am in) the game should automatically bring me back to scene 2. Is it possible to return to previous application (even if I close the application or game) on button click?
So if the button is clicked on, and I exit out of the application and or game, then re open it and play the game then it should automatically bring me back to the scene I exited out of (scene 2)
When ever application quit event is called, Save current scene as player preference.
using UnityEngine.SceneManagement;
void quitGame()
{
Scene currentScene = SceneManager.GetActiveScene();
PlayerPrefs.SetString("lastSceneName",currentScene.name);
Application.Quit();
}
above code will save the name of current scene at time of application quit.
Now when you restart your game and your scene(0) is loaded. call following method in Start() or Awake() function.
void loadLastScene()
{
if (PlayerPrefs.GetInt("isFirstTime") == 0) //this is to make sure this chunk of code doen't run on the very first usage.
{
PlayerPrefs.SetInt("isFirstTime", 1);
}
else if (PlayerPrefs.GetInt("isFirstTime") == 1)
{
SceneManager.LoadScene(PlayerPrefs.GetString("lastSceneName"));
}
}
Code not tested.
I want that the player could play again when they're done playing.
The method works fine when I place it OnGUI(), but when I apply it to an UI button, the restart just hangs and the countdown timer isn't working at all.
public void playAgain(){
Application.LoadLevel (Application.loadedLevelName);
Character.score = 0;
time.second = 30.0f;
}
I believe that you probably set the time.timeScale = 0 when the game ended, and you probably forgot to start it over on the start of the level.
I'm having difficulty pausing the game when I leave and switch back to it.
I'm trying to pause the SKSpriteNode called main, which contains all my sprites, when the view returns from the background. In-game, I can touch the pause button and the game pauses, and the resume button and it resumes.
This is my code:
func didBecomeActive() {
println("didBecomeActive")
main.paused = true
}
The first time this runs is when the app opens for the first time, and everything is paused as it should be. The second time, is when it returns from the background, and suddenly all the animations (SKActions, particles, etc.) start working.
I've confirmed that the method is running, and I've also tried setting main.paused to false and then true, and even self.paused to true. Nothing works.
I'm completely stumped. Anyone know what the issue is here?
Setting self.scene.paused = YES should fix this. I have tried it with a game I am developing and it works fine.
Just set self.scene.paused = YES when the game enters the background, then when it return to the foreground, it should stay paused till you resume it, i.e. set self.scene.paused = NO.
In my iPhone app, I'm using MPMoviePlayerController to play a movie. I'm hiding all the controls that are by default visible on the movie player. But I placed a "Replay" button over the player control.
At the end of movie, the player is being removed. But I want to stop at the last frame, so that when I click "Replay" button, it will start from beginning. I wrote the functionality for replay and its working good. When ever the movie is playing and on click of "Replay", its restarting the video from starting.
The problem I'm facing is that, at the end of movie its becoming white screen and pressing "Replay" button is not restarting the movie. How to handle this situation?
Its not necessary to create new instance. I found out the solution. Don't just release the instance of player. When replay button is clicked (either in middle of video or after completion), just pause it, move the location to beginning and play it. That's it.. no retain, no release nothing.... Its upto the programmer/developer when to release the player and remove it from the view.
I never used MPMoviePlayerController but pherhaps it's instance is released when the movie has ended. In this case incrementing the retaincounter and manually releasing it would solve the problem.