- Home /
Question by
unity_AafeYj5ClK8KNQ · May 27, 2020 at 10:02 AM ·
solution
My 2d game jump only one time
using UnityEngine; using System.Collections; using System.Drawing;
public class PlayerMovement : MonoBehaviour {
private bool Ground = true;
public float speed;
private Rigidbody2D Player;
public float jumpPower;
void Start()
{
Player = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector2 movement = new Vector2(moveHorizontal, moveVertical);
Player.AddForce(movement * speed);
if (Input.GetKey(KeyCode.RightArrow))
transform.Translate(Vector3.right * speed * Time.deltaTime);
if (Input.GetKey(KeyCode.LeftArrow))
transform.Translate(-Vector2.right * speed * Time.deltaTime);
GetComponent<Rigidbody2D>();
if (Input.GetKey(KeyCode.W) && Ground == true)
{
Player.AddForce(Vector2.up * jumpPower);
Ground = false;
}
Player.AddForce(new Vector2(0, -9.8f));
}
private void OnCollisionStay2D(Collision2D gg)
{
if (gg.gameObject.tag == "GrassThinSprite")
{
Ground = true;
}
}
} ,using UnityEngine; using System.Collections; using System.Drawing;
public class PlayerMovement : MonoBehaviour {
private bool Ground = true;
public float speed;
private Rigidbody2D Player;
public float jumpPower;
void Start()
{
Player = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector2 movement = new Vector2(moveHorizontal, moveVertical);
Player.AddForce(movement * speed);
if (Input.GetKey(KeyCode.RightArrow))
transform.Translate(Vector3.right * speed * Time.deltaTime);
if (Input.GetKey(KeyCode.LeftArrow))
transform.Translate(-Vector2.right * speed * Time.deltaTime);
GetComponent<Rigidbody2D>();
if (Input.GetKey(KeyCode.W) && Ground == true)
{
Player.AddForce(Vector2.up * jumpPower);
Ground = false;
}
Player.AddForce(new Vector2(0, -9.8f));
}
private void OnCollisionStay2D(Collision2D gg)
{
if (gg.gameObject.tag == "GrassThinSprite")
{
Ground = true;
}
}
}
Comment
Answer by Map-Builder · May 29, 2020 at 01:07 AM
"Question by unity_AafeYj5ClK8KNQ "
Question "Need this"
Body of question "Lagre code"
...
Replace GetKey by GetKeyDown, at your level it may do the trick