simple 2d jump does not work?
the below script is written for a simple jump when the player's 2d collider hits the grounds collider it works for sometime but after some iterations on continuously hitting the space the player remains static why is that so ? is raycast the only way we can do this?
public class jumper : MonoBehaviour { public float Jumpheight; private bool isJumping=false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Space) && isJumping == false) {
GetComponent<Rigidbody2D> ().AddForce (new Vector2(0, Jumpheight));
isJumping = true;
}
}
void OnCollisionEnter2D (Collision2D col)
{
if (col.transform.tag == "ground")
{
isJumping = false;
}
}
}
thanks in advance :)
Where does the player get stuck? Air or Ground?
Can you still move him sideways if that is an option.
What does a Debug.Log of isJumping in Update say?
Does OnCollisionEnter2D get called correctly? Check with a Debug.
After you have answered these questions we will look further :)
1> the player gets stuck on colliding with the ground
2>nope it is an infinite runner with scrolling background with no sideway movement
3>4> i runned debug it works fine for some time but it the OnCollisionEnter2D does not get called after certain iterations
i will send the screenshot
the script for the above is
using System.Collections;
public class jumper : $$anonymous$$onoBehaviour { public float Jumpheight; private bool isJumping=false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space) && isJumping == false) {
Debug.Log (1);
GetComponent<Rigidbody2D> ().AddForce (new Vector2(0, Jumpheight));
isJumping = true;
Debug.Log (2);
}
}
void OnCollisionEnter2D (Collision2D col)
{
Debug.Log (3);
if (col.transform.tag == "ground")
Debug.Log (4);
{
isJumping = false;
Debug.Log (5);
}
}
}
You're going to have trouble doing things that way.
1- You never check to see if the player left the ground, but your jump flag is only reset when they contact ground again. 2- http://docs.unity3d.com/ScriptReference/Force$$anonymous$$ode2D.Impulse.html
Consider using a bool to prevent multiple jump attempts, but you will want some way of resetting this in the event your player never leaves the ground at all.
So i need to check if player leaves the ground too? but the collider2d is not called at all after sometime even if i check if player leaves the ground or not would i be able to use collider2d? anyways i used raycast to solve my problem but i was just wondering why it cannot be done using collider2d although it works for sometime but why would it not call the OnCollision2D after few iterations is what i am confused about
first all thank you guys for such quick replies and support :)
for those who are wondering what could be the solution for this the below is the given script you can have double triple and as much as jump you want by using this code cheers happy coding :)
using UnityEngine; using System.Collections;
public class jumpdude : $$anonymous$$onoBehaviour {
public float jumpForce = 0f;
private Rigidbody2D rbody2D;
public Layer$$anonymous$$ask whatIsGround;
public bool grounded = false;
public GameObject groundCheckObject;
public float groundCheckDistance = 0f;
public bool can$$anonymous$$ultipleJump = false;
private int jumpCounter = 0;
public int maxJumps = 0;
// Use this for initialization
void Start () {
rbody2D = GetComponent<Rigidbody2D>();
jumpCounter = 1;
}
// Update is called once per frame
void Update () {
RaycastHit2D hit01 = Physics2D.Raycast (
groundCheckObject.transform.position,
Vector2.down,
groundCheckDistance,
whatIsGround);
grounded = (hit01.collider != null);
if (can$$anonymous$$ultipleJump) {
$$anonymous$$ultipleJump ();
} else {
singleJump ();
}
}
void singleJump()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space) && grounded) {
rbody2D.AddForce(Vector2.up * jumpForce);
}
}
void $$anonymous$$ultipleJump()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)){
if (jumpCounter < maxJumps) {
rbody2D.AddForce (Vector2.up * jumpForce);
jumpCounter++;
}
}
if(grounded)
{
jumpCounter = 1;
}
}
}
put the ground in the layer ground and number of max jump in the max jump tab and uncheck the max jump if you want single jump create a empty object as a child to the player and add it in the ground check object
happy coding cheers