void Patrol() { Vector3 curPos = agent.transform.position; //get agent current position Vector3 goalPos = waypoints[currentWaypointIndex].position; //where you want to go agent.transform.forward = (goalPos - curPos).normalized; //to change where the enemy is facing visually elapsed += Time.deltaTime; if (elapsed > 0.5f) { elapsed -= 0.5f;//set elapsed back to 0.0f navAgent.SetDestination(goalPos); } if ((goalPos - agent.transform.position).magnitude < waypointReachedThreshold)//if goalPos - agent.transform.position is less than waypointReachedThreshold(1.0f) { ++currentWaypointIndex; if (currentWaypointIndex >= waypoints.Length) { currentWaypointIndex = 0; } } //have we noticed out target? if ((seekTarget.position - agent.transform.position).magnitude < detectionRadius)//if less than 2.0f radius { goalPos = seekTarget.transform.position; navAgent.SetDestination(goalPos); currentState = States.Seek; } }
IEnumerator DestinationSetter() { RaycastHit _hitInfo; if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out _hitInfo)) { agent.SetDestination(_hitInfo.point); } yield return(new WaitForSeconds(200)); }
/// <summary> /// Makes the animal flee a certain collider. /// </summary> /// <param name="_collider">Collider to flee.</param> /// <returns></returns> protected override IEnumerator Flee(Collider _collider) { yield return(new WaitForSeconds(fleeDelay)); // Trigger animation SetAnimationOnline(1); float _xMinDistance = _collider.bounds.extents.x + detector.Collider.bounds.size.x; float _zMinDistance = _collider.bounds.extents.z + detector.Collider.bounds.size.z; Vector3 _actualDestination = new Vector3(); Vector3 _newDestination = new Vector3(); float _direction = 0; // Move while in range while ((Mathf.Abs(_direction = (detector.Collider.bounds.center.x - _collider.bounds.center.x)) < _xMinDistance) && (Mathf.Abs(_collider.bounds.center.z - detector.Collider.bounds.center.z) < _zMinDistance)) { _direction = Mathf.Sign(_direction); _newDestination = new Vector3(_collider.bounds.center.x + (_xMinDistance * 1.5f * _direction), transform.position.y, transform.position.z); if (_newDestination != _actualDestination) { if (_direction != isFacingRight.ToSign()) { transform.Rotate(Vector3.up, 180); transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y, transform.localScale.z * -1); isFacingRight = !isFacingRight; } // New destination ignored, got to be fixed _actualDestination = _newDestination; Debug.Log(_actualDestination); agent.SetDestination(_actualDestination); // Set destination for other clients TDS_RPCManager.Instance.RPCPhotonView.RPC("CallMethodOnline", PhotonTargets.Others, TDS_RPCManager.GetInfo(photonView, GetType(), "SetDestination"), new object[] { _actualDestination.x, _actualDestination.y, _actualDestination.z }); } yield return(null); } // Trigger animation SetAnimationOnline(0); fleeCoroutine = null; }
private void Update() { if (Input.GetKeyDown(KeyCode.Space)) { if (target != null) { agent.SetDestination(target.position); } } }
/// <summary> /// Get the next destination of the rabbit and set it on the navmeshagent /// Flip the rabbit and invert its goright boolean /// </summary> private void Run() { if (!PhotonNetwork.isMasterClient) { return; } float _x = goRight ? boundRight : boundLeft; Vector3 _targetPosition = new Vector3(_x, transform.position.y, transform.position.z); goRight = !goRight; agent.SetDestination(_targetPosition); TDS_RPCManager.Instance?.RPCPhotonView.RPC("CallMethodOnline", PhotonTargets.All, TDS_RPCManager.GetInfo(photonView, this.GetType(), "Flip"), new object[] { }); }
private IEnumerator MoveCat() { rigidbody.useGravity = true; agent.SetDestination(catState == CatState.RightPerch ? rightPerchInfos.LandingPosition : leftPerchInfos.LandingPosition); SetAnimationState((int)CatAnimationState.Run); SetAnimationState((int)CatAnimationState.EndJump); while (agent.IsMoving) { yield return(null); } movementCoroutine = StartCoroutine(GetOnPerch()); rigidbody.useGravity = false; }