Search Unity

What the??

Discussion in 'Scripting' started by GForce007, Oct 7, 2015.

  1. GForce007

    GForce007

    Joined:
    May 22, 2015
    Posts:
    27
    Hi guys!!

    Ok so I think I've tried all the wrong options of how to get this done.
    I'm creating a FPS game, and I'm struggling with my shotgun.

    I can create a nice bullet spray effect, but only from one direction.
    The moment I turn 90 degrees, the shot just shoots in one vertical line??
    You can see in the screenshots.

    Its like the axis are not rotating, and the x axis stays in the same position regardless of the rotation??

    Below is the code for my raycast, any ideas?

    Thanx!!!


    Code (csharp):
    1.  
    2.     for(int i = 0; i < 11; i++)
    3.             {
    4.                Vector3 tempPos = new Vector3(Random.Range(player.transform.position.x - bulletSpray.x,
    5.                                                           player.transform.position.x + bulletSpray.x),
    6.                                              Random.Range(player.transform.position.y - bulletSpray.y,
    7.                                                           player.transform.position.y + bulletSpray.y),
    8.                                                           player.transform.position.z);        
    9.              
    10.                 shotGunSpray.Add(tempPos);
    11.                 Debug.Log("Ray " + i + " " + shotGunSpray[i]);
    12.  
    13.                 RaycastHit hit;
    14.                Ray ray = new Ray(shotGunSpray[i], player.transform.forward);
    15.                          
    16.                 if(Physics.Raycast(ray, out hit, 50f) && hit.collider.tag != "Bullet" && hit.collider.tag != "Default")
    17.                 {
    18.                     Instantiate (bulletHole[Random.Range(0,5)], hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
    19.                 }
    20.  
    21.                 if(i >= 10)
    22.                 {
    23.                     GetComponentInChildren<GunController>().shotFired = false;
    24.                     shotFired = false;
    25.  
    26.                     shotGunSpray.Clear();
    27. //                    shotgunPellets.Clear();
    28.                 }
    29.             }
    30.         }
    31.  
     

    Attached Files:

  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    You're creating the bullet positions by offsetting randomly on the x-axis. This means that if you're facing in the direction of the x-axis, all of your bullets will be on a line.

    The issue is that you're working in world space. Note that your player's rotation isn't included anywhere in the code where you create the bullet's origin point.

    The easiest solution is to work in the player's space, where (0, 0, 0) is the player's position, and (1, 0, 0) is 1 metre to the player's right. Then you can transform that to world coordinates with transform.TransformPoint:

    Code (csharp):
    1. Vector3 tempPos = new Vector3(Random.Range(-bulletSpray.x, bulletSpray.x),
    2.                                                      Random.Range(-bulletSpray.y, bulletSpray.y),
    3.                                                      0);
    4.  
    5. shotGunSpray.add(player.transform.TransformPoint(tempPos));
    That should fix your issue.

    Note that there's a bunch of other weird things going on - you're using a list to store the spread points, but you're only ever accessing the last point in that list, and clearing it as you exit the for-loop, so there seems to be no reason for the list to exist.

    Your spray is also not really a tranditional (game) shotgun spray - you spread the starting position of your pellets, and then fire them straight foreward. This means that you'll be just as accurate at 5 metres as at 50.
     
  3. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Hi,

    In future please use topic titles that describe the nature of your problem. A lot of talented developers will flat out ignore a title like that for fear it will just be yet another noob post about halo or whatever.
     
    larku likes this.
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
  6. GForce007

    GForce007

    Joined:
    May 22, 2015
    Posts:
    27
    Thanx guys,

    Will check it out, hopefully I can get it to work now.
    Will keep you posted.

    Will also use a more descriptive title in the future, thanx for the hint.

    On a side note, does anyone know if you Unity has like a free "Unity for dummies", their descriptions aren't always very informative and sometimes a bit confusing.
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    The learn section is pretty good as a "for dummies".

    I would do a shot gun by projecting a unit sphere in front of the player. Probably using Random.insideUnitSphere + transform.forward. Then I would create rays from the muzzle through the points in the sphere. This would simulate the spread of a shotgun with distance from the muzzle.

    You could manipulate the degree of spread by changing the distance of the sphere from the muzzle.
     
  8. GForce007

    GForce007

    Joined:
    May 22, 2015
    Posts:
    27
    You guys friggin rock!!!
    Below is the final code for my shotgun and it works perfectly!!!

    Thanx!!!

    Code (csharp):
    1.  
    2.             for(int i = 0; i < 11; i++)
    3.             {
    4.                 Vector3 tempPos = (Random.insideUnitSphere * 0.2f) + crossHair.transform.position;
    5.  
    6.                 shotGunSpray.Add(tempPos);
    7.                 Debug.Log("Ray " + i + " " + shotGunSpray[i]);
    8.  
    9.                 RaycastHit hit;
    10.                 Ray ray = new Ray(shotGunSpray[i], crossHair.transform.forward);
    11.                 Debug.DrawRay( shotGunSpray[i], crossHair.transform.forward, Color.red );
    12.                          
    13.                 if(Physics.Raycast(ray, out hit, 50f) && hit.collider.tag != "Bullet" && hit.collider.tag != "Default")
    14.                 {
    15.                     Instantiate (bulletHole[Random.Range(0,5)], hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
    16.                 }
    17.  
    18.                 if(i >= 10)
    19.                 {
    20.                     GetComponentInChildren<GunController>().shotFired = false;
    21.                     shotFired = false;
    22.  
    23.                     shotGunSpray.Clear();
    24. //                    shotgunPellets.Clear();
    25.                 }
    26.