- Home /
Problem with player bouncing or jumping
So, I have tried all kinds of scripts for player to jump upwards or bounce off of a platform but none of them worked.
When I press Play, it looks like something is blocking my player from jumping or bouncing up.
This is the script from tutorial I found on YT, and it works fine for that person but not for me. I'm not sure what could be the problem but I think the problem is not in the script.
private void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.GetComponent().velocity.y <= 0) { collision.gameObject.GetComponent().AddForce(Vector3.up * 60f); } }
Can you show or tell me what components are attached to your gameObject by going to the 'Inspector' tab? :)
Answer by josi1301 · Jul 06, 2019 at 06:12 PM
I have managed to fix it. If anyone has the same problem I'll write here the code that is attached to my platform.
public float jumpForce = 10f;
void OnCollisionEnter2D(Collision2D collision)
{
Rigidbody2D rb = collision.collider.GetComponent<Rigidbody2D>();
if(rb != null)
{
Vector2 velocity = rb.velocity;
velocity.y = jumpForce;
rb.velocity = velocity;
}
}
Your answer
Follow this Question
Related Questions
Slowly move a GameObject on 1 axis, then destroy it. 1 Answer
Dodge movement - restrict movement of an object left and right 0 Answers
How can i make diagonal movement? 1 Answer
Player won't stop moving while blocking 1 Answer
Move Character to touched Position 2D (Without RigidBody and Animated Movement) 1 Answer