- Home /
Question by
unity_L5_0HtWQlPgUVQ · Feb 05, 2021 at 02:19 PM ·
2d-platformer
How to make Mario style Type jump in 2D
So Then I tried to jump in unity. Sometimes my player just sticks to the ground. Here is my script. Pls, help me. Sorry for my English.
{
Rigidbody2D rigidbody2d;
public float speed, forceJump;
bool ground;
float jump;
// Use this for initialization
void Start()
{
rigidbody2d = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
float horizontal;
horizontal = Input.GetAxis("Horizontal");
jump = Input.GetAxis("Jump");
Vector2 position = rigidbody2d.position;
if ((ground == true) && (Input.GetButtonDown("Jump") == true))
{
rigidbody2d.velocity = (new Vector2(rigidbody2d.velocity.x, forceJump));
}
else if (Input.GetButtonUp("Jump"))
{
if (rigidbody2d.velocity.y > 0.5f)
{
rigidbody2d.velocity = (new Vector2(rigidbody2d.velocity.x, rigidbody2d.velocity.y - 2.5f));
}
}
position.x = position.x + horizontal * speed * Time.timeScale;
rigidbody2d.position = position;
}
private void OnCollisionEnter2D(Collision2D coll)
{
ground = true;
}
private void OnCollisionExit2D(Collision2D coll)
{
ground = false;
}
}
Comment