- Home /
Sit automatically at waypoint
I have a patrolling script. It just makes the Character walk to random waypoints, and its all set up to work with a random timer. All that works fine! The issue is, when the Character reaches the way point, I want to make sure he sits in an exact position and plays his animation for that waypoint. Then once the timer is done, he will go back to patrolling as he should.
This is the patrol script
void Patrolling (){
character.Move (agent.desiredVelocity, false, false);
if(agent.remainingDistance < agent.stoppingDistance || waitTimeSet)
{
if(!waitTimeSet)
{
patrolWaitTime = Random.Range(20,40);
animator.SetBool(patrolWayPoints[wayPointIndex].name, true);
patrolTimer = 0;
waitTimeSet = true;
}
patrolTimer += Time.deltaTime;
if(patrolTimer >= patrolWaitTime)
{
animator.SetBool(patrolWayPoints[wayPointIndex].name, false);
if(wayPointIndex != null)
lastPoint = wayPointIndex;
wayPointIndex = Random.Range(0, patrolWayPoints.Length);
if(lastPoint == wayPointIndex)
{
int[] tempIntArray = new int[patrolWayPoints.Length - 1];
int cnt = 0;
for (int i = 0; i < patrolWayPoints.Length; i++)
{
if(i != lastPoint)
{
tempIntArray[cnt] = i;
cnt ++;
}
wayPointIndex = tempIntArray[Random.Range (0,tempIntArray.Length)];
}
}
agent.destination = patrolWayPoints[wayPointIndex].position;
waitTimeSet = false;
}
}
else
{
patrolTimer = 0;
}
}
IEnumerator WaitForSomeTime(){
waitTime = Random.Range(20,31);
yield return new WaitForSeconds(waitTime);
isClicked = false;
}
}
And this is the script that SHOULD make him sit at a point and play animation. Right now It is set to if you press E he will sit at the point. Bascally I want this to happen automatic instead of having to press a button. Once he reaches waypoint, get inside car. After timer is up, he will break from the waypoint and go back to patrolling. Make sense? haha The real effect will be he will roam an area and do different things at each way point. I just wanna make sure he is at a exact point.
void Update () {
Vector3 ahead = transform.forward;
Vector3 rayStart = new Vector3 (this.transform.position.x, this.transform.position.y + 1f, this.transform.position.z);
Ray ray = new Ray (rayStart, ahead);
RaycastHit hit;
if (Physics.Raycast (ray, out hit, 1f)) {
if (hit.transform.gameObject.tag == ("carDoor")) {
currCar = hit.transform.gameObject;
if ((currCar != null && !driving)) {
if (Input.GetKeyDown (KeyCode.E)) {
if (currCar.GetComponent<CarControl> ().lowDoors) {
_getInLowCar = true;
} else {
_getInCar = true;
}
}
}
}
}
if (driving) {
if (Input.GetKeyDown (KeyCode.E)) {
GameObject sitPoint = currCar.transform.Find ("sitPoint").gameObject;
this.transform.position = sitPoint.transform.position;
this.transform.rotation = sitPoint.transform.rotation;
this.transform.parent = null;
driving = false;
_getInCar = false;
_getInLowCar = false;
}
}
}
//FIXED UPDATE METHOD
void FixedUpdate(){
_animator.SetBool("GetInCar", _getInCar);
_animator.SetBool("GetInLowCar", _getInLowCar);
_animator.SetBool("Grounded", isGrounded());
}
IEnumerator GetInCar(){
_charCtrl.enabled = false;
GameObject sitPoint = currCar.transform.Find("sitPoint").gameObject;
this.transform.parent = sitPoint.transform;
this.transform.position = sitPoint.transform.position;
this.transform.rotation = sitPoint.transform.rotation;
yield return new WaitForSeconds(4.5f);
driving = true;
currCar.GetComponent<CarControl>().openDoor = false;
Transform Seat = currCar.transform.Find("seat");
this.transform.parent = Seat.transform;
this.transform.position = Seat.transform.position;
this.transform.rotation = Seat.transform.rotation;
}
IEnumerator GetOutCar(){
Transform Seat = currCar.transform.Find("seat");
this.transform.parent = Seat.transform;
this.transform.position = Seat.transform.position;
this.transform.rotation = Seat.transform.rotation;
GameObject sitPoint = currCar.transform.Find("sitPoint").gameObject;
yield return new WaitForSeconds(4.5f);
_charCtrl.enabled = true;
driving = false;
currCar.GetComponent<CarControl>().openDoor = false;
this.transform.position = sitPoint.transform.position;
this.transform.rotation = sitPoint.transform.rotation;
sitPoint = null;
Seat = null;
this.transform.parent = null;
}
bool isGrounded()
{
return Physics.Raycast(transform.position, -Vector3.up, _distToGround + 0.05f);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220611235746im_/https://answers.unity.com/themes/thub/images/avi.jpg)