Hey, thanks a lot, your line fixed it, even without the rest, at least in the new code, I didn’t check the old one.
I have a few more questions if you don’t mind please.
Is it possible to add an offset to the center of the controller?
The center field doesn’t work in 2D I think, or if it does, it doesn’t update the Gizmo (the yellow circle), or I don’t get how it works hehe. The Unity controller has a center option too and it’s a Vector3 with 3 fields (x, y ,z), while yours it only has 1 field and if I change the value it doesn’t move the yellow circle and I’d like to add an offset along the Y axis. It’s not a big deal, just wanted to know if it’s possible and how.
The second problem is about flipping the sprite of the character, which in 2D you usually do according to the position of your target or to the direction. To flip the sprite, I tried to use the current waypoint the character is going to, but sometimes while the character is following the path, it turns backward for no reason. It’s like the current waypoint, which shoud be in front of the character, just for a single frame, fell back. You can see it here:
The AI is following the path, the current waypoint should always be in front of the AI, because as soon as the AI gets close to the current waypoint, that waypoint is updated with the next one…and still it flips backward.
This is the function of the movement:
private void MoveToTarget()
_controller.locked = false;
_isMoving = true;
// Move the AI towards the current waypoint.
_controller.SetTarget(path.vectorPath[_currentWaypoint], speed, speed);
transform.position += _controller.CalculateMovementDelta(Time.fixedDeltaTime);
// Check the distance from the current waypoint and switch to the next one.
if (Vector3.Distance(transform.position, path.vectorPath[_currentWaypoint]) < nextWaypointDistance)
This is the Flip function:
private void Flip(Vector2 targetPos)
if (transform.position.x < targetPos.x && !_isFacingRight)
_spriteRen.flipX = false;
_shadowRen.flipX = false;
_isFacingRight = true;
else if (transform.position.x > targetPos.x && _isFacingRight)
_spriteRen.flipX = true;
_shadowRen.flipX = true;
_isFacingRight = false;
Now if I Flip the sprite before I switch to the next waypoint I get the weird behaviour, like in the GIF.
If I Flip the sprite after the switch, then I get an Array out of index error, because the _currentWaypoint can exceed the Array length…the weird thing is, even if I set the the _currentWaypoint equals to the Array length (in case it exceeds it), it still gives me the same error.
Am I missing something about the .SetTarget or the .CalculateMovementDelta? Maybe an update to the Array that I don’t know?
I tried to use the current and the previous positions of the AI to flip it and that weird behaviour still happens…it’s like every now and then, just for a single frame, the AI is pulled back and for this reason it flips backward, because the current poisition, just for that frame, instead of been in front of the previous position, it’s behind it.
Could it be the updating of the path? Maybe it uses a previous stored position as a starting point of the new the path and AI slightly jump backwards?
Thanks again for your time.