- Home /
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.
Add a Debug.Log(cur) in the else see if it triggers.
comparing floats with == also is a bad idea
Your script works fine here. Are you sure you have added more than one waypoint?
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.
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
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
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
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.
I assigned all the waypoints to your Transform array. The variable cur is always zero. I don't know why
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.
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
Your answer
Follow this Question
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