IEnumerator SimulateProjectile(SoldierController soldierContr) { Vector3 TargetPos = soldierContr.GetPositions(); TargetPos.y = 0f; GameObject newPlane = Instantiate(planePrefab, airstrip.position, airstrip.rotation); float targetDistance = Vector3.Distance(airstrip.position, TargetPos); float projectileVelocity = targetDistance / (Mathf.Sin(2f * angle * Mathf.Deg2Rad) / gravity); newPlane.GetComponent <Plane>().AttackPower = myContrl.SoldierInterface.AttackPower; newPlane.GetComponent <Plane>().EnemyTeamTag = myContrl.EnemyTeamTag; newPlane.GetComponent <Plane>().DestinationPoint = TargetPos; float Vx = Mathf.Sqrt(projectileVelocity) * Mathf.Cos(angle * Mathf.Deg2Rad); float Vy = Mathf.Sqrt(projectileVelocity) * Mathf.Sin(angle * Mathf.Deg2Rad); float flightDuration = targetDistance / Vx; newPlane.transform.rotation = Quaternion.LookRotation(TargetPos - newPlane.transform.position); float elapseTime = 0; while (elapseTime < flightDuration) { newPlane.transform.Translate(0, (Vy - (gravity * elapseTime)) * Time.deltaTime, Vx * Time.deltaTime); elapseTime += Time.deltaTime; yield return(null); } }
private void Update() { if (isFind) { float oldDistance = 1000f; SoldierController _targetContrl = null; foreach (Collider col in Physics.OverlapSphere(transform.position, SoldierInterface.DetectionRadius)) { if (col.tag == enemyTeamTag.ToString() && col.transform.GetChild(0).GetComponent <SoldierController>().IsAlive) { var currentDistance = Vector3.Distance(col.transform.position, GetPositions()); if (oldDistance > currentDistance) { oldDistance = currentDistance; _targetContrl = col.transform.GetChild(0).GetComponent <SoldierController>(); } } } if (_targetContrl != null) { isFind = false; targetContrl = _targetContrl; AttackInterface.AttackTarget(targetContrl); } } if (isMove && isMoveToTarget && targetContrl != null) { if (Vector3.Distance(GetPositions(), TargetContr.GetPositions()) <= SoldierInterface.AttackRange) { isMove = false; isMoveToTarget = false; agent.Stop(); agent.ResetPath(); AttackInterface.AttackTarget(targetContrl); } else { MoveInterface.MoveTo(TargetContr.GetPositions()); } } if (isMove && !isMoveToTarget) { if (agent.remainingDistance <= agent.stoppingDistance) { isMove = false; isFind = true; } } if (targetContrl != null) { Vector3 lookPos = targetContrl.GetPositions() - GetPositions(); if (lookPos != Vector3.zero) { Quaternion rotation = Quaternion.LookRotation(lookPos); myTransform.rotation = Quaternion.Slerp(myTransform.rotation, rotation, Time.deltaTime * SoldierInterface.RotationSpeed); } } }
public void AttackTarget(SoldierController targetContrl) { myContrl.IsFind = false; myContrl.TargetContr = targetContrl; if (Vector3.Distance(targetContrl.GetPositions(), myContrl.GetPositions()) > myContrl.SoldierInterface.AttackRange) { myContrl.IsMoveToTarget = true; myContrl.MoveInterface.MoveTo(targetContrl.GetPositions()); } else { if (canAttack) { canAttack = false; myContrl.StopSoldier(); myContrl.IsAttack = true; myContrl.WeaponInterface.WeaponAttack(targetContrl); StartCoroutine(Colldown()); } } }