Search Unity

Can't instantiate hitpointFX when standing above y=0 shooting downwards

Discussion in 'Scripting' started by Jeepster, Oct 19, 2020.

  1. Jeepster

    Jeepster

    Joined:
    Jan 23, 2014
    Posts:
    401
    Hi,

    This is a problem that I've tried to solve for ages now but unless my instantiation code is wrong I have no clue why this is happening so I'm hoping someone here does and has experienced the same problem.
    Explanation:
    I use Raycast to shoot and show a line render effect when I shoot. I also instantiate either dust, water splash or blood depending on what object I hit and this works but there are strange limitations:
    - If I stand at or just above y=0 and shoot downwards, unless it's right underneath my player the hitpoint fx do not instantiate even though the line render and the raycast works and registers what it hit.
    If I stand at y=3 or above and move the collider up above y=1.2f then it all works but anything beneath y=1.2f doesn't work. So my hit effects are floating above the water surface by at least 1.2f on the y-axis and it looks horrible at best.

    The raycast and line renderer works at any height and any angle it's just the hitpoint fx so here's my hitpoint fx code:

    Code (CSharp):
    1.    
    2. void Fire()
    3.     {
    4.         if (photonView.isMine)
    5.         {
    6.             if (isenabled2)
    7.         {
    8.             if (weaponData == null)
    9.         {
    10.             weaponData = gameObject.GetComponentInChildren<WeaponData>();
    11.             if (weaponData == null)
    12.             {
    13.                 Debug.LogError("Did not find a WeaponData script on child object");
    14.             }
    15.             return;
    16.         }
    17.         if (cooldown > 0)
    18.         {
    19.             return;
    20.         }
    21.         Ray ray = new Ray(RootToShoot.transform.position, RootToShoot.transform.forward);
    22.         Transform hitTransform;
    23.         Vector3 hitPoint;
    24.  
    25.         hitTransform = FindClosestHitObject(ray, out hitPoint);
    26.         if (hitTransform != null)
    27.         {
    28.             Debug.Log("We hit: " + hitTransform.name);
    29.  
    30.  
    31.  
    32.                
    33.                     if (hitTransform.gameObject.tag == "HitWaterGun")
    34.  
    35.                     {
    36.                      
    37.  
    38.  
    39.                         GameObject GO9 = PhotonNetwork.Instantiate(impactEffectwater.name, hitPoint, Quaternion.identity, 0);
    40.  
    41.            
    42.  
    43.                     }
    44.  
    45.                     if (hitTransform.gameObject.tag == "rock")
    46.  
    47.                     {
    48.  
    49.            
    50.                            GameObject GO = PhotonNetwork.Instantiate(impactEffectrock.name, hitPoint, Quaternion.identity, 0);
    51.                      }
    52.  
    53.             }
    I should also mention that if my player is under y=-1f all hitpoint effects work in all directions. I've tried to turn off each script one y one to try and see if it's something in my code by process of elimination, but the problem persists and I have no leads anymore. Has anyone had the same issue and is it my hitpoint instantiation code perhaps?