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): for(int i = 0; i < 11; i++) { Vector3 tempPos = new Vector3(Random.Range(player.transform.position.x - bulletSpray.x, player.transform.position.x + bulletSpray.x), Random.Range(player.transform.position.y - bulletSpray.y, player.transform.position.y + bulletSpray.y), player.transform.position.z); shotGunSpray.Add(tempPos); Debug.Log("Ray " + i + " " + shotGunSpray[i]); RaycastHit hit; Ray ray = new Ray(shotGunSpray[i], player.transform.forward); if(Physics.Raycast(ray, out hit, 50f) && hit.collider.tag != "Bullet" && hit.collider.tag != "Default") { Instantiate (bulletHole[Random.Range(0,5)], hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal)); } if(i >= 10) { GetComponentInChildren<GunController>().shotFired = false; shotFired = false; shotGunSpray.Clear(); // shotgunPellets.Clear(); } } }
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): Vector3 tempPos = new Vector3(Random.Range(-bulletSpray.x, bulletSpray.x), Random.Range(-bulletSpray.y, bulletSpray.y), 0); 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.
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.
I've always wondered if you could use http://docs.unity3d.com/ScriptReference/Random-insideUnitCircle.html for shotgun type scatters... i.e you could do a "1m spread at 100m" by having the circle 0.5 radius and adjust the points by forward *100... might have to play with the axis a bit, I think the circle is assuming top down?
You might have an easier time using this one: http://docs.unity3d.com/ScriptReference/Random-insideUnitSphere.html Using the unit circle you'd have to tweak its X and Y into the proper axes. The unit sphere you could just throw in anywhere.
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.
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.
You guys friggin rock!!! Below is the final code for my shotgun and it works perfectly!!! Thanx!!! Code (csharp): for(int i = 0; i < 11; i++) { Vector3 tempPos = (Random.insideUnitSphere * 0.2f) + crossHair.transform.position; shotGunSpray.Add(tempPos); Debug.Log("Ray " + i + " " + shotGunSpray[i]); RaycastHit hit; Ray ray = new Ray(shotGunSpray[i], crossHair.transform.forward); Debug.DrawRay( shotGunSpray[i], crossHair.transform.forward, Color.red ); if(Physics.Raycast(ray, out hit, 50f) && hit.collider.tag != "Bullet" && hit.collider.tag != "Default") { Instantiate (bulletHole[Random.Range(0,5)], hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal)); } if(i >= 10) { GetComponentInChildren<GunController>().shotFired = false; shotFired = false; shotGunSpray.Clear(); // shotgunPellets.Clear(); }