- Home /
Shotgun raycast
Hello everyone I looked around for this but I could not understand since the answers were all Js and I only work with C# I have a shotgun and everything is fine it shoots.. my problem is I want a realistic spread/ multiple ray cast shooting can anyone help ? thank you.
void RayShoot () {
Player.animation.Play("ShotgunShoot");
RaycastHit hit;
Vector3 DirectionRay = transform.TransformDirection (Vector3.forward);
Debug.DrawRay (transform.position, DirectionRay*Range, Color.red);
ray = Camera.main.ScreenPointToRay (Input.mousePosition+Random.onUnitSphere);
if(Physics.Raycast(ray , out hit , Range)){
}
Answer by robertbu · Jul 31, 2013 at 03:30 PM
There are three answers in this question. My answer produces a spread that skews towards the center. The answer by @Owen Reynolds produces a random square spread. My follow-up comment on this answer produces a random circular spread.
http://answers.unity3d.com/questions/501221/raycast-direction-problem.html
And I though of a new way if you are using a transform.forward of some object to aim. It also skews towards the center, but it is less complex:
#pragma strict
var variance = 1.0; // This much variance
var distance = 10.0; // at this distance
function Update() {
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)) {
for (var i = 0; i < 30; i++) {
var v3Offset = transform.up * Random.Range(0.0, variance);
v3Offset = Quaternion.AngleAxis(Random.Range(0.0, 360.0), transform.forward) * v3Offset;
var v3Hit = transform.forward * distance + v3Offset;
// Position an object to test pattern
var tr = GameObject.CreatePrimitive(PrimitiveType.Sphere).transform;
tr.localScale = Vector3(0.1, 0.1, 0.1);
tr.position = v3Hit;
}
}
}
And here is the pattern:
I read what you wrote in the link you gave me but I failed to write it in C# and merge it with what I have. I'm suffering I know >< and I keep getting errors on the lines with random.range ..
thank you very much for your effort.. appreciate it :)
void RayShoot () {
Player.animation.Play("ShotgunShoot");
RaycastHit hit;
Vector3 DirectionRay = transform.TransformDirection (Vector3.forward);
Debug.DrawRay (transform.position, DirectionRay*Range, Color.red);
ray = Camera.main.ScreenPointToRay (Input.mousePosition+Random.onUnitSphere);
if(Physics.Raycast(ray , out hit , Range)){
if(hit.collider.tag == "world"){
HitParticles.transform.position= hit.point;
HitParticles.transform.localRotation = Quaternion.FromToRotation(Vector3.forward,hit.normal);
HitParticles.Emit();
GameObject Shot;
Shot = Instantiate (Shot$$anonymous$$ark , hit.point,transform.rotation) as GameObject;
Shot.transform.LookAt(hit.point + hit.normal);
}
if(hit.collider.tag == "metal"){
metalhit.transform.position= hit.point;
metalhit.transform.localRotation = Quaternion.FromToRotation(Vector3.forward,hit.normal);
metalhit.Emit();
GameObject Shot;
Shot = Instantiate (Shot$$anonymous$$ark , hit.point,transform.rotation) as GameObject;
Shot.transform.LookAt(hit.point + hit.normal);
//Shot.transform.localRotation = Quaternion.AngleAxis (Random.value * 360 , Vector3.forward);
}
if(hit.collider.tag == "zombie"){
blood.transform.position= hit.point;
blood.transform.localRotation = Quaternion.FromToRotation(Vector3.forward,hit.normal);
blood.Emit();
blood2.transform.position= hit.point;
blood2.transform.localRotation = Quaternion.FromToRotation(Vector3.forward,hit.normal);
blood2.Emit();
hit.collider.Send$$anonymous$$essageUpwards("damage", Damage,Send$$anonymous$$essageOptions.DontRequireReceiver);
}
if(hit.rigidbody){
if(HitParticles){
hit.rigidbody.AddForceAtPosition(DirectionRay*force,hit.point);
}
}
}
BulletsLeft--;
if(BulletsLeft <0 ){
BulletsLeft =0;
}
if(BulletsLeft==0 ){
Reload();
}
}