//called when shootobject hit something void OnTriggerEnter(Collider col) { if (state == _State.Hit) { return; //if the shootobject has hit something before this, return } //if the shootobject hits another shootobject from friendly unit if (!GameControl.SOHitFriendly() && col != null) { if (srcLayer == col.gameObject.layer) { return; } } //register the state of the shootobject as hit state = _State.Hit; TDS.CameraShake(impactCamShake); //when hit a shootObject if (col != null && col.gameObject.layer == TDS.GetLayerShootObject()) { //if this is a beam shootobject, inform the other shootobject that it has been hit (beam doesnt use a collider) if (type == _SOType.Beam) { col.gameObject.GetComponent <ShootObject>().Hit(); } } //when hit a collectible, destroy the collectible if (col != null && col.gameObject.layer == TDS.GetLayerCollectible()) { ObjectPoolManager.Unspawn(col.gameObject); return; } //if there's a attack instance (means the shootobject has a valid attacking stats and all) if (attInstance != null) { float aoeRadius = attInstance.aStats.aoeRadius; //for area of effect hit if (aoeRadius > 0) { Unit unitInstance = null; //get all the potental target in range Collider[] cols = Physics.OverlapSphere(thisT.position, aoeRadius); for (int i = 0; i < cols.Length; i++) { //if the collider in question is the collider the shootobject hit in the first place, apply the full attack instance if (cols[i] == col) { unitInstance = col.gameObject.GetComponent <Unit>(); if (unitInstance != null) { unitInstance.ApplyAttack(attInstance.Clone()); } continue; } //no friendly fire, then skip if the target is a friendly unit if (!GameControl.SOHitFriendly()) { if (cols[i].gameObject.layer == srcLayer) { continue; } } unitInstance = cols[i].gameObject.GetComponent <Unit>(); //create a new attack instance and mark it as aoe attack, so diminishing aoe can be applied if enabled AttackInstance aInstance = attInstance.Clone(); aInstance.isAOE = true; aInstance.aoeDistance = Vector3.Distance(thisT.position, cols[i].transform.position); //apply the attack if (unitInstance != null) { unitInstance.ApplyAttack(aInstance); } } } else { if (col != null) { //get the unit and apply the attack Unit unitInstance = col.gameObject.GetComponent <Unit>(); if (unitInstance != null) { unitInstance.ApplyAttack(attInstance); } } } if (col != null) { //apply impact force to the hit object Vector3 impactDir = Quaternion.Euler(0, thisT.eulerAngles.y, 0) * Vector3.forward; TDSPhysics.ApplyAttackForce(thisT.position, impactDir, col.gameObject, attInstance.aStats); } } //add collider to the condition so the shootobject wont split if the shootObject didnt hit anything (it could have just reach the range limit) if (splitUponHit && col != null) { Split(col); } Hit(); }
//function call to fire the object public void Shoot(int srcL, float srcR, Transform shootPoint, AttackInstance aInstance = null, AimInfo aimInfo = null) { Init(); thisObj.SetActive(true); //cached all the passed information lcoally attInstance = aInstance; srcLayer = srcL; //the layer of the shooting unit (so we know it's from player or AI) srcRange = srcR; //the range of the shooting unit (so we know when to stop) state = _State.Shot; shootTime = Time.time; //to track how long the shoot object is has been travelledDistance = 0; //to track how far the shoot object is has been fired //if there's any hideObject, set it to true (it's probably set to false when the shoot-object last hit something) if (hideObject != null) { hideObject.SetActive(true); } //instantiate the shoot effect ShootEffect(thisT.position, thisT.rotation, shootPoint); if (type == _SOType.Simple || type == _SOType.Homing) { //if(aInstance!=null && thisCollider.enabled){ //~ Physics.IgnoreCollision(aInstance.srcUnit.GetCollider(), thisObj.GetComponent<Collider>(), true); //Debug.Log("collision avoidance with shooter unresolved"); //Physics.IgnoreCollision(srcCollider, thisCollider, true); //} // for homing shootobject, the shootobject needs to be aiming at some position, or a specific unit if (type == _SOType.Homing) { if (aimInfo.targetT != null) { targetUnit = aimInfo.unit; } targetPos = aimInfo.hitPoint + new Vector3(Random.value - 0.5f, 0, Random.value - 0.5f) * 2 * spread; initialPos = shootPoint.position; initialDist = Vector3.Distance(targetPos, initialPos); curveMod = Random.Range(0, 2f); dummyPos = thisT.TransformPoint(Vector3.forward * speed * 5); dummyPos = (targetPos + dummyPos) / 2; } } else if (type == _SOType.Beam) { //if(attachedToShootPoint) transform.parent=shootPoint; thisT.parent = shootPoint; ObjectPoolManager.Unspawn(thisObj, beamDuration - .01f); } else if (type == _SOType.Point) { StartCoroutine(PointRoutine(aimInfo)); } //update the layermask used to do the hit detection in accordance to rules set in GameControl UpdateSphereCastMask(!GameControl.SOHitFriendly(), !GameControl.SOHitShootObject(), !GameControl.SOHitCollectible()); }