- Home /
2d rigidbody jump not working
i am trying to make a platformer but my jump script is not working: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class player_move : MonoBehaviour
{
[SerializeField] private LayerMask platformLayerMask;
public bool canThrow = true;
public Transform throwPlat;
public bool hasJumped = false;
public bool hasJumpedOnPlat = false;
public float Dir;
public float speed = 5f;
private Rigidbody2D rb;
private BoxCollider2D boxCollider2d;
public float jumpSpeed = 30f;
public float JumpTime;
void Start()
{
jumpSpeed = 50f;
rb = GetComponent<Rigidbody2D>();
boxCollider2d = GetComponent<BoxCollider2D>();
}
void Update()
{
Dir = Input.GetAxisRaw("Horizontal");
if (Input.GetKeyDown("space") && canThrow)
{
canThrow = false;
ThrowPlatThrown();
}
if (isGrounded() && Input.GetKeyDown("w"))
{
jumpSpeed = 100f;
Debug.Log("start jump");
rb.velocity = Vector2.up * jumpSpeed;
}
}
public void ThrowPlatThrown()
{
float waitTime = 10f;
while (waitTime > 0f || hasJumped)
{
if (hasJumpedOnPlat)
{
hasJumpedOnPlat = false;
hasJumped = true;
}
waitTime -= Time.deltaTime;
}
Debug.Log("hasJumpedOnPlat");
}
private void FixedUpdate()
{
rb.velocity = new Vector2(Dir * speed, 0f);
}
private bool isGrounded()
{
float extraHeightText = .1f;
RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider2d.bounds.center, boxCollider2d.bounds.size, 0f, Vector2.down, extraHeightText, platformLayerMask);
Color rayColor;
if (raycastHit.collider != null)
{
rayColor = Color.green;
} else {
rayColor = Color.red;
}
Debug.DrawRay(boxCollider2d.bounds.center - new Vector3(boxCollider2d.bounds.extents.x, boxCollider2d.bounds.extents.y + extraHeightText), Vector2.right * (boxCollider2d.bounds.extents.x), rayColor);
//Debug.Log(raycastHit.collider);
return raycastHit.collider != null;
}
}
this is all of the code but the part that isnt working is:
if (isGrounded() && Input.GetKeyDown("w"))
{
jumpSpeed = 100f;
Debug.Log("start jump");
rb.velocity = Vector2.up * jumpSpeed;
}
on the screen the player is not jumping but upun verry close inspection the green box around the player grows, this might just be the boxxcast being called though. i have tried increasing the force but it doesnt work. also i tried changing to an addForce jump but it just started instantly teleporting the player up. a new jump script would work fine i just need it to be able to be called in random circumstances (through coroutines, not functions) but any help is good. gravity on rigidBody is 35, rotation is disabled, this is copied code, and i am on the newest version of unity.
Answer by Chimz · Sep 25, 2021 at 08:47 PM
I know you've tried addForce, but have you tried it in Impulse mode? Try this:
(jumpForce is your jumpSpeed)
rb.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
Your answer
Follow this Question
Related Questions
Raycast2D not working 1 Answer
Problem with Jump 1 Answer
Jumping only once (2D) 2 Answers
Choose Start and End Frame for 2D Animation 0 Answers
I have a problem with my jump script 1 Answer