Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ouija · Aug 13, 2015 at 07:21 PM · carwaypointartificial intelligence

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);
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

assign waypointcircuit to waypointprogresstracker at runtime 1 Answer

Check whether car has passed a point in world space 1 Answer

The player's car following a certain path 1 Answer

City traffic Lanes 3 Answers

Clones of Prefab not behaving as Prefab 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges