Search Unity

Physics is different between Editor and the Build

Discussion in 'Physics' started by ColdHand, Jul 10, 2020.

  1. ColdHand

    ColdHand

    Joined:
    Apr 10, 2018
    Posts:
    7
    Hi, so i have this problem. I am making a 2D Platform Fighter Game, and when it comes to Knockback, the Knockback on Editor, the Knockback works just fine, you can see the preview here.
    however when i played it on the build version, there's a difference; the enemy only knockbacks at Y axis, but the X axis still zero, so basically it's just going up. the preview here :
    . Do you guys have any idea what's causing the problem. I believe it's because the velocity of the rigidbody, but i still try to figure it out. here's the knockback script in case you want to check it out, thanks in advance! :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class TakingDamage : MonoBehaviour
    5. {
    6.     int startPercentage = 0;
    7.     public int percentage;
    8.     [Header("Component")]
    9.     [SerializeField]
    10.     Rigidbody2D rigid;
    11.     [SerializeField]
    12.     PlayerInputControl player;
    13.     Animator animator;
    14.  
    15.     //Knockback
    16.     float knockback;
    17.     //float knockbackLength;
    18.     public float knockbackCount = 0f;
    19.     bool knockbacked;
    20.     //bool fist;
    21.     Vector2 direction;
    22.  
    23.     // Start is called before the first frame update
    24.     void Start()
    25.     {
    26.         percentage = startPercentage;
    27.         animator = GetComponent<Animator>();
    28.    
    29.     }
    30.     private void FixedUpdate()
    31.     {
    32.         if (knockbacked)
    33.         {
    34.             animator.SetBool("isKnockbacked", true);
    35.          
    36.             if (knockbackCount > 0)
    37.             {
    38.              
    39.                 player.canNormalJump = false;
    40.                 player.canMove = false;
    41.                 knockbackCount -= Time.fixedDeltaTime;
    42.             }
    43.             if (knockbackCount <= 0)
    44.             {
    45.                 knockbacked = false;
    46.                 knockbackCount = 0f;
    47.                 player.canMove = true;
    48.                 rigid.velocity = new Vector2(0, rigid.velocity.y);
    49.             }
    50.         }
    51.         if (!knockbacked)
    52.         {
    53.             animator.SetBool("isKnockbacked", false);
    54.            // fist = false;
    55.         }
    56.      
    57.     }
    58.     private void OnTriggerEnter2D(Collider2D collision)
    59.     {
    60.      
    61.         if (collision.tag == "hitbox" && !player.blocked)
    62.         {
    63.          
    64.             direction = (collision.gameObject.transform.parent.parent.position - this.transform.position).normalized;
    65.             percentage += collision.GetComponent<PlayerAttackHitbox>().damage;
    66.             knockback = percentage * 0.4f;
    67.        
    68.             if (collision.transform.parent.parent.gameObject.GetComponent<PlayerInputControl>().knockbackSign)
    69.             {
    70.                 knockbackCount = knockback * 0.03f;
    71.                 knockbacked = true;
    72.                 if (direction.x < 0)
    73.                 {
    74.                     rigid.velocity = new Vector2(direction.x + knockback, knockback);
    75.                     //rigid.AddForce(new Vector2(direction.x + knockback, knockback));
    76.                 }
    77.                 else if (direction.x >= 0)
    78.                 {
    79.                     rigid.veloctiy = new Vector2(-(direction.x + knockback), knockback);
    80.                     //rigid.AddForce(new Vector2(-(direction.x + knockback), knockback));
    81.                 }
    82.                 // Debug.Log(rigid.velocity);
    83.             }
    84.             else if(collision.transform.parent.parent.gameObject.GetComponent<PlayerInputControl>().knockbackSign && collision.transform.parent.parent.gameObject.GetComponent<PlayerInputControl>().isLightAttacking)
    85.             {
    86.                 knockbackCount = knockback * 0.01f;
    87.                 knockbacked = true;
    88.                 if (direction.x < 0)
    89.                 {
    90.                     rigid.velocity = new Vector2((direction.x + knockback) * 0.5f, knockback / 3);
    91.                 }
    92.                 else if (direction.x >= 0)
    93.                 {
    94.                     rigid.velocity = new Vector2(-(direction.x + knockback) * 0.5f, knockback / 3);
    95.                 }
    96.                 //  Debug.Log(rigid.velocity);
    97.             }
    98.             //Debug.Log(direction.x + knockback + "," + knockback);
    99.             //StartCoroutine(Knockback(percentage * 0.1f, percentage * 0.01f, collision.gameObject.transform));
    100.             else if (!collision.transform.parent.parent.gameObject.GetComponent<PlayerInputControl>().knockbackSign)
    101.             {
    102.                 //Jika tidak cukup 'sakit' saja ; if not knockbacked, just hurt
    103.                animator.SetTrigger("Hurt");
    104.              
    105.             }
    106.         }
    107.         if (collision.tag == "Projectile Hitbox" && !player.blocked)
    108.         {
    109.             Vector2 direction = (collision.gameObject.transform.position - this.transform.position).normalized;
    110.             percentage += collision.GetComponent<PlayerAttackHitbox>().damage;
    111.             knockback = percentage * 0.4f;
    112.          
    113.             if (collision.gameObject.GetComponent<Projectile>().knockbackSign)
    114.             {
    115.                 knockbackCount = knockback * 0.05f;
    116.                 knockbacked = true;
    117.                 if (direction.x < 0)
    118.                 {
    119.                     rigid.velocity = new Vector2(direction.x + knockback, knockback);
    120.                     //rigid.AddForce(new Vector2(direction.x + knockback, knockback));
    121.                 }
    122.                 else if (direction.x >= 0)
    123.                 {
    124.                     rigid.velocity = new Vector2(-(direction.x + knockback), knockback);
    125.                     //rigid.AddForce(new Vector2(-(direction.x + knockback), knockback));
    126.                 }
    127.                 // Debug.Log(rigid.velocity);
    128.             }
    129.             else if (!collision.gameObject.GetComponent<Projectile>().knockbackSign)
    130.             {
    131.                 //Jika tidak cukup 'sakit' saja
    132.                 animator.SetTrigger("Hurt");
    133.             }
    134.             //  Debug.Log(rigid.velocity);
    135.         }
    136.         }
    137. }