- Home /
Looking for: Simple pathfinding in 2D Tower Defense game with no objects to collide with?
Im currently building a tower defense game.
This is an example of a level I've built:
Now I've looked in to pathfinding using Unity 2d and have pretty much come up empty apart from the notorious A* pathfinding. If I'm right in thinking it's rather complex, being a beginner programmer I wouldn't like to exactly delve in at the deep end if their is a simpler solution to my problem. Is their a method in Unity where I can spawn enemies at the red point and have them walk fairly centralized to the end point?
I don't particularly what any code etc I just want to get an idea of how to go about it. Any help appreciated!
Edit: Ive been made aware of Navmeshes however are they not a pro only feature?
Answer by SloppyDepot · Jan 15, 2014 at 05:33 AM
If I understand you correctly, you want to define a path and have some characters walk it. If that's the case, A* wouldn't really be necessary (since you don't need to find the path, you just need to walk it).
I'll share what I've done for my tower defense game.
After placing the level sprite in the scene, I created an empty game object called "Waypoints" which will act as the parent object for all the individual waypoints.
Then, I started creating the individual waypoints (which are also just empty game objects). The order in which they're placed is important, so just trace the path from start to finish. You should have something that looks like this when you're done:
![alt text][1]
Then, on my character's game object (which needs to have a Rigidbody2D component), I added the following script component:
public class FollowWaypointsScript : MonoBehaviour
{
private int _targetWaypoint = 0;
private BaseLevelScript _levelScript;
private Transform _waypoints;
public float movementSpeed = 3f;
// Use this for initialization
void Start ()
{
_levelScript = GameObject.Find("LevelScript").GetComponent<BaseLevelScript>();
_waypoints = GameObject.Find("Waypoints").transform;
}
// Update is called once per frame
void Update ()
{
}
// Fixed update
void FixedUpdate()
{
handleWalkWaypoints();
}
// Handle walking the waypoints
private void handleWalkWaypoints()
{
Transform targetWaypoint = _waypoints.GetChild(_targetWaypoint);
Vector3 relative = targetWaypoint.position - transform.position;
Vector3 movementNormal = Vector3.Normalize(relative);
float distanceToWaypoint = relative.magnitude;
float targetAngle = Mathf.Atan2(relative.y, relative.x) * Mathf.Rad2Deg - 90;
if (distanceToWaypoint < 0.1)
{
if (_targetWaypoint + 1 < _waypoints.childCount)
{
// Set new waypoint as target
_targetWaypoint++;
}
else
{
// Inform level script that a unit has reached the last waypoint
_levelScript.reduceHearts(1);
Destroy(gameObject);
return;
}
}
else
{
// Walk towards waypoint
rigidbody2D.AddForce(new Vector2(movementNormal.x, movementNormal.y) * movementSpeed);
}
// Face walk direction
transform.rotation = Quaternion.Euler(0, 0, targetAngle);
}
}
(Note: you can ignore the BaseLevelScript parts... They're specific to my game.)
Basically, what the script is doing is:
Setting the initial target waypoint to 0
- Each time FixedUpdate() is called:
Check the distance between the character's current position and the waypoint's position.
If the distance is less than 0.1, the character is close enough to the target waypoint to switch to the next waypoint (if one exists).
If the distance is larger than 0.1, the character walks towards it using rigidbody2D.AddForce.
This works pretty well for me.
I should also mention that I have Linear Drag set to 10 on my character's rigidbody.
Hope this helps. [1]: /storage/temp/20680-waypoints.jpg
Hello!
Thankyou very much for the response, I've implemented your code and took out the BaseLevelScript aspect, also adding in the Waypoints empty game object and filling it with Waypoint empty game objects following my path as you described. Unfortunately it doesn't seem to be doing as expected.
I've stepped through it and it seems to be working out the values of the distances correctly however my character simply moves downwards slowly at a slight angle until running off the screen.
Any idea what this could be?
Did you set the parent Waypoints object to be a waypoint as well or does it not matter where that is located?
OHHHHHHHHHHHHHHHHH. I just thought of Gravity! Very silly of me. Thanks so much for all your help, +1.
Oops, yea... I forgot to mention disabling the gravity. Glad you found it helpful though!
Your answer
![](https://koobas.hobune.stream/wayback/20220613130456im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Tower Target Random Tank 1 Answer
Multiple Animated Objects Causing Lag! 0 Answers
Why is my game pausing? 2 Answers
Generating random tower defense level by placing random square-shaped tiles? 1 Answer
Platform pathfinding 0 Answers