Processing game input - c#

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)

Related

Swipe cause GetMouseButtonDown(0)

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.

Changing Sprites using scripts in Unity2D C#

I am wanting to make a birds-eye view pixel-art game.
I currently have two sprite sheets set up, and split and whatnot
groundSheet and characterSheet these are split up into
ground_0_0_0 (A concrete floor)
ground_1_0_0 (grass)
character_0_0_0 (man idle animation frame 1)
character_0_0_1 (man idle animation frame 2)
character_0_1_0 (man run animation frame 1)
character_0_1_1 (man run animation frame 2)
character_1_0_0 (woman idle animation frame 1)
character_1_0_1 (woman idle animation frame 2)
character_1_1_0 (woman run animation frame 1)
character_1_1_1 (woman run animation frame 2)
The numbers after are a note as to:
first number - the main set of sprite animations (eg man)
second number - the animation set in use (eg run or idle)
third number - the frame of said animation.
(the ground has this as i plan to have animated grounds late on)
Now, I wish to make a script for the character (and ground alike) that has an editable value that is view able in the unity editor, for example how things like the sprite renderer has sprite, colour etc. Which dictates what first number to use (see above) what second number and the delay for the animation of the third number. This will then tell the sprite renderer what pictures to load and how quickly to load them. I also wish for the script to scan for the file starting with for example character_0_0_ and then count how many files after it, so it knows what to do when animating. I would like this script to be checking for a change in one of the variables viewable in the unity editor to change and as soon as it does it checks everything it needs for an animtion.
Something else could be done where there is only 1 box to edit in unity, which you put character_0_0_ or ground_1_0_ or something similar, and it checks everything that way (it also makes the script universal, and usable on the ground, character and walls (which I am adding later)).
This may seem confusing, but it make sense for me and many of you will probably mention a much easier way to do animations and such, but please only say these if it does what I want above.
For scripts and such my file layout:
/Assets
/scripts
ground.cs
character.cs
/sprites
characterSheet.png
character_0_0_0
character_0_0_1
character_0_1_0
character_0_1_1
character_1_0_0
character_1_0_1
character_1_1_0
character_1_1_1
groundSheet.png
ground_0_0_0
ground_1_0_0
(For some reason Stack overflow said the above was code, so i had to make it as that)
ground.cs and character.cs are the scripts in which I want to made as explained above.
In my object view thingy I have
Main Camera
ground
character
I am practically a newb to C# and JS I know bascially the grammar of C# (like where to use {} and put ; at the end of the lines). If you help me with this, i request that you explain the script, like use the // thing to simply explain what each command does, I know a few but not all of them. And I know someone is going to say it is really well documented in tutorial X and such, but most tutorials are not in unity 5 and while helping with the matter do not touch on it exactly.
Thank you for your help, if there is anything about this question/request that you do not understand (It is pretty complex) I will explain.
Maybe I am completely wrong, but it seems to me that you are trying to recreate an Animation system.
Is there a specific reason for which you wouldn't use Unity's animation system?
You can do everything that you describe here with it (change sprite, choose animation speed). And you would have almost no code to write. Just drag and drop you sprites in the editor for a start
EDIT - answer to first comment:
What you should do is create all the animations you need. Then in the animator, you choose which condition will trigger a transition to another animation (for instance, boolean jump is true will transition to animation jump). Then in your code you can call GetComponent().SetBool("Jump", true).
To have different character, you can create different gameObjects (one per character). They all have a different animator with animations specific to the character.
The other solutiojn if you really want one one gameObject and one animator is that you also add string condition to you animation (example, if character=="character1" and jump==true, transition to jump animation).
If I were you I would really focus on testing and learning all you can do with Unity animator. I can't think of a case were you would need to recreate the animation system yourself
Your question was long winded and hard to understand but ill try to help,
firstly if you want editable values in the unity editor I would Suggest using a serialized structure of information like this
[System.Serializable] // this makes it viewable in the inspector
public struct Sprite_Manager;
{
public Sprite[] Render_Sprites; // array of sprites to store sprites
public SpriteRenderer S_Renderer;
public float Anim_Delay;
}
public class Character : MonoBehavior {
Sprite_Manager SMG = new Sprite_Manager(); // instantiate a new instance of the struct
void Set_Sprite()
{
for(int i = 0; i < SMG.Render_Sprites.Length; i++)
{
SMG.S_Renderer.sprite = SMG.Render_Sprites[i];
}
}
void Update
{
Invoke("Set_Sprite", SMG.Anim_Delay);
}
}
Not sure if this is exactly what your looking for but this is one way you could setup a structure of sprite based information and use Invoke to setup some sort of delay when passing new sprites to the renderer.

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.

How to simulate touch events

I am working on a touch application using Kinect's depth sensor.
I am able to get the points where the user puts it's fingers, but I'd like to fire the corresponding Touch events.
For example, i'd like to be able to send TouchDown, Touch Up, Touch Move events.
I took a look at TouchDevice and TouchPoint classes, but I don't see how I should do this. I tried to create a fake TouchPoint and added it to a custom TouchDevice, but I don't know what to do next.
I tried to do a ReportDown but it only fires Touch_FrameReported with empty arguments.
So my question is, how do one fires the touch system events ? Is it possible with these classes, or should I go with windows messages (WM_TOUCH and WM_GESTURE) ?

XNA C# Controller Input Structures

I have looked all over the net and need some assistance. Is there a link somewhere to example code of all the buttons for the Xbox 360 controller. For example:
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
I know this exits the game if you press back on the controller.
I just need examples of states for both joysticks, d-pad, and all the buttons.
Have you checked out the MSDN XNA input documentation? It doesn't really have examples, but it appears you have the idea of what you do with the inputs.
The articles there have all the information on the inputs.
All the controller buttons which you can get to through GamePad.GetState(PlayerIndex.One).Buttons
The joystick which you can get through GamePad.GetState(PlayerIndex.One).Thumbsticks
And the D-Pad which you can get through GamePad.GetState(PlayerIndex.One).DPad
Also, it's a really good idea to write a wrapper for the buttons.
By this I mean: write a class that checks each button and keeps a state for the button being UP this frame and DOWN last frame, and then it can just report that the button was pushed.
This saves you having to check that every frame and maintain the state in your client code. Derive your class from IGameComponent and add it to your Game class' Component collections at runtime.

Categories

Resources