0
votes

Someone can tell me what is the problem with this code that's work but my player fly to the sky. I'm just started with codding but pls help me.

the start of this code start down here:

using System.Collections;

using System.Collections.Generic; using UnityEngine;

public class PlayerController : MonoBehaviour {

private Rigidbody2D rb;
public float speed;
public float jumpForce;
private float moveInput;

private bool isGrounded;
public Transform feetPos;
public float checkRadius;
public LayerMask whatIsGround;

private float jumpTimeCounter;
public float jumpTime;
private bool isJumpimg;


void Start()
{
    rb = GetComponent<Rigidbody2D>();

}

private void FixedUpdate()
{
    moveInput = Input.GetAxisRaw("Horizontal");
    rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
}

void Update()
{
    isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);

    if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
        isJumpimg = true;
    jumpTimeCounter = jumpTime;
        rb.velocity = Vector2.up * jumpForce;
    {

    }

    if (Input.GetKey(KeyCode.Space) && isJumpimg == true)

        if (jumpTimeCounter > 0)
        {
            rb.velocity = Vector2.up * jumpForce;
            jumpTimeCounter -= Time.deltaTime;
        } else
            isJumpimg = false;
    {

        {
        }
        }

    {
        if (Input.GetKeyUp(KeyCode.Space))
        {
            isJumpimg = false;
        }     
    }
}

}

1

1 Answers

2
votes

If this code is copied and pasted from your project I can see your problem. If not this may just be an accidental formatting problem when putting it on SO.

Lets go through this section line by line.

if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
    isJumpimg = true;
jumpTimeCounter = jumpTime;
    rb.velocity = Vector2.up * jumpForce;
{

}
  • You do your if condition to check if you are grounded and if the jump key is pressed
  • You don't open a scope with {} curly brackets. you instead take one indentation. When you do this with an if statement you are saying that only one line is relevant to the if statement. Anything after the first line is treated as though it will always happen.
  • You set the jumpTimeCounter variable which would normally be fine if it was inside an if statement.
  • You set the the velocity of the rigidbody to Vector2.up * jumpForce. This happens every frame as it's not inside an if statement so you fly off as soon as you load in.
  • You open and close a scope with {} but there is nothing in it and it is not attached to a loop or if statement.

The solution: Change the above section to this.

if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
{
    isJumpimg = true;
    jumpTimeCounter = jumpTime;
    rb.velocity = Vector2.up * jumpForce;
}

And be more careful about putting logic which is only supposed to happen under certain conditions inside of scopes controlled by if statements or loops. Don't worry, this is a common learner mistake. But now you may be more aware that all of the syntax like {}, (), and ; is there to help you tell the compiler what you intend the code to do.