Search Unity

Hitbox Layers and Bouncing Scripts Not Working

Discussion in 'Scripting' started by MisterSixtyFour, Jul 14, 2022.

  1. MisterSixtyFour

    MisterSixtyFour

    Joined:
    Jun 28, 2021
    Posts:
    60
    So I have this projectile script in my game, and it's meant to bounce around the screen until it runs out of either one of two variables:


    Code (CSharp):
    1.  void Update()
    2.     {
    3.         Vector2 pos = transform.position;
    4.  
    5.         pos.x += Time.deltaTime * xspeed * xdir;
    6.         pos.y += Time.deltaTime * yspeed * ydir;
    7.  
    8.         transform.position = pos;
    9.         if (pos.x <= xlimit * -1)
    10.         {
    11.             if (xhealth > 0 && player == false)
    12.             {
    13.                 xhealth -= 1;
    14.                 xdir = -xdir;
    15.                 pos.x = xlimit * xdir;
    16.             }
    17.             else
    18.             {
    19.                 if(pos.x < -11)
    20.                 {
    21.                     Destroy(gameObject);
    22.                 }
    23.             }
    24.  
    25.         }
    26.         transform.position = pos;
    27.         if (pos.x >= xlimit * 1)
    28.         {
    29.             if (xhealth > 0)
    30.             {
    31.                 xhealth -= 1;
    32.                 xdir = -xdir;
    33.                 pos.x = xlimit * xdir;
    34.             }
    35.             else
    36.             {
    37.                 if (pos.x > 11)
    38.                 {
    39.                     Destroy(gameObject);
    40.                 }
    41.             }
    42.  
    43.         }
    44.         if (pos.y >= ylimit * 1 || pos.y <= ylimit * -1)
    45.         {
    46.             if (yhealth > 0)
    47.             {
    48.                 yhealth -= 1;
    49.                 ydir = -ydir;
    50.                 pos.y = ylimit * ydir;
    51.             }
    52.  
    53.         }
    54.     }
    55.  
    56.     private void OnTriggerEnter(Collider collision)
    57.     {
    58.         Vector2 pos = transform.position;
    59.         ArkMovement bship = collision.GetComponent<ArkMovement>();
    60.         if (bship != null && pos.x < 5 && player == true)
    61.         {
    62.            
    63.             xdir = -xdir;
    64.             pos.x = xlimit * xdir;
    65.             if (bship.moveUp == true)
    66.             {
    67.                 ydir = 1;
    68.             }
    69.             if (bship.moveDown == true)
    70.             {
    71.                 ydir = -1;
    72.             }
    73.             if(bship.moveDown == false && bship.moveUp == false)
    74.             {
    75.                 ydir = 0;
    76.             }
    77.         }
    78.  
    79.     }
    80. }
    However two problems:
    1. Said projectile script is incredibly unpredictable. At times, the projectile will phase through the player even after previously being bounced or not at all, and sometimes it will get stuck on the xlimit or ylimit and then rapidly decrease the health variables.
    2. The player character's hitbox will need to be bigger to bounce said projectiles, but I've tried giving the player character a separate hitbox on a different layer to no success.

    How can I solve the errors I've had so far?