Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 mkobaner · Mar 26, 2016 at 05:14 PM · waypointalgorithm

Waypoint system suggestions

Hello,

i try to create a system so whenever i pass thourgh a waypoint player will automatically will face the next waypoint and if player wants to go back than this time he will aim for the previous waypoint. I wrote the following script even tough it works it doesnt dowhat i want.

After passing through first waypoint and second waypoint when i wanted to go back it reverts the controls so even tough i press backward i go forward.

You can see what happens here https://youtu.be/tCOx3TxSQcs

I think im making a logic error somewhere but i couldnt see so far.

Anyway here is my code, im using 2 gameobjects for waypoints now,any ideas on targeting next waypoint in a kiss ( keep it simple and stupid) would be great.

 using UnityEngine;
 using System.Collections;
 
 public class Control : MonoBehaviour {
 
 
     public float speed = 6.0F;
 
     // those decide where is our direction and where will we look,
     //will need to find a clever targeting system for passed around many wps later
     public GameObject nextWp;
     public GameObject prevWp;
 
     // triggered bool avoids multiple triggering on same object
     bool triggered;
     // forwarded bool helps to see direction being faced and moved is the same
     bool forwarded;
     // x saves whether we were moving forward or backward which will be used in ontrigger conditions
     float x;
 
 
     // this is the y position value
     public float curY2;
 
 
     // wait before checking triggered false for ontrigger
     IEnumerator Wait(){
         yield return new WaitForSeconds (1);
     }
 
 
     void Start(){
         // getting value for y position and setting forwarded true
             curY2=transform.rotation.y;
             forwarded=true;
 
         }
 
     
 
     // Update is called once per frame
     void Update () {
 
 
         // getting xvalue fromn input than move object also setting x to 1 for on trigger condition
         if (Input.GetAxis("Horizontal")> 0)
             {
                 transform.Translate(Vector3.forward * Time.deltaTime *speed );
             x = 1;
 
             // if cube was going -x direction than this if condition happens and
             //it swaps the direction 180degrees and also set forwarded true
             if (forwarded==false)
             {
                 curY2 = curY2 - 180;
                 forwarded = true;
 
             }
 
 
             }
 
         // if cube was going x direction than this if condition happens and
         //it swaps the direction 180degrees and also set forwarded false
             
         if (Input.GetAxis("Horizontal")< 0)
         {
             transform.Translate(-Vector3.forward * Time.deltaTime*speed );
             x = -1;
             if (forwarded == true) 
             {
                 curY2 = curY2 + 180;
                 forwarded = false;
             }
         }
             
         curY2 = this.transform.rotation.y;
     }
 
 
     void OnTriggerEnter(Collider temas)
     {
         
 
         if (temas.gameObject.name == "45degree" && x > 0&& triggered==false) {
 
             transform.LookAt(nextWp.transform.position);
             Debug.Log("contact x bigger than 0");
 
         }
 
         if (temas.gameObject.name == "45degree" && x < 0&& triggered==false) {
             
             transform.LookAt(prevWp.transform.position);
             Debug.Log("contact h smaller than 1");
 
         }
 
         triggered = true;
         StartCoroutine ("Wait");
         triggered = false;
     }
 
 
 
 }
 




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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Fredex8 · Mar 26, 2016 at 09:36 PM

That Wait coroutine isn't setup correctly. Currently your trigger is just going to be setting it true then immediately setting it false. The wait won't effect it.

      IEnumerator Wait(){
          triggered = true;
          yield return new WaitForSeconds (1);
          triggered = false;
      }

Then just call StartCoroutine("Wait"); when you want to toggle the bool. You can lose the quotations on it by the way and write StartCoroutine(Wait()); instead. As I posted in your last question you should probably roll those two if statements under OnTriggerEnter into one still though. Giving colliders excessive amount of conditions to check is just going to slow things down.

     void OnTriggerEnter(Collider temas)
     {
         if (temas.gameObject.name == "45degree" && triggered == false)
         {
             if (x > 0)
             {
                 transform.LookAt(nextWp.transform.position);
                 Debug.Log("contact x bigger than 0");
             }
             else if (x < 0)
             {
                 transform.LookAt(prevWp.transform.position);
                 Debug.Log("contact h smaller than 1");
             }
         }
         StartCoroutine(Wait());
     }


Comment
Add comment · Show 2 · Share
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
avatar image mkobaner · Mar 26, 2016 at 09:40 PM 0
Share

Well so far, that script is good for stopping multiple entrances, so it shouldnt be a problem thanks for insight tough.

avatar image Fredex8 mkobaner · Mar 26, 2016 at 10:07 PM 0
Share

Sorry I hadn't watched the video to see the issue and just got distracted by optimising stuff ins$$anonymous$$d....

Your best bet for creating a waypoint system like this (without using a navmesh) is probably to create a list or array of all the waypoints. Rather than using the wait coroutine to exclude further collisions with that object you should have a variable store the current waypoint.

      void OnTriggerEnter(Collider temas)
      {
          if (temas.gameObject.tag== "Waypoint")
          {
                  if (currentWaypoint != temas.gameObject)
                  {
                        currentWaypoint = temas.gameObject;
                        //$$anonymous$$ove to next/last waypoint
                  }
          }
      }

Something like that will stop you having to worry about multiple collisions with the same waypoint. The waypoint before and after that one would be the entry before and after it in the list so you can then get these objects and navigate towards them.

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

39 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 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

Error with the bfs algorithm That I can not understand 1 Answer

DFS & BFS help needed 1 Answer

Move an object through a set of positions 3 Answers

Facing moving direction using Waypoints 1 Answer

How do I add more waypoints to a moving platform script. 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