-
-
Save gekidoslair/6a8346bf6ae8e786c7aae894ee25dfaf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class RoamBetweenPoints : MonoBehaviour { | |
public Transform[] targets; | |
public LayerMask groundLayer; | |
private Transform target; | |
private NavMeshAgent agent; | |
private Quaternion smoothTilt; | |
private float waitFor = 5f; | |
private float wait = 4f; | |
private bool waiting = true; | |
void Start () { | |
agent = GetComponent<NavMeshAgent>(); | |
smoothTilt = new Quaternion(); | |
} | |
void Update () { | |
if(waiting){ | |
if(wait < waitFor){ | |
wait += Time.deltaTime; | |
agent.Stop(); | |
}else{ | |
Transform newTarget = target; | |
while(newTarget == target){ | |
int randomIndex = Random.Range(0,(targets.Length)); | |
target = targets[randomIndex]; | |
} | |
agent.SetDestination(target.position); | |
wait = 0f; | |
waiting = false; | |
agent.Resume(); | |
} | |
} | |
float distanceToTarget = 0; | |
if(target != null){ | |
distanceToTarget = Vector3.Distance(transform.position, target.position); | |
} | |
if(distanceToTarget < 20){ | |
waiting = true; | |
} | |
RaycastHit rcHit; | |
Vector3 theRay = transform.TransformDirection(Vector3.down); | |
if (Physics.Raycast(transform.position, theRay, out rcHit, 50f, groundLayer)) | |
{ | |
float GroundDis = rcHit.distance; | |
Quaternion grndTilt = Quaternion.FromToRotation(Vector3.up, rcHit.normal); | |
smoothTilt = Quaternion.Slerp(smoothTilt, grndTilt, Time.deltaTime * 2.0f); | |
transform.rotation = smoothTilt * transform.rotation; | |
Vector3 locPos = transform.localPosition; | |
locPos.y = (transform.localPosition.y - GroundDis); | |
transform.localPosition = locPos; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment