Search Unity

Why do two different addforce functions add two different forces???

Discussion in 'Physics' started by samdaman93, May 18, 2020.

  1. samdaman93

    samdaman93

    Joined:
    Oct 3, 2015
    Posts:
    37
    Hello,

    I've got a situation where my character has a jump function that adds 400 force. And I've got a force block that adds 400 force to the players rigidbody when he jumps on it. The "velocity block" adds about 1000 force it seems compared to what a normal jump adds. I've linked codes and snips below. I can't seem to see why this would be different???

    Feel free to ask for anything else!

    Cheers,

    https://imgur.com/a/f5EKJAS

    Player Code (Included everything but add force is in jump function)
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Player : MonoBehaviour
    7. {
    8.     [SerializeField]
    9.     private LayerMask groundLayerMask;
    10.     public float speed = 6.5f;
    11.     private Rigidbody2D rb;
    12.     private BoxCollider2D triggerCollider;
    13.     private BoxCollider2D playerCollider;
    14.     private SpriteRenderer spriteRenderer;
    15.     isGrounded isgrounded;
    16.  
    17.     public float jumpForce = 400f;
    18.     public float jumpCooldown = 0.1f;
    19.     private float jumpTimer;
    20.     public bool jumpOnCooldown = true;
    21.  
    22.     private float inputHorizontal;
    23.     private float inputVertical;
    24.     public Animator playerAnimator;
    25.     private float facingDirection = 1f;
    26.  
    27.     public float attackCooldown = 1f;
    28.     private float attackTimer;
    29.     private bool canAttack = true;
    30.  
    31.     public GameObject fireball;
    32.     public Transform fireballPoint;
    33.     public float fireballSpeed = 10f;
    34.     public float fireballDamage = 15f;
    35.  
    36.     public healthBarScript HPBar;
    37.     public float maxHp = 100f;
    38.     public float currentHP = 100f;
    39.  
    40.     void Start()
    41.     {
    42.         rb = GetComponent<Rigidbody2D>();
    43.         playerCollider = GetComponent<BoxCollider2D>();
    44.         spriteRenderer = GetComponent<SpriteRenderer>();
    45.         playerAnimator = GetComponent<Animator>();
    46.         triggerCollider = this.transform.Find("Trigger").GetComponent<BoxCollider2D>();
    47.         HPBar.setMaxHealth(maxHp);
    48.         HPBar.setHealth(currentHP);
    49.         groundLayerMask = LayerMask.GetMask("Ground");
    50.         isgrounded = GetComponentInChildren<isGrounded>();
    51.     }
    52.  
    53.     void Update()
    54.     {
    55.         inputHorizontal = Input.GetAxisRaw("Horizontal");
    56.         inputVertical = Input.GetAxisRaw("Vertical");
    57.  
    58.  
    59.         Debug.Log("Is player grounded?" + isgrounded.Grounded);
    60.  
    61.         if (isgrounded.Grounded)
    62.         {
    63.             playerAnimator.SetBool("isJumping", false);
    64.             playerAnimator.SetBool("isFalling", false);
    65.         }
    66.         else
    67.         {
    68.             if (rb.velocity.y <= 0)
    69.             {
    70.                 playerAnimator.SetBool("isFalling", true);
    71.                 playerAnimator.SetBool("isJumping", false);
    72.             }
    73.             else
    74.             {
    75.                 playerAnimator.SetBool("isJumping", true);
    76.                 playerAnimator.SetBool("isFalling", false);
    77.             }
    78.         }
    79.  
    80.         if (inputHorizontal < 0)
    81.         {
    82.             facingDirection = -1f;
    83.             spriteRenderer.flipX = true;
    84.             playerAnimator.SetBool("isWalking", true);
    85.  
    86.         }
    87.         else if (inputHorizontal > 0)
    88.         {
    89.             facingDirection = 1f;
    90.             spriteRenderer.flipX = false;
    91.             playerAnimator.SetBool("isWalking", true);
    92.         }
    93.         else if (inputHorizontal == 0)
    94.         {
    95.             playerAnimator.SetBool("isWalking", false);
    96.         }
    97.  
    98.         if (jumpTimer <= jumpCooldown)
    99.         {
    100.             jumpTimer += Time.fixedDeltaTime;
    101.         }
    102.         else
    103.         {
    104.             jumpOnCooldown = false;
    105.         }
    106.  
    107.         if (Input.GetKey(KeyCode.Space) && isgrounded.Grounded && !jumpOnCooldown)
    108.         {
    109.             jump();
    110.             jumpTimer = 0;
    111.             jumpOnCooldown = true;
    112.         }
    113.  
    114.         if (!canAttack && attackTimer <= attackCooldown)
    115.         {
    116.             attackTimer += Time.deltaTime;
    117.         }
    118.         else if (attackTimer >= attackCooldown)
    119.         {
    120.             canAttack = true;
    121.         }
    122.      
    123.  
    124.         if (Input.GetKey(KeyCode.Z) && canAttack)
    125.         {
    126.             attack();
    127.             canAttack = false;
    128.             attackTimer = 0f;
    129.         }
    130.  
    131.     }
    132.  
    133.     // Update is called once per frame
    134.     void FixedUpdate()
    135.     {
    136.         rb.velocity = new Vector2(inputHorizontal * Time.fixedDeltaTime * speed, rb.velocity.y);
    137.         //rb.transform.position += new Vector3(inputHorizontal, 0.0f, 0.0f)*Time.fixedDeltaTime * speed;
    138.     }
    139.  
    140.     //public bool onGround()
    141.     //{
    142.     //    float extraHeight = 0.05f;
    143.     //    RaycastHit2D rayCastHit = Physics2D.Raycast(playerCollider.bounds.center, Vector2.down, playerCollider.bounds.extents.y + extraHeight, groundLayerMask);
    144.     //    Color rayColor;
    145.     //    if (rayCastHit.collider != null)
    146.     //    {
    147.     //        rayColor = Color.green;
    148.            
    149.     //        if(rayCastHit.collider.tag == "Moving Platform")
    150.     //        {
    151.     //            transform.SetParent(rayCastHit.transform);
    152.     //        }
    153.  
    154.     //        if (rayCastHit.collider.tag == "Velocity Block")
    155.     //        {
    156.     //            rayCastHit.transform.GetComponent<velocityBlock>().triggered = true;
    157.     //            return false;
    158.     //        }
    159.  
    160.     //    }
    161.     //    else
    162.     //    {
    163.     //        rayColor = Color.red;
    164.     //        transform.SetParent(null);
    165.     //    }
    166.  
    167.     //    return rayCastHit.collider != null;
    168.     //}
    169.  
    170.     void jump()
    171.     {
    172.         rb.velocity = new Vector2(rb.velocity.x, 0.0f);
    173.         rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Force);
    174.     }
    175.  
    176.     public void takeDamage(float Damage)
    177.     {
    178.         currentHP -= Damage;
    179.         HPBar.setHealth(currentHP);
    180.         if (currentHP < 0)
    181.         {
    182.             death();
    183.         }
    184.     }
    185.  
    186.     private void death()
    187.     {
    188.         //do something
    189.     }
    190.  
    191.     void OnTriggerStay2D(Collider2D other)
    192.     {
    193.         if (other.tag == "Enemy")
    194.         {
    195.             Enemy enemy = other.GetComponent<Enemy>();
    196.             takeDamage(enemy.damage);
    197.         }
    198.     }
    199.  
    200.     void attack()
    201.     {
    202.         GameObject Fireball = Instantiate(fireball);
    203.         Fireball script = fireball.GetComponent<Fireball>();
    204.         script.damage = fireballDamage;
    205.         Fireball.transform.position = fireballPoint.transform.position;
    206.         Rigidbody2D Fireballrb = Fireball.GetComponent<Rigidbody2D>();
    207.         Fireballrb.velocity = new Vector2(facingDirection * fireballSpeed, Fireballrb.velocity.y);
    208.  
    209.     }
    210. }
    211.  
    212.  
    213.  

    Force Block Code
    Code (CSharp):
    1. void OnTriggerEnter2D(Collider2D Other)
    2.     {
    3.         if (triggered)
    4.         {
    5.             Other.attachedRigidbody.velocity = new Vector2(Other.attachedRigidbody.velocity.x, 0f);
    6.             Other.attachedRigidbody.AddForce(Vector2.up * bounceForce, ForceMode2D.Force);
    7.         }