Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Direisnecro · Oct 28, 2013 at 03:46 AM · movement scriptpathfind

Waypoint pathfinding gone terribly wrong.

Hey everyone, i have this script(Below). And its my messy attempt at doing a waypoint pathfinding script without arrays, the problem is, when the enemy gets to the first waypoint, he turns upways, angled himself, and flies away.

 var Waypoint1 : GameObject;
 var Waypoint2 : GameObject;
 var Waypoint3 : GameObject;
 var Waypoint4 : GameObject;
 var Waypoint5 : GameObject;
 var Waypoint6 : GameObject;
 var TargetWaypoint : GameObject;
 
 var Counter : int = 1;
 
 
 
 
 function Start () {
     Waypoint1 = GameObject.Find("Waypoint1");
     Waypoint2 = GameObject.Find("Waypoint2");
     Waypoint3 = GameObject.Find("Waypoint3");
     Waypoint4 = GameObject.Find("Waypoint4");
     Waypoint5 = GameObject.Find("Waypoint5");
     Waypoint6 = GameObject.Find("Waypoint6");
     TargetWaypoint = GameObject.Find("Waypoint" + Counter);
 }
 
 function Update () {
     
     var toTarget : Vector3 = TargetWaypoint.transform.position - transform.position;
     
  
     var turnRate : float = 200 * Time.deltaTime;
     var lookRotation : Quaternion = Quaternion.LookRotation(toTarget);
     transform.rotation = Quaternion.RotateTowards(transform.rotation, lookRotation, turnRate);
     
     transform.Translate(Vector3.forward * Time.deltaTime);
     
 }
 
 
 function OnCollisionEnter( hit : Collision)
 {
     if (hit.gameObject.name == GameObject.Find("Waypoint" + Counter))
     {
         Counter = Counter + 1;
         Debug.Log("We collided");
         
         TargetWaypoint = GameObject.Find("Waypoint" + Counter);
     }
 
 
 
 
 }






I noticed that this code: if (hit.gameObject.name == GameObject.Find("Waypoint" + Counter)) { Counter = Counter + 1; Debug.Log("We collided");

         TargetWaypoint = GameObject.Find("Waypoint" + Counter);
     }

is not executing for some reason. so i figure thats the problem. but i dont know why its not working.

Any help would be nice.

Comment
Add comment · Show 1
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 sethuraj · Oct 28, 2013 at 07:06 AM 0
Share

Way points are usually used as invisible nodes of the path.That is player will penetrate through these points.'OnCollisionEnter()' works based on mesh blocking and requires more physics and rigid body dynamics calculations. Use 'OnTriggerEnter()' ins$$anonymous$$d of 'OnCollisionEnter()' and make your all waypoints objects assigned with a box or sphere Collider with 'Trigger' enabled.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sethuraj · Oct 28, 2013 at 07:19 AM

You are missing a RigidBody component on your player object.Add a RigidBody component to the object which you are attaching this script to.Enable 'IsTrigger' on all your waypoint object colliders.

Use this ('OnTriggerEnter' instead of 'OnCollisionEnter') for detecting the way points.

 function OnTriggerEnter( hit : Collider)
 {
 
     if (hit.gameObject.name ==TargetWaypoint.name)
     {
        Counter = Counter + 1;
        Debug.Log("We collided");
  
        TargetWaypoint = GameObject.Find("Waypoint" + Counter);
     }
  
 }


Tested Working.. Cheers... :-)

Comment
Add comment · 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

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

16 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

Related Questions

Looking for: Simple pathfinding in 2D Tower Defense game with no objects to collide with? 1 Answer

[CLOSED] Can't Move Left Or Right (a/d) In This Movement Script? [CLOSED] 0 Answers

How do I correctly combine behaviors on a base class? 1 Answer

Mobile camera smooth 0 Answers

Limiting the rotation based on the angle of the player mesh 0 Answers


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