Question by
myC4 · Jun 27, 2020 at 09:19 AM ·
rigidbody2dmovement scriptgroundrigidbody-collision
How do I prevent a rigidbody2D from falling through the ground?
I have the following script:
public float speed = 1;
public float jumpForce = 1;
private Rigidbody2D playerBody;
private float trueSpeed;
private float trueJumpForce;
void Start()
{
playerBody = GetComponent<Rigidbody2D>();
}
void Update()
{
if (Input.GetKeyDown("d"))
{
trueSpeed = speed * Time.deltaTime;
}
else if (Input.GetKeyDown("a"))
{
trueSpeed = speed * -1 * Time.deltaTime;
}
else if (Input.GetKeyDown("w"))
{
trueJumpForce = jumpForce * Time.deltaTime;
}
else
{
trueSpeed = 0.0f;
trueJumpForce = 0.0f;
}
playerBody.AddForce(new Vector2(trueSpeed, trueJumpForce), ForceMode2D.Impulse);
}
but if I run it, the player will fall through everything. I have a ground element which it should land on, but how do I do that?
Comment
Your answer
Follow this Question
Related Questions
Limiting backwards speed ,Limiting backwards speed 0 Answers
My player's rigidbody 2d is being continuously pushed downwards by box collider 2d 1 Answer
Y-axis movement stuck. Help me please :c,Not Moving by pressing Button :C. Help me please 0 Answers
Rigidbody2d.AddForce wont move character 0 Answers
2D Physics not working correctly 1 Answer