- Home /
Horizontal Rigidbody2D.Addforce not working, but vertical works fine?
EDIT! So I loaded up a FRESH PROJECT, created a circle and added my script to it. When I hit start the circle launches off at a 45 degree angle, it works perfectly fine. I also tried adding a horizontal addforce on another object in my original project and it resulted in a jittery teleport to the right.
Something is broken inside my project related to horizontal force... What could it be?!
This has driven me and a bud mad for 24 hours now and I've searched high and low but have not found any similar problems or applicable solutions.
I'm developing a mario-style side scroller game and the concept is simple: Hit the box, a coin pops out with vertical and horizontal force. This is the ONLY code so far in the script attached to the coin.
void Start()
{
forceUp = new Vector2(0, upForce);
forceSide = new Vector2(sideForce, 0);
GetComponent<Rigidbody2D>().AddForce(forceUp, ForceMode2D.Impulse);
GetComponent<Rigidbody2D>().AddForce(forceSide, ForceMode2D.Impulse);
}
This is the code that instantiates the coin in the box script:
private void OnCollisionEnter2D(Collision2D collision)
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down, 1.0f, layerMask);
if (!isOpen && hit.collider != null)
{
isOpened();
Instantiate(openPrize, new Vector3(transform.position.x, transform.position.y+1.0f, transform.position.z), transform.rotation);
}
}
problem is: The coin will pop "up" a distance directly related to the value I plug into the "up force" variable.
The coin will not move horizontally at all. If I jack the horizontal variable up to some stupid number like 5000, it will teleport a short distance to the side.
$$anonymous$$aybe joining the forces into a single vector helps
force = new Vector2(sideForce, upForce);
GetComponent<Rigidbody2D>().AddForce(force , Force$$anonymous$$ode2D.Impulse);
That's how it was initially. I separated the values to try to make it work.
Try creating a Physics $$anonymous$$aterial 2D, removing all friction and adding it to the object's rigidbody. maybe it's friction behavior.
Answer by logicandchaos · Jan 12, 2021 at 05:51 PM
It's because you are doing it in 2 steps I think Try
GetComponent<Rigidbody2D>().AddForce(new Vector2(sideForce, upForce), ForceMode2D.Impulse);
in your Start method, delete the rest.
Nope. I've tried it both ways. It was originally how you describe it.
I think it's your other script, this line here: Instantiate(openPrize, new Vector3(transform.position.x, transform.position.y+1.0f, transform.position.z), transform.rotation); you instantiate it to position transform.position.x, transform.position.y+1.0f, you could also try setting the velocity instead of AddForce which I find works a lot better for me. O maybe different types of force modes. Also check your object to see if there are restraints set in the inspector.
Your answer
![](https://koobas.hobune.stream/wayback/20220613023142im_/https://answers.unity.com/themes/thub/images/avi.jpg)