- Home /
Make player jump when space button is pressed?
I want to make my player jump from one block to another when space button is pressed. When the button is pressed player moves x direction and y direction to make the feel of jumping;
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public float jumpForce = 6.0f;
private bool jump;
void Update(){
if (Input.GetKeyDown (KeyCode.Space)) {
jump = true;
}
}
void FixedUpdate(){
if (jump) {
Jump();
jump = false;
}
}
void Jump(){
Vector2 jumping = rigidbody2D.velocity;
jumping.y = transform.position.y + jumpForce;
jumping.x = transform.position.x + jumpForce;
rigidbody2D.velocity = jumping;
}
}
Comment
Please, make some research before posting a question :
Your answer

Follow this Question
Related Questions
Why does my character never stop jumping? 0 Answers
How to make the player turn in the air 1 Answer
Player movement 0 Answers
How to prevent player from moving in air while jumping in place?? 0 Answers
Detecting Collision for Jumping 1 Answer