Tap to Jump

Hello,
I’ve got a “tapToJump” script that works when i attach it directly to my character sprite, but when I attach it to a gameObject that contains my character sprite it doesn’t work anymore, well it does a lot of nonsense things.
I don’t think the problem is in my code, you can check if you want,
So if you have any clues that could help me, I would be grateful ! Can I attach my script to a game object that contains my character to be able to move him ?

Here’s my code, and also how can I put the code in a bbcode for codes ?

private void FixedUpdate()
{
grounded = Physics2D.OverlapCircle(groundCheck.position,
groundCheckRadius, whatIsGround);
}

void Update () {

if (grounded)
doubleJump = false;

if ((Input.touchCount == 1 && grounded) || (Input.touchCount == 1 && !grounded && !doubleJump))
{
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
GetComponent().velocity = new Vector2(
GetComponent().velocity.x, jump);
doubleJump = true;
}
grounded = false;
}
}

You can turn that into a code block with the insert icon (3 icons to the right of the smiley). Your code expects a Rigidbody2D component to be on the same GameObject as the script, so maybe that is your problem. If you want your script to be on a different GameObject than your Rigidbody2D, you’ll have to have a reference to the Rigidbody2D’s GameObject through a public variable or some other way. Typically the Rigidbody2D will be on the top level parent object if it is going to work correctly, but it is ok to have your sprite in a child of that.

1 Like