Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Can someone explain me this (Physics and forces)

Discussion in '2D' started by migbernabeumor, Aug 8, 2019.

  1. migbernabeumor

    migbernabeumor

    Joined:
    Apr 7, 2019
    Posts:
    16
    Hello im new in game development and i have some doubts about physics and forces.

    Im making a game(platformer 2d) where my player has movement like mario but when the player clicks in one direction the character does and attack in that direction with a little jump, my problem begins here:

    -When the character jumps and i click in the direction of the jump the character makes a jump with a lot of force.
    -And when i use 2 or more times the attack sideways in the air the little jump becomes bigger.

    Im using myBody.AddForce(Vector2,ForceMode2D.Impulse) to make my jump and my attack.

    Thank you for read this.
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    102
    Have you defined an amount you want to jump? I.E. a float/int called jumpForce? Here is what i do:
    Code (CSharp):
    1.  rb.AddForce(new Vector2(0, jumpForce), ForceMode2D.Force);
    jumpForce is a public variable i can change in the inspector to adjust the height my character goes. Also, are you checking with a bool to see if the player is "grounded" or not?
     
  3. migbernabeumor

    migbernabeumor

    Joined:
    Apr 7, 2019
    Posts:
    16
    Hello Cornysam(and others users),

    I think i didnt explain myself quite well sorry, i will try to clear out my problem, my jump works perfectly and my attack too.

    My problem is when i jump and attack at the same time because i add an impulse to my RigidBody2D with the jump and my attack adds a mini dash with an impulse to my RigidBody2D in the direction i want to attack(with the mouse).

    Then, both forces sum their velocity and acceleration, and create a bigger impulse.

    I'm asking if someone knows a way to avoid that both forces to sum and create this undesired effect.

    Please make me know if i explained myself good this time thank you for help.
     
  4. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    102
    Ahh i think i understand now. Can you post you code so we can better see where these forces may be adding up?
     
  5. migbernabeumor

    migbernabeumor

    Joined:
    Apr 7, 2019
    Posts:
    16
    My variables are in spanish sorry, this is my jump:


    Code (CSharp):
    1. //Jump
    2.  
    3. if (Input.GetKeyDown("w") && suelo && !saltando)
    4.             {
    5.                 recordarSalto = recordarSaltoTiempo;
    6.                 distanciaSalto = distanciaSaltoTiempo;
    7.  
    8.             }
    9.  
    10. if ((recordarSalto > 0) && suelo)
    11.             {
    12.                 recordarSalto = 0;
    13.                 salto = true;
    14.             }
    15.  
    16. if (Input.GetKey("w") && salto)
    17.             {
    18.                 if (distanciaSalto > 0 && !atacando)
    19.                 {
    20.                     myBody.AddForce(Vector2.up * fuerzaSalto, ForceMode2D.Impulse);
    21.                     distanciaSalto -= Time.deltaTime;
    22.                 }
    23.                 else
    24.                 {
    25.                     salto = false;
    26.                 }
    27.             }
    28.  
    29.             if (Input.GetKeyUp("w"))
    30.             {
    31.                 salto = false;
    32.             }
    33.  
    34. //Attack
    35.  
    36. if (Input.GetMouseButtonDown(0))
    37.             {
    38.                 salto = false;
    39.                 recordarSalto = -1;
    40.                 distanciaSalto = -1;
    41.  
    42.                 atacando = true;
    43.                 animator.SetTrigger("Atacar");
    44.  
    45.                 gameObject.GetComponent<CapsuleCollider2D>().size = new Vector2(2f, 1f);
    46.  
    47.                 GameObject objetoAtacado;
    48.                 posicionRaton = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    49.  
    50.                 Vector3 desdeRatonHastaJugador = posicionRaton - (Vector2)transform.position;
    51.  
    52.                 float desfase = 1f;
    53.                 Vector2 hitBox = new Vector2(1, 1);
    54.  
    55.                 vectorAtaque = desdeRatonHastaJugador.normalized * desfase;
    56.  
    57.                 puntoA = (Vector2)transform.position + vectorAtaque - hitBox * 0.5f;
    58.                 puntoB = puntoA + hitBox;
    59.  
    60.                 int elementosAtacados = Physics2D.OverlapArea(puntoA, puntoB, filtroDeAtaque, resultadoAtaques);
    61.                 //Debug.Log(elementosAtacados);
    62.  
    63.                 for (int i = 0; i < elementosAtacados; i++)
    64.                 {
    65.                     objetoAtacado = resultadoAtaques[i].gameObject;
    66.                     if (objetoAtacado.GetComponent<RecibirAtaque>() != null)
    67.                     {
    68.                         objetoAtacado.GetComponent<RecibirAtaque>().Golpe();
    69.                     }
    70.                 }
    71.  
    72.                 float rotation = Mathf.Atan2(vectorAtaque.y, vectorAtaque.x) * Mathf.Rad2Deg;
    73.            
    74.                 transform.rotation = Quaternion.Euler(0f, 0f, rotation);
    75.  
    76.                 if (rotation > 65 && rotation < 110)
    77.                 {
    78.                     myBody.AddForce(vectorAtaque * 9, ForceMode2D.Impulse);
    79.                 }
    80.                 else
    81.                 {
    82.                     if (rotation < -65 && rotation > -110)
    83.                     {
    84.                         myBody.AddForce(vectorAtaque * 7, ForceMode2D.Impulse);
    85.                     }
    86.                     else
    87.                     {
    88.                         myBody.AddForce(vectorAtaque * 10, ForceMode2D.Impulse);
    89.                     }
    90.                 }
    91.  
    92.  
    93.                 if (rotation > 90 || rotation < -90)
    94.                 {
    95.                     transformada.localScale = new Vector3(1f, -1f, 1f);
    96.                 }
    97.                 else
    98.                 {
    99.                     transformada.localScale = new Vector3(1f, 1f, 1f);
    100.                 }
    101.  
    102.             }
    103.             Debug.DrawLine(transform.position, (Vector2)transform.position + vectorAtaque, Color.yellow);
    104.             Debug.DrawLine(puntoA, puntoB, Color.red);

    I have one more function that ends my attack animation but my problem here are the forces.
    Thank you for your time.
     
    Last edited: Aug 8, 2019
  6. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    102
    Could you edit that last post and put it in code tags from the text bar? The spanish isnt hard to understand, I can generally see what you're doing but its hard without the proper code tag format. But, I may have a couple of suggestions or ideas: First, maybe you could add some debug.logs where you are checking for rotation and then adding force depending on the value. It could be that one gets called very quickly after another since you have 2 ifs and 2 else, the 2 ifs could conflict as could the 2 elses. Does that make sense? Not sure if i wrote that very well. Second, If you are certain that they don't get called close to another and aren't compounding onto each other, then try to multiply it by Time.deltaTime, sometimes that helps. If those 2 suggestions don't work, edit your post into code tags and add some comment lines to help with understanding.
     
  7. migbernabeumor

    migbernabeumor

    Joined:
    Apr 7, 2019
    Posts:
    16
    The player can call them at the same time but i dont know how to avoid it.
    Code edited.
    Thank you for your time.
     
  8. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    102
    Thank you! Much easier to read. So if the player can call all of the myBody.AddForce functions at once then it is likely where your force is being messed up. Try debugging the rotation float within each If and Else block to find what value it is. From there you will probably have a better idea of what to change your parameters for those If and Else blocks. Try to get it so only ONE of those blocks can run at once so it isn't constantly multiplying your forces.
     
  9. hlw

    hlw

    Joined:
    Aug 12, 2017
    Posts:
    250
    You might want to determine a max x and y velocity that seem okay for your character. Then,when you add forces,check if it is too much, and if it is,set the velocity directly to that amount in the same block of code.
     
  10. migbernabeumor

    migbernabeumor

    Joined:
    Apr 7, 2019
    Posts:
    16
    I fixed this problem making the velocity of my character 0 (Vector2.zero) before adding the next force.
    Thank you for your time.