Hey I am very new to Unity and have tried everything I can but my player at some point just stopped being able to jump on my 2d tile map blocks while I am moving to the left or right
my ground check is rigidbody.velocity.y == 0 which I know isn’t good but at least worked before
void Start()
{
rigidbody = GetComponent();
}
// Update is called once per frame
void Update()
{
if (isTalking)
{
TextPanel.SetActive(true);
InteractText.SetActive(false);
}
else
{
if (canInteract)
{
InteractText.SetActive(true);
if (Input.GetKeyDown(KeyCode.X))
{
isTalking = true;
}
}
else
{
InteractText.SetActive(false);
}
}
if (!isTalking)
CheckInputs();
else
{
left = false;
right = false;
}
}
private void FixedUpdate()
{
if (!dashing)
{
if (left)
{
facingRight = false;
//adds velocity so I am able to move left
rigidbody.velocity = new Vector2(-speed, rigidbody.velocity.y);
}
else if (right)
{
facingRight = true;
//adds velocity so I am able to move right
rigidbody.velocity = new Vector2(speed, rigidbody.velocity.y);
}
else
{
//removes velocity
rigidbody.velocity = new Vector2(0, rigidbody.velocity.y);
}
}
//if (Physics2D.Linecast(transform.position, transform.position - new Vector3(1, 1, 0), 1 << LayerMask.NameToLayer(“Ground”)) && rigidbody.velocity.y == 0
//|| Physics2D.Linecast(transform.position, transform.position - new Vector3(-1, 1, 0), 1 << LayerMask.NameToLayer(“Ground”)) && rigidbody.velocity.y == 0)
if (rigidbody.velocity.y == 0)
{
//if I am touching any gamobjects layred ground I am Grounded
grounded = true;
}
else
{
//if I am not touching any gamobjects layred ground I am not Grounded
grounded = false;
}
if(jump)
{
//makes it so my charather jumps x1 jump height for when I do a normal jump
Jump(jumpPower);
}
}
void Jump(float power)
{
//adds upwards force for when I am jumping, and we make it like this so we can call jump at multiple instances but with diffrent power (normal spacebar jump and for the Bouncepad)
rigidbody.AddForce(new Vector2(0, power));
jump = false;
}
void CheckInputs()
{
if (!dashing)
{
if (Input.GetKey(KeyCode.LeftArrow))
{
//When pressing left arrow left = true running the phyischs for my charather to walk left
left = true;
right = false;
}
else if (Input.GetKey(KeyCode.RightArrow))
{
//When pressing left arrow right = true running the phyischs for my charather to walk right
right = true;
left = false;
}
else
{
//no code is runned and I stand still
left = false;
right = false;
}
}
if (Input.GetKey(KeyCode.Space))
{
//íf jump is fall and charather is grounded you are allowed to jump(makig sure I can’t infinty jump and such)
if (!jump && grounded)
jump = true;
}
if (Input.GetKeyDown(KeyCode.C))
{
if (!dashing && canDash)
StartCoroutine(Dash());
}
}
IEnumerator Dash()
{
dashing = true;
canDash = false;
if (facingRight)
{
rigidbody.velocity = new Vector2(dashSpeed, rigidbody.velocity.y);
}
else
{
rigidbody.velocity = new Vector2(-dashSpeed, rigidbody.velocity.y);
}
yield return new WaitForSeconds(dashLength);
dashing = false;
StartCoroutine(DashCooldown());
}
IEnumerator DashCooldown()
{
yield return new WaitForSeconds(dashLength);
canDash = true;
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == “NPC”)
{
//The player can interact with all GameObecjts that has the tag NPC
canInteract = true;
}
if (collision.tag == “BouncePad”)
{
//when colliding with tag BouncePad doubble my jumping heigth for that bounce
//sets the old velocity to zero so the new velocity can be applied without coliding with the old velocity
rigidbody.velocity = new Vector2(rigidbody.velocity.x, 0);
Jump(bouncePower * 2);
}
if(collision.tag == “Respawn”)
{
respawnPosistion = collision.transform.position;
}
if (collision.tag == “Hazard”)
{
transform.position = respawnPosistion;
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if(collision.tag == “NPC”)
{
//removes the abillity to interact when exiting a GameObeject with the tah NPC
canInteract = false;
}
}
}