Keymapping in unity - c#

I'm working on a game in unity. I have several things that are supposed to happen when a certain key is pressed, like w to move forward. The problem, is that some people might prefer a different key layout and different buttons for different actions.
I saw lots of unity games where you're able to choose yourself your prefered keys, however I can't seem to find a explanaiton of how to do so.
Is there a way of creating some kind of menu, where when you press on a certain action name, unity then, would record your input, and during the game check if that button is pressed?

You can create your own script what wil handle it.Most easy way to save your keys to json for example and every time game load then this file is loaded.And when user change key binding then you save it to that file.
For example if you want change jump, then you select jump in your menu , let user press button he want and then save button he pressed to that file.So for jump will be used this button.
You can use this
https://docs.unity3d.com/ScriptReference/Event-keyCode.html

Related

How to detect if button was not hit / how to make something fade away if not clicked

Hi i am making a card inventory where i have 4 selected cards and a grid of cards to select from. when pressing a card i have a button that popps up that you can press to select the card, but my problem is that i have no clue how to make that button go away.
i want to have a select button show up when pressing a card, and pressing another will make it go away, or pressing anything else will make it go away. but i have no clue how to.
the dropdown ui template ha this built inn, when clicking the dropdown the options show up, when clicking anything else it goes away, i want that.
edit: for further clarification, i have 2 buttons, 1 makes the other one visable, the other one is going to go away when clicking anything else than the button itself. that is all i have
any idea random wonderful strangers on the internett
love
As I misunderstood your problem the first time, I'll write it the second time.
I think this is really simple to solve, so basically create a huge and transparent button which will detect all of the clicks besides from the cards and buttons.
So my solution is:
Create a button called for example "Background" and set its scale to an enormous values like 1000 to cover the whole screen all time.
Set all of the button's colors (Normal Color, Highlighted, Pressed, etc.) to transparent - basically set the Alpha value to 0
The button has to be above all of the other buttons in the Hierarchy of Canvas (or however you called it) in order to work as a background and not cover up other buttons.
Detect the clicks on the "Background", which will just hide the "Select" and the "Info" button.
I hope this time it will work.

Why cant I add keyframes to my animator in Unity3D?

I have a Unity3D animation, but, I can't add keyframes, it only gives me the option of Add Animation Event.
I also noticed that in the Animation component of the object, It doesn't allow me to check the box of enabled, If I turn it on, the checkbox becomes red and when I turn on/off the record keyframe mode the checkbox automatically unchecks...
I dont know if it could be related, but, this object was created copypasting of another object that has animations, I just deleted all of them and created new ones, but I dont know if this could have affected the object, because I have 2 objects create the same way, and they allow me to put keyframes.
You should set up your animation like the following:
Select the object you want to animate.
Go to the animation window, click 'Create'.
Save the animation to the desired folder. You should now have an animator file and an animation file.
Be sure the object is selected and add the desired properties on the left and everything should work just fine.
Hope this helps

Unity Prefab Issue, different reactions from different prefabs

So I've hit a wall in a unity project, and I've been unable to even find a similar problem. I'm not sure how to easily describe it, but here goes:
I have a prefab (a simple 3D object triggered on mouse click/finger tap). It is meant to act as a button (it cannot be a UI button for various reasons). I have a bunch of them. Each button has a script attached, so that when the button is pressed, it will display some text and and some images. However, this is not working.
To test it, I put down a few of these buttons. I filled in the script box for two of them, lets call them A and B. They were directed to the same UI elements, but each was given a different set of images to produce. However, when I test it, it only calls the images from B. If A is the only one set up, all the buttons call from A, even without a script attached.
I need some way to refresh the assigned starting "values" for the variables. So that it will pull from what I assigned them in the editor only when clicked on, and not before. Any help or suggestions would be appreciated.
Try this link Instantiating Prefabs from the unity site.
A possible solution would be to set a global Boolean variable for each button (prefab). If button 1 is active, set the variable to true and in for buttons 2 and 3 make them turn to false for inactive.
I suggest designing your script this way for each button. After the button is pressed refreshed the values in the variable for each button and check these values again once a button is pressed.
You seem to be almost there.
public class A : Mono {
public ImageToShow Image; // Assign this in inspector
void OnMouseDown() {
Image.DoYourThing(); // Do whatever you need to here for the image
}
}
When this is on many different "Prefabs" if you handle the prefabs correctly, each button will have its own instance with its own reference to the image you assigned inside the inspector.
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html
Link for OnMouseDown.
Each button can have the SAME script, you just neeed to make sure that each prefab instance you have SAVED has the correct image assigned to its script.
Hope this helps.
Edit: Doing it this way, each button will be "active" but wont do anything if not tapped. If you tap or click an inactive game object (which is your button because its not a GUI element) no method on that object will be called.

How do I make a button seem like pressed and then unpressed from code naturally in c# for WP7?

I am trying to make buttons seem like pressed automatically so the user can then follow the buttons pressed but everytime I try it doesn't seem natural because the computer does it really fast all the changes!
Any ideas? I have to get a serious of buttons pressed that look like user pressed and then pass to unpressed again
Thanks a lot!!!!
Well I'm trying to make a game where you remember the buttons that were randomly selected by the computer and then you press them, so I need computer to act like if the user were touching them and then the user follows...
How about using a custom storyboard animation that takes the button to the pressed, and then unpressed state?

How to detect more than one keypress simultaneously in C#?

I am working on a map editor for my game and I use the arrow keys to scroll around the map area. Currently I'm using the KeyDown event, which works fine for scrolling up, down, left or right - but my question would be how to allow diagonal scrolling. Currently, if I hold the right-arrow and then (whilst keeping the right-arrow held) I then press and hold the down-arrow, the down direction replaces the scrolling to the right instead of scrolling diagonally to the bottom right.
Is there a way, for instance, that I could check whether another arrow key is being pressed whilst in the KeyDown event? How can I respond to more than one key being held?
Thanks
Not easily, the key-down of the 2nd keystroke stops the 1st one from repeating. What you have to do is record that the key is down in the KeyDown event. Cancel that state in the KeyUp event. Next, use a Timer (~250 msec) and in its Tick event check the state of the keys.
The easiest way to do this is to use a Windows API call to get the state of every key on they keyboard, and then check the keys you are interested in: GetKeyboardState via PInvoke.
You can call it on KeyDown/KeyUp, check the state of the keyboard, and act appropriately.

Categories

Resources