public bool RayShoot(bool first) { bool res = false; RaycastHit[] hits; List<casthit> castHits = new List<casthit> (); // calculate ray length by running magnitude. float raySize = runningmMagnitude; Vector3 direction = GetComponent<Rigidbody>().velocity.normalized; if (first) { raySize = FirstRaylength; direction = initialDirection; } if (raySize <= 2.0f) { raySize = 2.0f; } Vector3 pos1 = this.transform.position - (direction * raySize); //Vector3 pos2 = pos1 + (direction * raySize); if (first) { pos1 = initialPosition; //pos2 = pos1 + (direction * raySize); } // if you add line renderer. you can see how the ray is casting if (lineRenderer) { LineRenderer line = (LineRenderer)GameObject.Instantiate (lineRenderer, pos1, Quaternion.identity); line.SetPosition (0, pos1); line.SetPosition (1, pos1 + (raySize * direction)); line.name = "laser " + raySize + " direction " + direction; GameObject.Destroy (line, 10); } // shoot ray to casting all objects int castcount = 0; RaycastHit[] casterhits = Physics.RaycastAll (pos1, direction, raySize, ignoreWalkThru); for (int i=0; i<casterhits.Length; i++) { if (casterhits [i].collider && Vector3.Dot ((casterhits [i].point - initialPosition).normalized, initialDirection) > 0.5f) { // checking a target tags. make sure it hit only taged object. if (tagCheck (casterhits [i].collider.tag) && casterhits [i].collider.tag != this.gameObject.tag) { // checking for make sure it not hit the same object 2 time. if (hitedCheck (casterhits [i].collider)) { castcount++; casthit casted = new casthit (); casted.distance = Vector3.Distance (initialPosition, casterhits [i].point); casted.index = i; casted.name = casterhits [i].collider.name; castHits.Add (casted); } } } } // and sorted first to the last by distance hits = new RaycastHit[castcount]; castHits.Sort ((x,y) => x.distance.CompareTo (y.distance)); for (int i=0; i<castHits.Count; i++) { hits [i] = casterhits [castHits [i].index]; } for (var i = 0; i < hits.Length && hitcount < HitCountMax; i++) { RaycastHit hit = hits [i]; if (first) { firsthited = true; } else { hited = true; } GameObject hitparticle = null; GameObject flowparticle = null; targetLocked = null; float hitparticlelifetime = 3; float flowparticlelifetime = 3; if (hit.collider.GetComponent<Rigidbody>()) { hit.collider.GetComponent<Rigidbody>().AddForce (direction * HitForce, ForceMode.Force); } // get bullet hiter component. AS_BulletHiter bulletHit = hit.collider.gameObject.GetComponent<AS_BulletHiter> (); if (bulletHit != null) { // get particle from hiter object if (bulletHit.ParticleHit) { hitparticle = (GameObject)Instantiate (bulletHit.ParticleHit, hit.point, hit.transform.rotation); } if (bulletHit.ParticleFlow) { flowparticle = (GameObject)Instantiate (bulletHit.ParticleFlow, this.transform.position, hit.transform.rotation); flowparticle.transform.SetParent(this.transform); } // using action preset if (actionPreset && !firsthited) { actionPreset.TargetHited (this, bulletHit, hit.point); } // get particle life time from hiter hitparticlelifetime = bulletHit.ParticleLifeTime; flowparticlelifetime = bulletHit.ParticleParticleFlowLifeTime; bulletHit.OnHit (hit, this); } else { if (ParticleHit) { hitparticle = (GameObject)Instantiate (ParticleHit, hit.point, hit.transform.rotation); } } // call do damage function on an object that's got hit. hit.collider.SendMessageUpwards (DamageMethodName, (float)Damage, SendMessageOptions.DontRequireReceiver); this.SendMessageUpwards (DoHitMethodName, SendMessageOptions.DontRequireReceiver); // just setup particle after hit to properly if (hitparticle != null) { hitparticle.transform.forward = hit.normal; if (ParticleSticky) hitparticle.transform.parent = hit.collider.transform; GameObject.Destroy (hitparticle, hitparticlelifetime); } if (flowparticle != null) { GameObject.Destroy (flowparticle, flowparticlelifetime); } // check a hit number if bullet is still hit under max count. res = true; hitcount++; if (DestroyWhenHit || hitcount >= HitCountMax || tagDestroyerCheck (hit.collider.tag)) { destroyed = true; } } if (destroyed) { if (actionPreset) { // using action preset actionPreset.OnBulletDestroy (); } // completely removed GameObject.Destroy (this.gameObject, DestroyDuration); } return res; }
public bool ShootRayTest(Vector3 origin, Vector3[] direction, int damage, float size, int hitmax, string id, string team) { bool res = false; for (int b=0; b<direction.Length; b++) { int hitcount = 0; int castcount = 0; RaycastHit[] hits; List<casthit> castHits = new List<casthit> (); float raySize = size; RaycastHit[] casterhits = Physics.RaycastAll (origin, direction [b], raySize); for (int i=0; i<casterhits.Length; i++) { if (casterhits [i].collider) { if (tagCheck (casterhits [i].collider.tag) && casterhits [i].collider.gameObject != this.gameObject && ((casterhits [i].collider.transform.root && casterhits [i].collider.transform.root.gameObject != this.gameObject) || casterhits [i].collider.transform.root == null)) { castcount++; casthit casted = new casthit (); casted.distance = Vector3.Distance (origin, casterhits [i].point); casted.index = i; casted.name = casterhits [i].collider.name; castHits.Add (casted); } } } hits = new RaycastHit[castcount]; castHits.Sort ((x,y) => x.distance.CompareTo (y.distance)); for (int i=0; i<castHits.Count; i++) { hits [i] = casterhits [castHits [i].index]; } for (var i = 0; i < hits.Length && hitcount < hitmax; i++) { RaycastHit hit = hits [i]; res = true; hitcount++; if (hitcount >= hitmax || tagDestroyerCheck (hit.collider.tag)) { break; } } } return res; }
public bool ShootRay(Vector3 origin, Vector3[] direction, int damage, float size, int hitmax, string id, string team) { bool res = false; for (int b=0; b<direction.Length; b++) { int damages = damage; int hitcount = 0; int castcount = 0; RaycastHit[] hits; List<casthit> castHits = new List<casthit> (); float raySize = size; RaycastHit[] casterhits = Physics.RaycastAll (origin, direction [b], raySize); for (int i=0; i<casterhits.Length; i++) { if (casterhits [i].collider) { if (tagCheck (casterhits [i].collider.tag) && casterhits [i].collider.gameObject != this.gameObject && ((casterhits [i].collider.transform.root && casterhits [i].collider.transform.root != this.gameObject.transform.root && casterhits [i].collider.transform.root.gameObject != this.gameObject) || casterhits [i].collider.transform.root == null)) { castcount++; casthit casted = new casthit (); casted.distance = Vector3.Distance (origin, casterhits [i].point); casted.index = i; casted.name = casterhits [i].collider.name; castHits.Add (casted); } } } hits = new RaycastHit[castcount]; castHits.Sort ((x,y) => x.distance.CompareTo (y.distance)); for (int i=0; i<castHits.Count; i++) { hits [i] = casterhits [castHits [i].index]; } for (var i = 0; i < hits.Length && hitcount < hitmax; i++) { RaycastHit hit = hits [i]; DamagePackage dm; dm.Damage = damage; dm.Normal = hit.normal; dm.Direction = direction [b]; dm.Position = hit.point; dm.ID = id; dm.Team = team; hit.collider.SendMessage ("OnHit", dm, SendMessageOptions.DontRequireReceiver); res = true; hitcount++; if (hitcount >= hitmax || tagDestroyerCheck (hit.collider.tag)) { break; } int damageReduce = (int)((float)damages * 0.75f); damages = damageReduce; } } return res; }
public bool RayShoot(bool first) { bool res = false; RaycastHit[] hits; List <casthit> castHits = new List <casthit> (); // calculate ray length by running magnitude. float raySize = runningmMagnitude; Vector3 direction = rigidBodyComp.velocity.normalized; if (first) { raySize = FirstRaylength; direction = initialDirection; } if (raySize <= 2.0f) { raySize = 2.0f; } Vector3 pos1 = this.transform.position - (direction * raySize); //Vector3 pos2 = pos1 + (direction * raySize); if (first) { pos1 = initialPosition; //pos2 = pos1 + (direction * raySize); } // if you add line renderer. you can see how the ray is casting if (lineRenderer) { LineRenderer line = (LineRenderer)GameObject.Instantiate(lineRenderer, pos1, Quaternion.identity); line.SetPosition(0, pos1); line.SetPosition(1, pos1 + (raySize * direction)); line.name = "laser " + raySize + " direction " + direction; GameObject.Destroy(line, 10); } // shoot ray to casting all objects int castcount = 0; RaycastHit[] casterhits = Physics.RaycastAll(pos1, direction, raySize, ignoreWalkThru); for (int i = 0; i < casterhits.Length; i++) { if (casterhits [i].collider && Vector3.Dot((casterhits [i].point - initialPosition).normalized, initialDirection) > 0.5f) { // checking a target tags. make sure it hit only taged object. if (tagCheck(casterhits [i].collider.tag) && casterhits [i].collider.gameObject != this.gameObject) { // checking for make sure it not hit the same object 2 time. if (hitedCheck(casterhits [i].collider)) { castcount++; casthit casted = new casthit(); casted.distance = Vector3.Distance(initialPosition, casterhits [i].point); casted.index = i; casted.name = casterhits [i].collider.name; castHits.Add(casted); } } } } // and sorted first to the last by distance hits = new RaycastHit[castcount]; castHits.Sort((x, y) => x.distance.CompareTo(y.distance)); for (int i = 0; i < castHits.Count; i++) { hits [i] = casterhits [castHits [i].index]; } for (var i = 0; i < hits.Length && hitcount < HitCountMax; i++) { RaycastHit hit = hits [i]; if (first) { firsthited = true; } else { hited = true; } targetLocked = null; Rigidbody hitrig = hit.collider.GetComponent <Rigidbody> (); if (hitrig) { hitrig.AddForce(direction * HitForce * Time.deltaTime, ForceMode.Force); } // get bullet hiter component. AS_BulletHiter bulletHit = hit.collider.gameObject.GetComponent <AS_BulletHiter> (); if (bulletHit != null) { // using action preset if (actionPreset && !firsthited && bulletHit.HasAction) { actionPreset.BaseDistance = bulletHit.BaseActionDistance; actionPreset.TargetHited(this, bulletHit, hit.point); } // get particle life time from hiter bulletHit.OnHit(hit, this); } // call do damage function on an object that's got hit. hit.collider.SendMessageUpwards(DamageMethodName, (float)Damage, SendMessageOptions.DontRequireReceiver); this.SendMessageUpwards(DoHitMethodName, SendMessageOptions.DontRequireReceiver); // check a hit number if bullet is still hit under max count. res = true; hitcount++; if (DestroyWhenHit || hitcount >= HitCountMax || tagDestroyerCheck(hit.collider.tag)) { destroyed = true; } } if (destroyed) { if (actionPreset) { // using action preset actionPreset.OnBulletDestroy(); } // completely removed GameObject.Destroy(this.gameObject, DestroyDuration); } return(res); }
public bool RayShoot(bool first) { bool res = false; RaycastHit[] hits; List <casthit> castHits = new List <casthit>(); float raySize = runningmMagnitude; Vector3 direction = GetComponent <Rigidbody>().velocity.normalized; if (first) { raySize = FirstRaylength; direction = initialDirection; } if (raySize <= 2.0f) { raySize = 2.0f; } Vector3 pos1 = this.transform.position - (direction * raySize); //Vector3 pos2 = pos1 + (direction * raySize); if (first) { pos1 = initialPosition; //pos2 = pos1 + (direction * raySize); } if (lineRenderer) { LineRenderer line = (LineRenderer)GameObject.Instantiate(lineRenderer, pos1, Quaternion.identity); line.SetPosition(0, pos1); line.SetPosition(1, pos1 + (raySize * direction)); line.name = "laser " + raySize + " direction " + direction; GameObject.Destroy(line, 10); //Debug.Log ("Shoot with size " + raySize); } // shoot ray to cast all objects int castcount = 0; RaycastHit[] casterhits = Physics.RaycastAll(pos1, direction, raySize, ignoreWalkThru); for (int i = 0; i < casterhits.Length; i++) { if (casterhits[i].collider && Vector3.Dot((casterhits[i].point - initialPosition).normalized, initialDirection) > 0.5f) { if (tagCheck(casterhits[i].collider.tag) && casterhits[i].collider.tag != this.gameObject.tag) { if (hitedCheck(casterhits[i].collider)) { castcount++; casthit casted = new casthit(); casted.distance = Vector3.Distance(initialPosition, casterhits[i].point); casted.index = i; casted.name = casterhits[i].collider.name; castHits.Add(casted); //Debug.Log("cast "+casterhits[i].collider.name +" ("+Vector3.Distance(initialPosition,casterhits[i].point)+")"); } } } //Debug.Log ("all cast " + casterhits [i].collider.name); } // sorted first to the last hits = new RaycastHit[castcount]; castHits.Sort((x, y) => x.distance.CompareTo(y.distance)); for (int i = 0; i < castHits.Count; i++) { hits[i] = casterhits[castHits[i].index]; //Debug.Log("soted cast "+castHits[i].index+" to "+i+" "+castHits[i].name+" ("+castHits[i].distance+")"); } for (var i = 0; i < hits.Length && hitcount < HitCountMax; i++) { RaycastHit hit = hits[i]; if (first) { firsthited = true; } else { hited = true; } GameObject hitparticle = null; if (hit.collider.GetComponent <Rigidbody>()) { hit.collider.GetComponent <Rigidbody>().AddForce(direction * HitForce, ForceMode.Force); } if (hit.collider.gameObject.GetComponent <AS_BulletHiter>()) { AS_BulletHiter bulletHit = hit.collider.gameObject.GetComponent <AS_BulletHiter>(); if (bulletHit != null) { if (bulletHit.ParticleHit) { hitparticle = (GameObject)Instantiate(bulletHit.ParticleHit, hit.point, hit.transform.rotation); } if (actionPreset && !firsthited) { actionPreset.TargetHited(this, bulletHit, hit.point); } this.transform.position = hit.point; bulletHit.OnHit(hit, this); hit.collider.SendMessageUpwards(DamageMethodName, (float)Damage, SendMessageOptions.DontRequireReceiver); this.SendMessageUpwards(DoHitMethodName, SendMessageOptions.DontRequireReceiver); } } else { if (ParticleHit) { hitparticle = (GameObject)Instantiate(ParticleHit, hit.point, hit.transform.rotation); } } if (hitparticle != null) { hitparticle.transform.forward = hit.normal; if (ParticleSticky) { hitparticle.transform.parent = hit.collider.transform; } GameObject.Destroy(hitparticle, 7); } res = true; hitcount++; if (DestroyWhenHit || hitcount >= HitCountMax || tagDestroyerCheck(hit.collider.tag)) { destroyed = true; } } if (destroyed) { if (actionPreset) { actionPreset.OnBulletDestroy(); } GameObject.Destroy(this.gameObject, 3); } return(res); }
void Start() { Collider[] colliders = Physics.OverlapSphere (this.transform.position, Radius); foreach (Collider hit in colliders) { float distance = Vector3.Distance (this.transform.position, hit.transform.position); float dmMult = 1 - ((1.0f / Radius) * distance); if (dmMult < 0) dmMult = 0; int castcount = 0; RaycastHit[] hits; List<casthit> castHits = new List<casthit> (); RaycastHit[] casterhits = Physics.RaycastAll (this.transform.position, (hit.transform.position - this.transform.position).normalized, distance + 0.2f); for (int i=0; i<casterhits.Length; i++) { if (casterhits [i].collider) { if (casterhits [i].collider.gameObject != this.gameObject) { castcount++; casthit casted = new casthit (); casted.distance = Vector3.Distance (this.transform.position, casterhits [i].point); casted.index = i; casted.name = casterhits [i].collider.name; castHits.Add (casted); } } } hits = new RaycastHit[castcount]; castHits.Sort ((x,y) => x.distance.CompareTo (y.distance)); for (int i=0; i<castHits.Count; i++) { hits [i] = casterhits [castHits [i].index]; } int hitcount = 0; bool pass = true; for (var i = 0; i < hits.Length && hitcount < 10; i++) { if (hits [i].collider.gameObject == hit.GetComponent<Collider>().gameObject) { pass = true; break; } if (tagDestroyerCheck(hits[i].collider.tag)) { pass = false; break; } hitcount++; } if (pass) { if (hit && hit.GetComponent<Rigidbody>()) hit.GetComponent<Rigidbody>().AddExplosionForce (Force, this.transform.position, Radius, 3.0F); DamagePackage dm; dm.Damage = (int)((float)Damage * dmMult); dm.Normal = hit.transform.forward; dm.Direction = (hit.transform.position - this.transform.position).normalized * Force; dm.Position = hit.transform.position; dm.ID = OwnerID; dm.Team = OwnerTeam; hit.GetComponent<Collider>().SendMessage ("OnHit", dm, SendMessageOptions.DontRequireReceiver); } } Destroy(this.gameObject,Duration); }