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 /
  • Help Room /
avatar image
0
Question by MakerDavid · Mar 18, 2016 at 12:38 PM · c#beginnerarrayspathfinding

A box is going to follow a simple path (Doesn't work).

So I am trying to make a box follow a path consisting of 5 empty game objects scattered around the map. The 5 empty GameObjects names are Waypoint 1 up to Waypoint 5. I have doubled checked at least 5 times. Whenever I run my code I get no error, but my Box Object just spins around. Debug.log(currentwaypoint) is always zero which is understandable. Even though I can probably find a code on the internet that has zero problems, I want to understand why this doesn't work when it seems 100% logical in my eyes.

 using UnityEngine;
     using System.Collections;
     
     public class Moveto : MonoBehaviour {
         public Transform[] waypoints = new Transform[5]; 
         float speed = 50;
         int currentwaypoint = 0;
     
         void Start () {
             waypoints[0] = GameObject.Find("Waypoint1").transform;
             waypoints[1] = GameObject.Find("Waypoint2").transform;
             waypoints[2] = GameObject.Find("Waypoint3").transform;
             waypoints[3] = GameObject.Find("Waypoint4").transform;
             waypoints[4] = GameObject.Find("Waypoint5").transform;
           
         }
         
         void Update () {
             
             if (currentwaypoint < waypoints.Length) {
     
                 transform.position = Vector3.MoveTowards(transform.position, waypoints[currentwaypoint].position, speed * Time.deltaTime);
                 transform.LookAt(waypoints[currentwaypoint]);
                 Debug.Log(currentwaypoint);
                 if (gameObject.transform.position == waypoints[currentwaypoint].position)
                {
                    currentwaypoint++;
                }
                 
             }
         }
     }



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 Jessespike · Mar 18, 2016 at 08:05 PM 0
Share

Code works fine for me. Double check the waypoints in the scene.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by vadithiyan · Mar 18, 2016 at 08:21 PM

The code works fine, tested it just now. Check if your waypoints are named correctly. If it still spins around in the same place, try changing

 (gameObject.transform.position == waypoints[currentwaypoint].position)

to something like

 if(Vector3.SqrMagnitude(waypoints[currentwaypoint].position - gameObject.transform.position) < 0.0001f)

to account for floating point precision.

Comment
Add comment · Show 3 · 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 MakerDavid · Mar 18, 2016 at 08:40 PM 0
Share

I had the empty GameObjects as children to my Box but decided to make them parent less and it worked now. I still don't see why that shouldn't work though, but then again I don't have a lot of experience. It turns in instant though so I will have to work on that.

avatar image vadithiyan MakerDavid · Mar 18, 2016 at 08:47 PM 0
Share

When you move the parent object, the child objects move along with the parent, so when you make a parent object move towards the child it will never reach the child. The same goes for rotation, any rotation applied to the parent will be translated to the child with the parent as a pivot. So the child(the way point) kept moving as you updated the parent, hence the continuous rotation.

avatar image MakerDavid vadithiyan · Mar 18, 2016 at 09:09 PM 0
Share

Oh, yea. That's true, thanks. I actually should have thought of that, but I have been sick for 4 days so I'll let it slide for now :P

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How would you move from a array like from [2] to [3] or backwards like [2] to [1] 2 Answers

Problems with jumping in Doodle Jump like game 2 Answers

Press the button play music in the next scene. 1 Answer

Index out of range error 1 Answer

Detecting a continuous set of objects in a tile based game (ie pipes, walkways, walls) 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