Question by
shubham112 · Jun 14, 2019 at 07:10 AM ·
unity 52d gamejumpingif statement
Why 2d jump not working on touch?
First of all, here's my code :
public class PlayerController : MonoBehaviour
{
private Rigidbody2D rb;
public float speed;
public int jump;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
private bool onGround = true;
public float yMin, yMax;
private const int max_jump = 2;
private int currentJump = 0;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
rb.velocity = new Vector2(speed, rb.velocity.y);
onGround = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
}
private void FixedUpdate()
{
rb.position = new Vector2
(
Mathf.Clamp(rb.position.x, -5.0f, 5000000.0f),
Mathf.Clamp(rb.position.y, yMin, yMax)
);
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began && (onGround || max_jump > currentJump))
{
//Touch touch = Input.GetTouch(0);
//Debug.Log("Touch Working");
rb.AddForce(Vector2.up * jump);
onGround = false;
currentJump++;
}
private void OnCollisionEnter2D(Collision2D collision)
{
// Debug.Log("Collision Working");
onGround = true;
currentJump = 0;
}
}
Now, when i build and run in my phone and touch the screen, it just doesn't jump. What could be the issue here? Also, should I use Raycast instead of this? If yes, then why? Thanks in advance.
Comment
Did you copy the code? if yes, are you using oncollisionenter as a local function for any reason? i doubt they cant be used as local functions.
Your answer

Follow this Question
Related Questions
Player can only jump once!? 0 Answers
Fast start and ending at jump but slow middle. 0 Answers
[2D] Slide object after collision 2 Answers
2D sidescrolling game. Jumping problems through platforms and platform effector. 0 Answers
Broken Jump Physics 0 Answers