- Home /
Question by
Dracolyte · Nov 21, 2020 at 11:02 AM ·
2d gamerigidbody2daddforce
How to calculate the rb.AddForce for my game?
I want to create a 2d clicker game, where you launch the player forwards to destroy walls. I have two problems right now: I dont know how to calculate the rb.AddForce so, that it always hits the wall with one click (If I increase the force, the player gets launched far back because of the physics material or he glitches through the wall). My second problem is, that I dont know how to get the player smoothly in his exact original position after he hits the wall. It sounds very wierd but I hope that you can Imagine what I want to create..
This is the Project right now: https://streamable.com/76dujj
Thats my code:
public Rigidbody2D rb;
public float force;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
TouchAttack();
}
}
void TouchAttack()
{
rb.AddForce(Vector2.right * force, ForceMode2D.Impulse );
}
}
Comment
Answer by MurphyMurph_21 · Nov 21, 2020 at 01:01 PM
@Dracolyte This should at least make it smooth
void FixedUpdate()
{
if (Input.GetMouseButtonDown(0))
{
TouchAttack();
}
}
void TouchAttack()
{
rb.AddForce(Vector2.right * force, ForceMode2D.Impulse ) * Time.fixedDeltaTime;
}