private IEnumerator FlyAway(Vector3 direction, float waitTime, bool scared, FlyStatus preStatus) { // leave if (scareCount > scareMaxCount) { flyStatus = FlyStatus.LEAVE; yield break; } flyStatus = FlyStatus.SCARED; Vector3.Normalize(direction); // FLY if (scared) { targetPosition = transform.position + direction * 2f; } else { targetPosition = transform.position + direction; } yield return(new WaitForSeconds(waitTime)); // do what it was doing if (flyStatus == FlyStatus.LANDED) { flyStatus = FlyStatus.LANDING; FindLandTarget(); } else { flyStatus = preStatus; } }
// Start is called before the first frame update void Start() { gameController = FindObjectOfType <DuckHuntPlayGameController>(); duckAnimator = this.GetComponent <Animator>(); hitCry = GetComponent <AudioSource>(); Vector2 currentPosition = this.transform.position; currentX = currentPosition.x; currentY = currentPosition.y; // determine initial flying direction ans speed if (currentX > 0) { direction = -1; this.transform.localRotation = Quaternion.Euler(0, 180, 0); } else { direction = 1; } // At start stage of fly, move fast at vertial, move slow at horizontal hSpeed = direction * hSpeedAtStart * 0.3f; vSpeed = vSpeedAtStart * 2.0f; flyStatus = FlyStatus.Raising; // set the time stamp for next turnning point nextTurnTimeStamp = Time.time + Random.Range(minTimeOfTurnDirection, maxTimeOfTurnDirection) / 10.0f; }
public IEnumerator LandedAndChilling() { Debug.Log("Landed And Chilling"); while (flyStatus == FlyStatus.LANDED) { yield return(new WaitForSeconds(maxPauseTime)); } landed = false; hasLandTarget = false; if (landCount > landMaxStops) { flyStatus = FlyStatus.LEAVE; Debug.Log("Start leaving"); } else if (flyStatus != FlyStatus.LANDED) { yield break; } else { StartCoroutine(FlyAway(LandTarget - transform.position, 5f, false, FlyStatus.LANDED)); } LandTarget = Vector3.zero; }
///////////////////////////////////////////////////////////////////////////// /// Behaviors ///////////////////////////////////////////////////////////////////////////// public void Entering() { // fly in targetPosition = Vector3.MoveTowards(transform.position, descendTarget, descendSpeed * deltaTime); //small random x & z targetPosition.x += Mathf.Sin(Time.time * 4f) * ((Mathf.Cos(Time.time) + 1f) / 4f * descendWanderSide); // OnFlyInFinish if ((descendTarget - transform.position).sqrMagnitude < 1f) { Debug.Log("Entered!"); if (flyScenario == FlyScenario.WANDER) { flyStatus = FlyStatus.WANDER; StartCoroutine(Wandering()); } else { flyStatus = FlyStatus.LANDING; FindLandTarget(); } if (OnEnterFinish != null) { OnEnterFinish(); } } }
void Fly() { if (status == FlyStatus.FlySpline) { //Debug.LogError("status == 0"); //发射,随机自转 tTick += FrameReplay.deltaTime; refreshDelay -= FrameReplay.deltaTime; if (refreshDelay <= 0.0f && !outofArea) { RefreshSpline(); refreshDelay = 0.1f; } if (tTick > tTotal) { status = outofArea ? FlyStatus.FlyReturn : FlyStatus.FlyGoto;//如果还没有撞到敌人.则在接下来的时间里,直线追击敌人 //tTotal = Vector3.Distance(transform.position, owner.WeaponR.position) / returnspeed; //tTick = 0.0f; return; } transform.Rotate(vecRotate, Space.Self); //Debug.LogError("tTick:" + tTick + " tTotal:" + tTotal); Vector3 v = spline.Eval(tTick / tTotal); //Debug.LogError(v.ToString()); transform.position = v; } else if (status == FlyStatus.FlyGoto) { transform.Rotate(vecRotate, Space.Self); Vector3 dir = auto_target.mSkeletonPivot - transform.position; //Debug.LogError("dir.magnitude:" + dir.sqrMagnitude + " speed*time.deltatime:" + speed * Time.deltaTime); //WsGlobal.AddDebugLine(transform.position, transform.position + dir, Color.red, "dir"); if (dir.magnitude <= speed * Time.deltaTime)//下一帧能够超过目标. { status = FlyStatus.FlyReturn; } transform.position = transform.position + dir.normalized * speed * Time.deltaTime; } else if (status == FlyStatus.FlyReturn) { //Debug.LogError("status == 1"); //回收-直线回转-穿墙 transform.Rotate(vecRotate, Space.Self); Vector3 dir = owner.WeaponR.position - transform.position; //Debug.LogError("dir.magnitude:" + dir.sqrMagnitude + " speed*time.deltatime:" + speed * Time.deltaTime); //WsGlobal.AddDebugLine(transform.position, transform.position + dir, Color.red, "dir"); if (dir.magnitude <= speed * Time.deltaTime)//速度太大的时候,可能会2边跑,而且距离都大于5,这样要看夹角是否改变了方向. { //Debug.LogError("WeaponReturned"); owner.WeaponReturned(_attack.PoseIdx); owner.weaponLoader.ShowWeapon(); GameObject.Destroy(gameObject); return; } transform.position = transform.position + dir.normalized * speed * Time.deltaTime; } }
public void OnTriggerEnter(Collider other) { if (other.transform.root.gameObject.layer == LayerManager.Scene) { if (status == FlyStatus.FlySpline || status == FlyStatus.FlyGoto) { status = FlyStatus.FlyReturn; playedTime = 0.0f; totalTime = Vector3.Distance(transform.position, owner.WeaponR.position) / returnspeed; spline.SetControlPoint(2, transform.position); } } else { MeteorUnit unit = other.GetComponentInParent <MeteorUnit>(); if (unit == null) { return; } if (unit == owner && status == FlyStatus.FlyReturn) { //Debug.LogError("WeaponReturned"); owner.WeaponReturned(_attack.PoseIdx); owner.weaponLoader.ShowWeapon(); MeshRenderer mr = gameObject.GetComponentInChildren <MeshRenderer>(); if (mr != null) { BoxCollider bc = mr.gameObject.GetComponent <BoxCollider>(); bc.enabled = false; } GameObject.Destroy(gameObject); return; } if (unit.Dead) { return; } if (attackTick.ContainsKey(unit)) { return; } //同队忽略攻击 if (unit.SameCamp(owner)) { return; } if (unit == owner) { return; } unit.OnAttack(owner, _attack); attackTick.Add(unit, 0.2f); } }
void checkFlyingStatus() { if (transform.position.y > -0.5 && !isHit && flyStatus == FlyStatus.Raising) { duckAnimator.SetBool("FlyHorizontal", true); flyStatus = FlyStatus.FlyHorizontal; } if (transform.position.y > -0.4 && !isHit && flyStatus == FlyStatus.FlyHorizontal) { hSpeed = direction * hSpeedAtStart; vSpeed = vSpeedAtStart; } }
public void OnTriggerEnter(Collider other) { if (other.transform.root.gameObject.layer == LayerMask.NameToLayer("Scene")) { if (status == FlyStatus.FlySpline || status == FlyStatus.FlyGoto) { status = FlyStatus.FlyReturn; tTick = 0.0f; tTotal = Vector3.Distance(transform.position, owner.WeaponR.position) / returnspeed; spline.SetControlPoint(2, transform.position); } } else { MeteorUnit unit = other.GetComponentInParent <MeteorUnit>(); if (unit == null) { return; } if (unit == owner && status == FlyStatus.FlyReturn) { //Debug.LogError("WeaponReturned"); owner.WeaponReturned(_attack.PoseIdx); owner.weaponLoader.ShowWeapon(); GameObject.Destroy(gameObject); return; } if (unit.Dead) { return; } if (attackTick.ContainsKey(unit)) { return; } //同队忽略攻击 if (unit.SameCamp(owner)) { return; } if (unit == owner) { return; } unit.OnAttack(owner, _attack); attackTick.Add(unit, 0.2f); } }
public IEnumerator Landing() { // break if no landTarget if (!hasLandTarget) { yield break; } while ((LandTarget - transform.position).sqrMagnitude > 0.05f * 0.05f) { targetPosition = Vector3.MoveTowards(transform.position, LandTarget, leaveSpeed * 2f * deltaTime); yield return(null); } Debug.Log("Maybe landed~"); // change orientation to things normal RaycastHit hit; if (Physics.Raycast(transform.position, transform.forward, out hit, 2f, DetectLayers)) { landNormal = hit.normal; landed = true; //targetRotation = Quaternion.Euler(landNormal); // doesn't seem to be right... targetRotation = Quaternion.LookRotation(Vector3.up, landNormal); // doesn't seem to be right... if (OnLandFinish != null) { OnLandFinish(); } Debug.Log("Landed!"); flyStatus = FlyStatus.LANDED; landCount++; StartCoroutine(LandedAndChilling()); } else { Debug.Log("Nah, didn't land~"); landed = false; LandTarget = Vector3.zero; hasLandTarget = false; FindLandTarget(); } }
private void OnTriggerStay(Collider other) { if (other.transform.root.gameObject.layer == LayerMask.NameToLayer("Scene")) { if (status == FlyStatus.FlySpline || status == FlyStatus.FlyGoto) { status = FlyStatus.FlyReturn; tTick = 0.0f; tTotal = Vector3.Distance(transform.position, owner.WeaponR.position) / returnspeed; spline.SetControlPoint(2, transform.position); } } else { MeteorUnit unit = other.GetComponentInParent <MeteorUnit>(); if (unit == null) { return; } if (unit.Dead) { return; } if (attackTick.ContainsKey(unit)) { return; } //同队忽略攻击 if (unit.SameCamp(owner)) { return; } if (unit == owner) { return; } unit.OnAttack(owner, _attack); attackTick.Add(unit, 0.2f); } }
public IEnumerator Wandering() { Debug.Log("Start wandering"); while (flyStatus == FlyStatus.WANDER) { yield return(new WaitForSeconds(Random.Range(minPauseTime, maxPauseTime))); if (wanderCount > wanderMacCount) { Debug.Log("Max wander! Leaving now."); flyStatus = FlyStatus.LEAVE; yield return(null); } // wander Vector3 newTarget = Random.insideUnitSphere * wanderRadius; newTarget.y = Mathf.Clamp(newTarget.y, -0.5f, 0.5f); targetPosition += newTarget; wanderCount++; } }
///////////////////////////////////////////////////////////////////////////// /// General ///////////////////////////////////////////////////////////////////////////// private void Update() { // For triggerin event in Editor to test #if UNITY_EDITOR if (Input.GetKeyDown("1")) { CanMove = true; leaveTarget = transform.position; descendTarget = Random.insideUnitSphere * 4f; if (descendTarget.y < 0) { descendTarget.y *= -1; } descendTarget.y = Mathf.Clamp(descendTarget.y, 0.1f, 2f); // calculate target height, e.g. +1 above surface //RaycastHit hit; //if (Physics.Raycast(transform.position, Vector3.down, out hit, 20f)) { // descendTarget = hit.point; //} Debug.Log(descendTarget); flyStatus = FlyStatus.ENTER; } else if (Input.GetKeyDown("2")) { if (flyStatus == FlyStatus.WANDER) { if (OnWanderFinish != null) { OnWanderFinish(); } } flyStatus = FlyStatus.LANDING; FindLandTarget(); } else if (Input.GetKeyDown("3")) { if (flyStatus == FlyStatus.WANDER) { if (OnWanderFinish != null) { OnWanderFinish(); } } flyStatus = FlyStatus.LEAVE; Debug.Log("Start leaving"); } #endif if (!CanMove) { return; } switch (flyStatus) { case FlyStatus.ENTER: Entering(); break; case FlyStatus.LANDED: LandedAndChecking(); break; case FlyStatus.LEAVE: Leaving(); break; } deltaTime = Time.deltaTime; // Update agent if (targetPosition - transform.position != Vector3.zero) { targetRotation = Quaternion.LookRotation(targetPosition - transform.position); } transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, lerpRate * deltaTime / turnTime); transform.position = Vector3.Lerp(transform.position, targetPosition, lerpRate * deltaTime); // Update models follow agent, except rot x & z model.transform.position = transform.position; Vector3 newRot = transform.eulerAngles; if (!landed) { newRot.x = newRot.z = 0; } model.transform.eulerAngles = newRot; }