Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

622
Visualizações
Unity 2D: Rigidbody2D 'addforce' works vertically but not horizontally?

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.

the animator of the prefab has "Apply root motion" enabled.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I figured it out... There is a setting in the animator of the prefab im using called "Apply root motion". I have never used it, dunno what it does but it was checked by default in the asset i imported and that was somehow stopping the movement. Thanks for your help!

over 4 years ago · Santiago Trujillo Relatório

0

I think your forcemode is probably not right.

Docs:

Apply the impulse force instantly. [...] This mode is useful for applying forces that happen instantly, such as forces from explosions or collisions.

Probabaly ForceMode2D.Force is better.

This mode is useful for setting up realistic physics where it takes more force to move heavier objects.

But maybe it's because you apply to forces at once. Try this:

// Force up
await Task.Delay(TimeSpan.FromSeconds(1));
// Force side
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda