Home>
I was able to do a jump operation in the unity 2D action game development,
I kept jumping on the directional keys and jumped many times.
How can I make one jump when I press the arrow key once?
*Because I am a beginner to programming, I do not know how to change the operation key input. .. ..
If i keep pressing the arrow keys, you will repeatedly jump.
Applicable source code // act when keyed
float horizontalKey = Input.GetAxis("Horizontal");
float verticalKey = Input.GetAxis("Vertical");
float xSpeed = 0.0f;
float ySpeed = -gravity;
if (isGround)
{
if (verticalKey >0)
{
ySpeed = jumpSpeed;
jumpPos = transform.position.y;// record the jumped position
isJump = true;
jumpTime = 0.0f;
}
else
{
isJump = false;
}
}
else if (isJump)
{
// Is the up arrow key pressed?
bool pushUpKey = verticalKey>0;
// Is the current height below the flight height
bool canHeight = jumpPos + jumpHeight>transform.position.y;
// Is the jump time too long?
bool canTime = jumpLimitTime>jumpTime;
if (pushUpKey&&canHeight&&canTime&&!isHead)
{
ySpeed = jumpSpeed;
jumpTime += Time.deltaTime;
}
else
{
isJump = false;
jumpTime = 0.0f;
}
}
What I tried
Key input method
if(Input.GetKeyDown("Jump"))
I also tried replacing it with
I got an error and it didn't work.
Please provide more detailed information here.
-
Answer # 1
Related articles
- unity ui: button does not respond when clicked
- c# - navigation setting of button does not work well in unity ui
- unity - want to change the button image on the screen by acquiring the image of the object in the list
- c # - about action games (unity 2d platformer controller) in unity
- ios - i want to add action to the alert button
- unity - direction to make button images shine periodically
- c # - [unity] i want to push the button attached to the dynamically generated prefab to destroy another dynamically generated pr
- c # - wait for input until unity button is pressed
- c # - i want to play the animation by pressing the [unity] button
- unity - attack se is played by pressing the button on the result screen
- [unity] how to select ui button when ui button is activated/deactivated
- unity2d - 2d unity game button display hiding does not work
- [swift] i want to implement the action of the toolbar button set in xib
- i want to implement a java button action
- unity - text of ui changed by processing of button placed by editor extension is not updated
- c # - unity 2d action game i want to avoid slipping on a slope
- i can't successfully attach a method to a button ui in unity
- i can't use anykeydown and button click process together in unity
- unity - i don't know how to attach a method and target button object to onclick from script
Related questions
- c # - player production in unity (shooting game)
- c # - unity) i want to get the image size in prefab
- a question about unity please tell me how to display animatorsettrigger in onclick() of button
- c# - [unity] hit detection of tile map after adding composite collider 2d
- c# - method detection by instantiated gameobjects
- c # - [unity 2d] i want to create a gimmick that clings to a rope, hangs, swings like a pendulum, and jumps
- i want the button to proceed to the next in unity to appear when the game clear is displayed in the ui
- how do i place sprites responsively in unity2d?
- c # - unity) video thumbnails
- c # - unity) show/hide prefab button conditionally
As a general method, the character's ground contact is determined. That is, determine if the character is standing on the ground. And make sure you only jump when you are standing on the ground.
Unity 2D If you googling with the ground contact judgment, there will be a lot of hits, but here is an example.
How to make Unity 2D action [ground contact judgment]