Swipe cause GetMouseButtonDown(0) - c#

I have a script that recognizes swiping input. When I now start a swipe, it also triggers Input.GetMouseButtonDown(0). Can you tell me how to delete Input.GetMouseButtonDown(0) that comes from the swipe?
With that swipe the game gets paused. (with Time.timeScale = 0f;)
After resume, the action which was triggered from Input.GetMouseButtonDown(0) before pausing the game goes on.
My Ideas: Work with TouchPhase.Stationary or look for short tap?

Off the top of my head, you can either:
Call Input.ResetInputAxes; it will clear all input flags for one frame.
Set a flag that a swipe was handled, and check for it when checking for Input.GetMouseButtonDown(0) elsewhere.
Both will require ensuring that the swipe detection happens before you check for Input.GetMouseButtonDown(0).
One way of ensuring it, is by using Unity's script execution order settings.

I would recommend not using Input class at all, it causes many problems, better have separate RaycastTarget on UI with Script implementing IPointerDownHandler, IPointerUpHandler, that will ensure only one RaycastTarget at the moment is handling your touches. Input.GetMouseButton will return true even if some RaycastTarget already should have consumed your events.

Related

How to set animations "LoopTime" to false from script (Unity 2020.1.15f1)

I spent good time trying to find any solution but everything I find seems to be for older versions or something? Basically I want to set an AnimationClip.LoopTime from true to false or vice versa from script.
small pic of the inspector showing AnimationClip.LoopTime
I want this to let the object finish the currently running animation cycle. The GameObjects Animator.ApplyRootMotion=true to let the animation occur at the current objects position.
If I just set the Animator to enabled=false like it's advised in some posts, the animation stops right away of course and that leaves the object off position.
I get the desired info with myAnimator.GetCurrentAnimatorClipInfo(0)[0].clip.isLooping but that doesn't let me set it...
Somehow it has to be possible to just set looping=false and avoid having to create additional complicated transitions or something with the Animator?
Any help/hint is greatly appreciated!
Put an event at the end of animation, which would call function inside a script. The function could disable the Animator or stop animation, depending on your need for further enabling.

issue using "Any State" in Unity Animator

So, i am making a little 2D game, and in this game the player can snowboard, so, i made the player animator, and i wanted the player to snowboard doesn't matter the state, so i used the "Any State" state to transition the current animation to the "9_Snowboarding" animation using a bool called "isSnowboarding", and it worked fine.
The problem began when i wanted the player to jump, i created the jump animation, and i created a bool to make the transition happen called "isJumping", and i set the bool to true by code.
Instead of transitioning to the animation and playing it, the animator controller keeps transitioning the "9_Snowboarding" to the "10_SnowboardJumping" multiple times, and i dont know how to solve this.
Alternatively, if you absolutely need a bool there for some reason, you can disable the Option "Can Transition To Self" in the Transition settings of the Any State to your State Die.
Particularly in your case you need to use a "trigger" instead of a "bool" parameter.
The problem with bool is that isJumping is always set to true so your condition keeps on matching and you keep on transitioning to the same animation.
"Triggers" on the other hand will disable once used. So try adding something like Jump trigger and set that in your code

How to stop the animation halfway and transit in Unity?

I have this structure of animations:https://i.stack.imgur.com/Rv0fq.png
The idle state runs in a loop until the user presses "space",then parameter named "Jump" becomes true and the transition between idle and Jump becomes active.But before it becomes active user has to wait for "idle" to finish.How can i make it so when user presses space,idle automatically stops and transits to Jump? I tried making 'animation' stop like this:
if(Input.GetKeyDown("space")){
anim.Stop();
animator.SetBool("Jump",true);
}
But it didn't work.
Ok, I warn you the jump is not that easy.
You will need to know if the player is on ground if you don't want your player to be able to jump in the air and many other things.
Anyway to your question.
To avoid exit time on animation you should uncheck the "Has exit time" value on the animator arrow between an animation and the other one.
Your current code just stop the animator but that's not what you really want... you want it to return to Idle.
The best thing you can do is:
- From "Any state" to "Jump" (you should create a blend tree between Jump and Fall)--->in the arrow uncheck "Has exit time" and set up a new Bool "JumpBool" (for example).
From "Entry" to "Idle"
From "Jump" to "Idle" when "JumpBool" is false.
In your code:
if(Input.GetKeyDown("space") && !OnGround()){
animator.SetBool("JumpBool",true);
}
(you should create your OnGround method).
else if(OnGround()){
animator.SetBool("JumpBool",false);
}
Of course this is almost pseudo-code. Like I said you should create a BlendTree for Jump and Fall where you should be able to tell if you need to play the jump animation of just the fall animation (maybe with a float depending on your corrent rigidbody .velocity.y speed).

How to initiate click to activate code in Unity 3D?

I am designing a shooting game in Unity 3d for which I have a requirement that only when an image is tapped the shooting should occur. Please check the command used for the purpose:
if (Input.GetMouseButtonDown(0)){
if (!clickedConfirmed){
GameObjectClicked = GetClickedGameObject();
clickedConfirmed = true;
}
Currently, shooting occurs when clicked anywhere on the screen. How can activation of shooting be bonded only to the gameobject (image) instead of being activated when clicked anywhere on the screen.
Your need to give more context in your example, especially what GetClickedGameObject() is actually doing internally.
As such I'm limited in what I can say is the problem, but I suspect that you aren't using GameObjectClicked correctly.
I would expect that you'd do a check such as:
this.gameObject == GameObjectClicked
Note that if this being used as a method of developing a UI there are more elegant solutions out there, including the long awaited new GUI system included in the Unity3d 4.6 open beta.

Processing game input

So my basic 2d game framework in SlimDX is going well. I've created a custom Sprite object, and have some rudimentary mastery of rendering sprites. I just need to move them now.
I have a basic game loop set up, which is based on an old C# game programming book. Having set up all the graphics device bits and bobs, and drawn my sprite on screen, I enter a method in my Game class called Run(). This has the following content:
public void Run()
{
while( this.Created )
{
// Process one frame of the game
ProcessFrame();
Render();
// Handle all events
Application.DoEvents();
}
}
ProcessFrame() is supposed to have all the game logic in it, in response to events. Whilst I have a simple event handler (based on an override of OnKeyDown) set up to detect keypresses, I'm wondering how to collect keypresses such that I can process the responses to them in the ProcessFrame() method.
Some initial research on this subject suggests creating a HashSet<Keys>. I've never done this before, so I'm not sure how I would go about it.
There are probably many ways to do this, so I thought I'd see what people would recommend before jumping right in there.
Thanks in advance everyone.
I don't believe you need to manually capture inputs. This typically would be handled by the direct input keyboard class. I personally have not used SlimDX myself yet but as it is a wrapper for DirectX (which I've dabbled in) you should just need to initialize a variable to hold the reference to the keyboard so that you can poll it for changes, and then act upon those changes.
http://slimdx.org/docs/#T_SlimDX_DirectInput_Keyboard
Such that you would code something like
if (myKeyboard.IsKeyDown(Keys.Escape)) { ProgramStatus = GameState.Title; }
assuming my reference variable was called myKeyboard.
Edit:
I might also mention that depending on the particular library your using that it can be beneficial to save current, and previous keyboard states. This is very useful when you want to ensure that the program only takes in one keystroke vs many.
XNA as I recall has a KEYDOWN method for controllers, useful to test if the button is being held down. If you need to take in a input for each time the button is hit capture both.
IE if your library has a Keypress method, your good, if it captures KEYDOWN only you may need to capture current and previous.
if (myMouse.LeftButton == ButtonState.Pressed && oldMouse.LeftButton == ButtonState.Released)

Categories

Resources