Reputation: 55
I've recently finished my project on A* pathfinding, following Sebastian Lague's playlist on Youtube. However, and I believe that was the case for him as well, I can't get the seekers (which are capsules) from entering each other. It almost worked when I gave them rigidbodies, but then they started teleporting everywhere and were being generally very buggy.
Here's the Coroutine that makes the seekers follow the shortest path found:
IEnumerator FollowPath()
{
bool followingPath = true;
int pathIndex = 0;
transform.LookAt(path.lookPoints[0]);
float speedPercent = 1;
while (followingPath)
{
Vector2 pos2D = new Vector2(transform.position.x, transform.position.z);
while (path.turnBoundaries[pathIndex].HasCrossedLine(pos2D))
{
if (pathIndex == path.finishLineIndex)
{
followingPath = false;
break;
}
else
{
pathIndex++;
}
}
if (followingPath)
{
if (pathIndex >= path.slowDownIndex && stoppingDst > 0)
{
speedPercent = Mathf.Clamp01(path.turnBoundaries[path.finishLineIndex].DistanceFromPoint(pos2D) / stoppingDst);
if (speedPercent < 0.01f)
{
followingPath = false;
}
}
Quaternion targetRotation = Quaternion.LookRotation(path.lookPoints[pathIndex] - transform.position);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.deltaTime * turnSpeed);
transform.Translate(Vector3.forward * Time.deltaTime * speed * speedPercent, Space.Self);
}
yield return null;
}
}
What's making them go forward is the transform.Translate()
method toward the end. I'd really appreciate it if someone could help me out with this. Let me know if I should've included more parts of my code.
Upvotes: 1
Views: 174
Reputation: 55
So it turns out I am a fool, and the capsule is actually the child of an empty GameObject which has the script (I did this in order to change the pivot point of the object).
All I had to do was remove the collider and the rigidbody from the capsule itself and add them to the parent object, and now everything works.
Upvotes: 0
Reputation: 422
transform.Translate()
just moves a gameobject, it doesn't know or care what else exists in the world.
If you're using rigidbodies, you want to use myRigidBody.MovePosition()
.
If you aren't, then you need to find a way to manually detect and handle collisions.
More information here:
Upvotes: 1