I am attempting to make a simple third person camera rotate around my character. I want the X key to rotate right and the Z key to rotate left. I have this working. My issue is, when I have one of the keys pressed and held (say X), then press any other key, Input.GetKey(KeyCode.X) stops returning true. So for the following simple example, it would print the message until another key is pressed, then will not again until I re-press the key.
void LateUpdate()
{
if(Input.GetKey(KeyCode.X))
{
print("X is down");
}
}
This prevents me from being able to rotate the camera while moving my character, for as soon as I attempt to change directions via an arrow key, it renders my if statement false. Is there another method I should be using?
Edit - This is not an issue with Unity or my code. I was using Teamviewer, which must handle the inputs differently
This works for me. Could it be you are using else if while checking if the button is pressed?
if (Input.GetKey(KeyCode.X))
{
print("X is down");
}
if (Input.GetKey(KeyCode.Y))
{
print("Y is down");
}
It also works for me if i have only 1 if. Even if i press any other key "X is down" is printed.
This could be a hardware issue. Different quality levels of keyboards have different behavior. For example, I once had a keyboard where if I pressed more than three keys at the same time, it would not register when they were released, and my character would be stuck running or spinning or whatever until I pressed and released the key again. Expensive "gaming" keyboards should handle multiple simultaneous keypresses better than others. YMMV.
It ended up being due to remote access. I was using Teamviewer to remote into the laptop that was running Unity. Once I used the laptop directly it worked as expected.
Related
In my game you need to fix things by pressing X. When X is pressed an animation of the guy fixing is played, then the text changes for the number of things left to fix, then the broken thing's game object is destroyed.
The issue I'm having is if the player spams X when fixing one thing it minuses the amount left enough to win the game without having to fix the other things (if that makes sense).
I have tried adding delays and stopping time, which haven't had the desired result.
I just had to change my fixhole bool to false!
The following code locks the cursor only when I left click my mouse after pressing Escape Key. Can anyone suggest me a way to overcome this problem?
public class lockCursor : MonoBehaviour
{
public bool locked = true;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (locked == false)
{
Debug.Log("locked");
Cursor.lockState = CursorLockMode.Locked;
locked = true;
}
else
{
Debug.Log("Unlocked");
Cursor.lockState = CursorLockMode.None;
locked = false;
}
}
}
}
As said in the API for Cursor.lockState
In the Editor the cursor is automatically reset when escape is pressed, or on switching applications.
Due to escape having a special role itself within the Unity Editor the GameView loses focus when pressing it.
There is not really a (trivial) way around this since it would require to intercept they keypress before the Unity Editor itself gets the chance to handle that escape press. (Which would always happen before it is passed on to the player -> your code.)
What you can do however would be simply use a different KeyCode only for the editor and escape only in a build like e.g.
#if UNITY_EDITOR
if(Input.GetKeyDown(KeyCode.E))
#else
if(Input.GetKeyDown(KeyCode.Escape))
#endif
{
...
You can replace the E with any other KeyCode you wish to use to "simulate the escape key" only within the Unity Editor itself. (Also see Platform dependent compilation)
As derHugo mentioned in his comment, your issue lies in the API for Cursor.lockState where it says:
"In the Editor the cursor is automatically reset when escape is
pressed, or on switching applications. In the Standalone Player you
have full control over the mouse cursor, but switching applications
still resets the cursor."
When you go to press Escape, it removes you from the application/Game Window. This is why it'll appear like your cursor is no longer locked. This is also why it goes into it's locked state as soon as you click back into the game. The same thing can be demonstrated by using your code, starting the game and then switching into another window on another monitor. Then move your mouse back over to the screen where the Game Window is. It'll appear as though it is no longer in a locked state... that is until you click in the game and it recognizes the cursor again.
To solve your problem, I would recommend using any key other than KeyCode.Escape. Maybe KeyCode.Space like the API shows.
Also, to further demonstrate the issue here, once you switch the code to use KeyCode.Space, press Space to lock the cursor and then try pressing Escape. Again you'll see that it appears to be unlocked until you press the Left Mouse Button in the Game Window again. This is because the Game thinks that you are no longer in the Game Window once you press Escape.
Hope this helps!
(check image) Hi! I actually have two questions,
THE FIRST ONE: I have two flash animations that I want to play in front of the tank's turret whenever I shoot a bullet (I have different weapons with different fire delays, machine gun, bombs..) so how can I play that flash animation to work in with the fire delay of a gun ? (note that if I uncheck loop time, the animation stops in the last sprite + I don't know how to replay it).
SECOND QUESTION: I want simply to animate my turret when I shoot I just want it to go back a little and return, you know what I mean, only in unity, how, please?
give me just the idea of working and I'll do my research :) thank you!
I have tried to enable my animation when I shoot and uncheck the loop time, but the animation stops at the last animation sprite and I don't know how to replay it.
Using Unity's built in animator, I'm assuming you are using this, you need to have at least have 2 states, in your case Idle and Fire. You would then set the transitions of the animations to go from one another, Fire -> Idle and Idle -> Fire.
Within the Idle-> Fire you will need add a trigger due to it only happening once.
You may also need 2 SpriteRenderer Components to allow for the display of the Shot effect, just make a child GameObject with it and edit it in the Animation Window.
Triggers reset as soon as the transition has been made.
If you do not have a trigger go to the parameter tab and click the '+' button which will allow you to create a new trigger. Now that that is created, in your code whenever you want your tank / boat to fire just add. referenceToAnimator.SetTrigger ("triggerName");
Bringing up your delay you could always add an additional wait state that mirrors the idle state but only runs for the selected amount of time. Your new Animator would look like.
Idle -> Trigger -> Wait -> Fire -> Idle
Recently I am trying to make basic games in Console-Application, like to move the cursor with the arrows (maybe as a very basic start of snake and games that you move something all over the Console screen), and I wanted to ask if it's possible to make that the cursor won't stop to move, and just change the direction when I press on some arrow? Is it something with events?
Thanks.
I would suggest something like the following:
bool isRight, isUp; //Used to determine direction
while (gamePlaying)
{
if(Console.KeyAvailable)
{
Console.ReadKey(); //Do your thing in here
}
}
So you check if a key is being pressed, and if it is you read it.
Also, bear in mind if you're making a console game you'll want to double-buffer your screen and use the Win32 API to minimise write calls if possible or your screen with flicker horribly.
I do not believe there is anyway to control the cursor position of the console window. You can read or write text, but that's about it. So if you want to make a console game, it will need to be text-based. You may be able to make an game ASCII art based where new text is printed to the console fast enough that it appears to be animated, but I don't think the console can go fast enough to make this work well.
For instance, consider a brick breaker game where once the ball falls below the paddle, the user would get a displayed message (using SpriteFont) asking whether he would like to play again. If that condition was met,and the sprite font stated 'Play again, enter 'y' for yes, 'n' for no' how would one proceed to take that input in XNA?
Well, first you're gonna want to have a boolean to see if the game has ended or not. If you didn't have the boolean then the user could press Y or N anytime and the game would restart.
It should be simple to setup one anyways, just have it detect when the ball falls below the paddle and set the bool to true;
Then, the rest should be quite simple to figure out.
if (gameRestart)
{
if (Keyboard.GetState().IsKeyDown(Keys.Y))
{
// code for restart goes here
}
else if (Keyboard.GetState().IsKeyDown(Keys.N))
{
// do nothing, I guess?
}
}
Here is a tutorial on detecting key presses in XNA: http://msdn.microsoft.com/en-us/library/bb203902.aspx