Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void ControlRandomWander()
- {
- float pointDist = Vector3.Distance(currentWanderPos, transform.position);
- if(pointDist < 2f || currentWanderPos == Vector3.zero)
- {
- wanderWaitTimer += Time.deltaTime * 15;
- anims.LookAround(true);
- if (wanderWaitTimer >= wanderWaitTime)
- {
- Vector3 randPos = GetRandomPositionAroundTarget(transform.position, -wanderRadius, wanderRadius);
- NavMeshPath pth = new NavMeshPath();
- NavMesh.CalculatePath(transform.position, randPos, agent.areaMask, pth);
- float pathDist = 0f;
- if (pth.status == NavMeshPathStatus.PathComplete)
- {
- for (int i = 0; i < pth.corners.Length - 1; i++)
- {
- pathDist += Vector3.Distance(pth.corners[i], pth.corners[i + 1]);
- }
- Debug.Log(pathDist);
- if (pathDist <= wanderRadius)
- {
- currentWanderPos = randPos;
- wanderWaitTime = Random.Range(wanderWaitMin, wanderWaitMax);
- anims.LookAround(false);
- wanderWaitTimer = 0;
- MoveToPosition(randPos, true);
- }
- }
- }
- }
- else
- {
- if (agent.destination != currentWanderPos)
- {
- MoveToPosition(currentWanderPos, true);
- }
- }
- }
- Vector3 GetRandomPositionAroundTarget(Vector3 pos, float minRange, float maxRange)
- {
- float offsetX = Random.Range(minRange, maxRange);
- float offsetZ = Random.Range(minRange, maxRange);
- Vector3 orgPos = pos;
- orgPos.x += offsetX;
- orgPos.z += offsetZ;
- NavMeshHit hit;
- if(NavMesh.SamplePosition(orgPos, out hit, 5f, agent.areaMask))
- {
- Debug.Log(hit.position);
- return hit.position;
- }
- Debug.Log(hit.position);
- return pos;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement