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 adeeb500 · Nov 16, 2017 at 06:34 PM · unity 52d game

waypoint stuck at the first waypoint

I have this code in a 2d game.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class GhostMovement : MonoBehaviour {

 public Transform[] waypoints;
 int cur = 0;

 public float speed = 0.3f;


 // Use this for initialization
 void Start () {
     //Debug.Log(currentwaypoint);

 }


 void FixedUpdate () {
     // Waypoint not reached yet? then move closer
     if (transform.position != waypoints[cur].position) {
         Vector2 p = Vector2.MoveTowards(transform.position, waypoints[cur].position, speed);
         GetComponent<Rigidbody2D>().MovePosition(p);
     }
     // Waypoint reached, select next one
     else cur = (cur + 1) % waypoints.Length;
 }

}

I don't why the enemy stuck when it reached the first waypoint.

alt text

alt text

screen-shot-2017-11-16-at-30251-pm.png (116.4 kB)
screen-shot-2017-11-16-at-30316-pm.png (41.6 kB)
Comment
Add comment · Show 4
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 ShadyProductions · Nov 16, 2017 at 06:39 PM 0
Share

Add a Debug.Log(cur) in the else see if it triggers.

comparing floats with == also is a bad idea

avatar image Hellium · Nov 16, 2017 at 07:10 PM 0
Share

Your script works fine here. Are you sure you have added more than one waypoint?

avatar image adeeb500 · Nov 17, 2017 at 12:56 AM 0
Share

Can someone help me?

avatar image haruna9x adeeb500 · Nov 17, 2017 at 10:49 AM 0
Share

1e-5 is too small. I checked your code and found the stop distance at about 1e-3. Legen gave you the answer, to improve performance, can also use sqr$$anonymous$$agnitude will reduce the cost calculation Sqrt.

NOTE: If you do not answer the question, use a comment. If you want to update more information about your question, use the edit function. If the question has been answered, please accept it as a correct answer.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Legend_Bacon · Nov 16, 2017 at 06:53 PM

Hello there,

You should probably not compare Vectors using "==". Instead, try using:

 if(Vector3.Distance(transform.position, waypoints[cur].position) < 0.5f)
 {
    // change to next waypoint here
 }
 else
 {
    // execute movement code here
 }

Aside from that, I would really not recommend using a GetComponent() 60+ times a second. Instead, declare a private Rigidbody2D myBody = null; at the top, and have your start assign it like this:

 private void Start()
 {
 myBody = GetComponent<Rigidbody2D>();
 }

I hope that helps!

~LegendBacon

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 adeeb500 · Nov 16, 2017 at 07:02 PM 0
Share

still same problem

avatar image Legend_Bacon adeeb500 · Nov 17, 2017 at 05:14 PM 0
Share

Hello there,

In that case, the only piece of advice I can give you is to debug. Put debug logs everywhere, see which ones get triggered, what they output, etc... I would especially debug (cur) every frame, but if it always shows the same value, then also debug your calculation (cur +1) & waypoints.length. Finally, debug the position of the ghost and of the waypoint every frame to see what's going on.

And a last thing: Since you use the rigidbody to move your object, make sure it doesn't have constraints that would prevent it from moving in certain directions in the inspector.

I hope that helps. Good luck!

~LegendBacon

avatar image Hellium · Nov 16, 2017 at 07:07 PM 0
Share

I recently learned that comparing two vectors using == is fine thanks to the operator overloading. Under the hood, Unity checks if the magnitude of their difference is less than 1e-5

avatar image
0

Answer by Docien · Nov 16, 2017 at 07:48 PM

Your script should be working fine! Add some Debug.Log() statements in a few areas. Try and output the current waypoint to see if it's actually switching to the next one, and the current waypoints position. narrowing down the possibilities of whats going wrong will definitely help here. Also, check if you've assigned all the waypoints to your Transform array.

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 adeeb500 · Nov 16, 2017 at 08:01 PM 0
Share

I assigned all the waypoints to your Transform array. The variable cur is always zero. I don't know why

avatar image Docien adeeb500 · Nov 17, 2017 at 08:51 PM 0
Share

Alright. Try putting a debug.log() statement under your else statement and output something like "Waypoint Reached!". I have a feeling that the else statement may not be activated at all, for whatever reason.

avatar image
0

Answer by ibrarOzi · Nov 17, 2017 at 10:59 AM

Hello There First of all try calculating a distance range and if player reaches it then increment to next waypoint.

Second try adding the position directly into player position (avoiding the rigid body function).

For debugging purposes just comment the MoveTowards line and directly assign the waypoint position in player position. Then check whether your else condition is working properly or not

2d Rigidbody Reference

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

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

Trying to reset lvl on collsion 1 Answer

Mobile Development screen resolution and aspect ration 1 Answer

Unity 5 Platform Effector 2D Bug or Not 0 Answers

Attacking Enemies at an Angle 0 Answers

How can i fix this shooting code in a 2D plane? 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