Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Raycast Impact Effect Shotgun Help

Discussion in 'Scripting' started by MikawasakiDigital, Jun 6, 2019.

  1. MikawasakiDigital

    MikawasakiDigital

    Joined:
    Mar 29, 2019
    Posts:
    95
    Hey guys, I'm working on making a shotgun for my FPS.

    I've pretty much built it on top of my script I was using to fire an auto rifle.

    I got the shotgun to work fine with spread and all, but I can't get the shots to register unless I hit the target with my center shot. Like the spread only appears once i hit them with what feels like a main raycast.

    It I shoot at the floor for example only one shot registers.



    I'll drop my script below, if anyone could help I'd really appreciate it. Thanks again for any insight.
    Code (CSharp):
    1.  void secShoot()
    2.     {
    3.  
    4.         for (int i = 0; i < shotFragments; i++)
    5.         {
    6.             currentMag--;
    7.  
    8.             SetMagBarUi();
    9.  
    10.             Vector3 shootDirection = MainCamera.transform.forward;
    11.  
    12.             Vector3 weaponObjectLocalPosition = WeaponObject.transform.localPosition;
    13.  
    14.             weaponObjectLocalPosition.z = weaponObjectLocalPosition.z - secRecoilAmount;
    15.  
    16.             WeaponObject.transform.localPosition = weaponObjectLocalPosition;
    17.  
    18.             shootDirection.x += Random.Range(-spreadFactor, spreadFactor);
    19.  
    20.             shootDirection.y += Random.Range(-spreadFactor, spreadFactor);
    21.  
    22.             muzzleFlash.Play();
    23.  
    24.             nextFire = Time.time + secFireRate;
    25.  
    26.             Quaternion fireRotation = Quaternion.LookRotation(transform.forward);
    27.  
    28.             Quaternion randomRotation = Random.rotation;
    29.  
    30.             fireRotation = Quaternion.RotateTowards(fireRotation,randomRotation,Random.Range(0.0f,spreadAngle));
    31.  
    32.             if (Physics.Raycast(MainCamera.transform.position, fireRotation*Vector3.forward, out hit, Mathf.Infinity))
    33.             {
    34.                 Instantiate(secImpactEffect, hit.point, Quaternion.LookRotation(hit.normal));
    35.  
    36.                 GameObject go = Instantiate<GameObject>(secImpactEffect, hit.point, Quaternion.LookRotation(hit.normal));
    37.  
    38.                 IDamageable damageable = hit.collider.GetComponent<IDamageable>();
    39.  
    40.                 hit.rigidbody.AddForceAtPosition(transform.forward * secHitForce, hit.point);
    41.  
    42.                 float normalisedDistance = hit.distance / secMaximumRange;
    43.  
    44.                 if (damageable != null)
    45.                 {
    46.                     if (normalisedDistance <= 1)
    47.                     {
    48.                         damageable.DealDamage(Mathf.RoundToInt(Mathf.Lerp(secMaximumDamage, secMinimumDamage, normalisedDistance)));
    49.                     }
    50.                 }
    51.             }
    52.         }
    53.     }
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,745
    Use shotDirection for raycast direction instead of Vector3.forward.
     
    MikawasakiDigital likes this.
  3. MikawasakiDigital

    MikawasakiDigital

    Joined:
    Mar 29, 2019
    Posts:
    95
    Thanks, I'll take a look and make that change as soon as I get home from work.

    Also, just to clarify this change is done on line 40, correct?

     
    Last edited: Jun 6, 2019
  4. MikawasakiDigital

    MikawasakiDigital

    Joined:
    Mar 29, 2019
    Posts:
    95
    Hey, I meant line 32. And that did't work. I tried shootDirection and still nothing. By any chance does anything else look wrong about it?

     
  5. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    As I think I've told you before, slap some Debug.Logs all across that bad boy to see what's going on, put one after every if statement, loop iteration, function call.
     
    MikawasakiDigital likes this.
  6. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,745
    This

    Code (CSharp):
    1. Debug.DrawLine(MainCamera.transform.position, MainCamera.transform.position + fireRotation, Color.red, 3);
    will draw red lines for every raycast for 3 seconds in the Scene view so you may clearly see what's going on.
     
    MikawasakiDigital likes this.
  7. MikawasakiDigital

    MikawasakiDigital

    Joined:
    Mar 29, 2019
    Posts:
    95
    Thanks palex, I'll get right on it tonight.

     
  8. MikawasakiDigital

    MikawasakiDigital

    Joined:
    Mar 29, 2019
    Posts:
    95
    Lmao I always forget to use that tool for things like this. Thanks again man.
     
  9. MikawasakiDigital

    MikawasakiDigital

    Joined:
    Mar 29, 2019
    Posts:
    95
    Hey bro, good afternoon.

    I'm getting issues with the "+" being used there.

    I'll leave an image below of the error.

    What could be used to replace that "+" symbol?
     

    Attached Files:

  10. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    You cant add a quaternion to a vector3, you can multiply them, but quaternion comes first,
    fireRotation * camera.position
     
    palex-nx likes this.